Skip to content

Description ​

我是用 NAS 存放備份檔。方式有 2 種

  1. rsync 來建立鏡像資料夾。您需先用 CIFS 或 NFS 來將 Synology NAS 上的資料夾掛載至 Linux 伺服器。
  2. Linux 執行指令,無需掛載資料夾。

其中第 2 種方式是我實做出來的方式。

rsync ​

rsync 是一種異地備份軟體,與一般備份軟體不同的是,rsync 採用增量備份的方式,此類備份方式在每次備份前會先比較兩邊資料的差異,然後僅將差異的資料備份過去,而非備份全部的資料。

rsync 用於遠程複製和同步文件和目錄,以及在 Linux/Unix 系統中本地複製和同步。借助 rsync 命令,您可以跨目錄,跨硬碟和網路遠端和本地複製和同步數據,在兩台 Linux 計算機之間執行數據備份和鏡像。

rsync 的運作方式分為以下兩種:

  • 伺服器(Server)模式

    • 以常駐(Daemon)的方式運作,亦即以伺服器模式來服務(預設通訊埠為873),通常會在目的端運作,接收其他主機的備份資訊。
  • 客戶端(Client)模式

    • 以一般程式方式運作,可視為使用者端(Client)程式,通常用在來源端主機上,將資料備份到備份主機上運作。

rsync 優點和功能 ​

  • 它有效的將文件複製到遠端系統或從遠端系統同步文件。
  • 支持複製 links、devices、owners、groups 和 permissions。
  • 它比 scp(Secure Copy)更快,因為 rsync 使用遠端更新協議,它允許僅傳輸兩組文件之間的差異。第一次,它將文件或目錄的全部內容從源複製到目標,但是從下次起,它僅將更改的塊和字節複製到目標。
  • Rsync 消耗較少的 bandwidth,因為它使用壓縮和解壓縮方法,同時發送和接收數據兩端。

rsynv 參數 ​

  • -v

    • verbose
  • -z

    • 壓縮文件數據
  • -h

    • 人類可讀的輸出數字,以人類可讀的格式
  • -b

    • 表示要產生備份檔。當目的端上已存在同樣的檔案時,就將原先的檔案重新命名為其他的檔名,在此組態下,可利用 --suffix 子選項來命名備份檔案名稱的前綴檔名。另外,也可以指定 --backup-dir 子選項來設定備份檔的存放位置。
  • -u

    • 在備份時會略過所有已經存在於目的端,且文件時間比要備份的檔案為新的檔案。
  • -l

    • 要保留符號連結(Symbolic Link)型式的檔案。
  • -p

    • 表示要保留檔案的權限資訊。
  • -o

    • 要保留檔案擁有者資訊(僅對擁有者為root有效)。
  • -g

    • 將保留檔案所屬群組資訊。
  • -t

    • 表示要保留檔案時間資訊。
  • -r

    • 表示以遞迴的方式來備份資料,包括所要備份目錄下的所有子目錄。
  • -a

    • 等同於設定 -rlptgoD 等組態。
  • -D

    • 表示要保留設備檔案資訊(僅對擁有者為root有效)。
  • --progress

    • 顯示進度
  • --delete

    • 刪除來源端已經不存在但在目的端存在的檔案。
  • --force

    • 當目的端的目錄被覆蓋時,就強制先刪除該目錄。
  • --bwlimit

    • 限制 I/O bindwitdh
  • --daemon

    • 表示 rsync 將以常駐程式的型式來執行,亦即以伺服器的型式來執行。
  • --remove-source-files

    • 成功傳輸後,將來源檔案刪除

rsync 啟用 ​

shell
$ sudo vi /etc/default/rsync
RSYNC_ENABLE=true #預設 false

實作 ​

ubuntu to ubuntu ​

  • ubuntu
    • 192.168.222.128
    • client
  • ubuntu
    • 192.168.222.132
    • server(client 備份資料儲存地方)

預設上無須配置 rsync 設定檔即可傳輸(範例1、2)

  1. 複製目錄/檔案,從 128 傳至 132(IP)
shell
$ rsync -zvh test.csv.zip lab702@192.168.222.132:/tmp
The authenticity of host '192.168.222.132 (192.168.222.132)' can't be established.
ECDSA key fingerprint is SHA256:TmidBei2XVw7f2lqHuSrjfFHsFRz6riglpwY/tKXlXc.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.222.132' (ECDSA) to the list of known hosts.
lab702@192.168.222.132's password:
skipping directory .
test.csv.zip

sent 675.07M bytes  received 35 bytes  8.49M bytes/sec
total size is 674.68M  speedup is 1.00
  1. 複製目錄/檔案,從 132 傳至 128(IP)
shell
lab702@elk:~$ ls /tmp/
dupl-usagereport-4684-20190214012833.json  HttpServer  systemd-private-fdf1de5804eb4744968fea5bf13e6734-systemd-timesyncd.service-6nds2r  vmware-root
lab702@elk:~$ rsync -azvh lab702@192.168.222.132:/tmp/test.csv.zip /tmp
lab702@192.168.222.132's password:
receiving incremental file list
test.csv.zip

sent 43 bytes  received 675.07M bytes  16.67M bytes/sec
total size is 674.68M  speedup is 1.00
lab702@elk:~$ ls /tmp/
dupl-usagereport-4684-20190214012833.json  HttpServer  systemd-private-fdf1de5804eb4744968fea5bf13e6734-systemd-timesyncd.service-6nds2r  test.csv.zip  vmware-root
  1. 無密碼 因為上述的做法需要輸入 server 端的密碼。以下是透過配置檔來達到認證不用輸入密碼,而進行傳輸。
Server 設置 ​
shell
[rsync_test]
path = /tmp # 存放備份資料的目錄
auth users =  lab702 # 認證帳號
uid = root # 用來啟動 rsync server 的 uid
gid = root # 用來啟動 rsync server 的 gid
read only = false # 是否設定為唯讀 
write_only = false # 是否設定為可寫
log file = /var/log/rsync.log
hosts allow = 192.168.222.0/24 # 允許使用 rsync連入的ip
#hosts deny = * # 不允許連入的 ip,* 表示全拒絕
secrets file = /etc/rsyncd.secrets # 認證檔存放路徑
dont compress = *.gz *.tgz *.zip *.z *.rpm *.deb *.iso *.bz2 *.tbz # 不對這些副檔名的檔案做壓縮

設定認證,Serve 認證 Client

shell
$ sudo vi /etc/rsyncd.secrets
account:password # 帳號:密碼

權限設定,認證檔案的權限設為 600,並將擁有者設為 root

shell
$ sudo chmod 600 /etc/rsyncd.secrets 
$ sudo chown root:root /etc/rsyncd.secrets

client 設定來源端的密碼認證檔,權限也如上

shell
$ sudo vi /etc/rsyncd.pwd # server 所設定的密碼

完成後,傳輸資料時則不必輸入 Server 端密碼

shell
$ sudo rsync -avzrpog --password-file=/etc/rsyncd.pwd a.txt lab702@192.168.222.132::rsync_test

ubuntu rsync to NAS ​

  • ubuntu 12
  • NAS

這部分是我因為有管理學校的小系統,這系統都有做定期備份,但學長們都是用將資料拉到 USB 做備份,因此我接手的時候想到用 rsync 備份至 NAS,這樣減少了定期拿 USB 手動拿資料的時間。 NAS 需要設定,但 NAS 部分我不是負責人。這裡就不描述。

將 ubuntu 要備份資料遠端至 NAS 上 ​
shell
rsync -av  --progress --remove-source-files  /home/misc/backup/NFS [NASAccount]@[NAS-Domain]::[資料夾名稱]
shell
$ sudo vi /etc/rsyncd.pwd
$ sudo chmod 600 /etc/rsyncd.pw
$ sudo chown root:root /etc/rsyncd.pwd
shell
$ sudo rsync -av  --progress --remove-source-files --password-file=/etc/rsyncd.pwd   a.txt  [NASAccount]@[NAS-Domain]::[資料夾名稱]

錯誤解決 ​

服務端設置 read only = false ​
shell
$ rsync -av  --progress --remove-source-files  test.csv.zip  [NASAccount]@[NAS-Domain]::[資料夾名稱]
Password:
sending incremental file list
test.csv.zip
         32,768   0%    0.00kB/s    0:00:00
rsync: read error: Connection reset by peer (104)
rsync error: error in socket IO (code 10) at io.c(785) [sender=3.1.1]
調整 NAS 給予的檔案大小限制 ​
shell
rsync: writefd_unbuffered failed to write 4 bytes to socket [sender]: Connection reset by peer (104)
rsync: connection unexpectedly closed (28 bytes received so far) [sender]
rsync error: error in rsync protocol data stream (code 12) at io.c(605) [sender=3.0.9]
硬碟磁區受損 ​

調整一下 NAS

shell
$ sudo rsync -av  --progress --remove-source-files /home/misc/backup/NFS [NASAccount]@[NAS-Domain]::[資料夾名稱]
[sudo] password for itachi:
Password:
sending incremental file list
NFS/jihmsp/
NFS/jihmsp/2018-10-20-05-00-02_jihmsp.tar.gz
  1330249728  78%   11.16MB/s    0:00:32
rsync: writefd_unbuffered failed to write 4 bytes to socket [sender]: Connection reset by peer (104)
rsync: write failed on "NFS/jihmsp/2018-10-20-05-00-02_jihmsp.tar.gz" (in AICT_BIT_Backup): Disk quota exceeded (122)
rsync error: Quota exceeded (code 54) at receiver.c(358) [receiver=3.0.9]
rsync: connection unexpectedly closed (399 bytes received so far) [sender]
rsync error: error in rsync protocol data stream (code 12) at io.c(605) [sender=3.0.9]

權限 600 和 gid、uid 問題 ​

權限 600,uid、gid 都是 root,這部分要看 Server 配置檔的設定

shell
ERROR: password file must not be other-accessible
rsync error: syntax or usage error (code 1) at authenticate.c(175) [sender=3.0.9]

參考資料 ​

How_to_back_up_Linux_computer_to_Synology_NAS

rsync commands

網管人

weithenn

Built with VitePress