31.Linux下的數據備份工具rsync
阿新 • • 發佈:2018-02-01
傳輸 authent 沒有 tmp 包含 passwd 推出 過濾 send 八周二次課(1月30日)
10.28 rsync工具介紹
10.29/10.30 rsync常用選項
10.31 rsync通過ssh同步
一、rsync工具介紹
Linux系統下有很多數據備份工具,常用的是rsync,從字面意思理解為remote sync(遠程同步)。rsync不僅可以遠程同步數據(類似於scp),而且可以本地同步數據(類似於cp),但不同於cp或者scp的一點是,它不會覆蓋以前的數據(如果數據已經存在),而是先判斷已經存在的數據和新數據的差異,只有數據不同時才會把不相同的部分覆蓋。
- 如果沒有rsync命令,請使用yum install -y rsync安裝.
舉例:
[root@localhost ~]# rsync -av /etc/passwd /tmp/1.txt sending incremental file list passwd sent 2465 bytes received 31 bytes 4992.00 bytes/sec total size is 2391 speedup is 0.96
上例中把/etc/passwd同步到/tmp/目錄下,並改名為1.txt,如果要改成遠程復制,數據備份的格式是: 用戶名@IP:path,比如192.168.188.128:/root/.具體用法如下:
[root@localhost ~]# rsync -av /etc/passwd [email protected]:/tmp/1.txt The authenticity of host ‘192.168.72.133 (192.168.72.133)‘ can‘t be established. ECDSA key fingerprint is 54:1a:44:33:2e:df:c2:58:41:cf:f3:d2:e3:69:87:b7. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added ‘192.168.72.133‘ (ECDSA) to the list of known hosts. [email protected]‘s password: sending incremental file list passwd sent 2465 bytes received 253 bytes 175.35 bytes/sec total size is 2391 speedup is 0.88
-
這裏要求遠程的機器也必須安裝有rsync,需要驗證密碼是因為這裏沒有做兩機互聯.
rsync是一個功能非常強大的工具,其命令也有很多功能特色選項。
1.命令語法格式:
- [ ] rsync [OPTION]... SRC DEST
- [ ] rsync [OPTION]... SRC [USER@]host:DEST // 第一例不加user@host,默認的就是root
- [ ] rsync [OPTION]... [USER@]HOST:SRC DEST//從遠程目錄同步數據到本地
- [ ] rsync [OPTION]... [USER@]HOST::SRC DEST
- [ ] rsync [OPTION]... SRC [USER@]HOST::DEST
2.rsync常用選項
- [ ] -a :包含-rtplgoD,a選項後面可以跟--no-OPTION這個表示關閉-rlptgoD中的某一個例如 -a--no-l 等同於-rptgoD
- [ ] -r :同步目錄時要加上,類似cp時的-r選項
- [ ] -v :同步時顯示-些信息,讓我們知道同步的過程
- [ ] -l :保留軟連接
- [ ] -L :加上該選項後,同步軟連接時會把源文件給同步
- [ ] -p :保持文件的權限屬性
- [ ] -o :保持文件的屬主
- [ ] -g :保持文件的屬組
- [ ] -D :保持設備文件信息
- [ ] -t :保持文件的時間屬性
- [ ] --delete :刪除DEST中SRC沒有的文件
- [ ] --exclude :過濾指定文件,如--exclude “logs”會把文件名含logs的文件或者目錄過濾掉,不同步
- [ ] -P :顯示同步過程,比如速率,比-v更加詳細
- [ ] -u :加上該選項後,如果DEST中的文件比SRC新,則不同步
- [ ] -z :傳輸時壓縮
上述選項筆記多,常用的有-a,-v,-z,--delete和--exclude.
3.測試rsync的選項
為了更換的測試,需要新建目錄和文件
[root@localhost ~]# ls
anaconda-ks.cfg grep httpd-2.4.29.tar.gz [root@localhost split_dir
awk httpd-2.4.29 initial-setup-ks.cfg sed xaa
## ls 下沒有合適的目錄和文件,只能重新建
[root@localhost ~]# mkdir rsync
[root@localhost ~]# cd rsync
[root@localhost rsync]# mkdir 111
[root@localhost rsync]# touch 1 2 3 /root/123.txt
[root@localhost rsync]# ln -s /root/123.txt ./134.txt //建立軟鏈接
[root@localhost rsync]# ls -l
總用量 0
-rw-r--r-- 1 root root 0 1月 31 23:22 1
3.1 -a選項用法
3.2 使用-L的用法
[root@localhost ~]# rsync -avL ./rsync/ ./test2/
sending incremental file list
111/
111/123.txt
sent 158 bytes received 35 bytes 386.00 bytes/sec
total size is 0 speedup is 0.00
[root@localhost ~]# ls -l test2/
總用量 0
-rw-r--r-- 1 root root 0 1月 31 23:22 1
drwxr-xr-x 2 root root 21 1月 31 23:50 111
-rw-r--r-- 1 root root 0 1月 31 23:22 134.txt
-rw-r--r-- 1 root root 0 1月 31 23:22 2
-rw-r--r-- 1 root root 0 1月 31 23:22 3
-rw-r--r-- 1 root root 0 1月 31 23:22 321.txt
3.3 使用delete選項
[root@localhost ~]# rsync -avL --delete ./rsync/ ./test2/
sending incremental file list
sent 116 bytes received 13 bytes 258.00 bytes/sec
total size is 0 speedup is 0.00
[root@localhost ~]# ls
123.txt 556.txt awk httpd-2.4.29 initial-setup-ks.cfg rsync split_dir xaa
321.txt anaconda-ks.cfg grep httpd-2.4.29.tar.gz [root@localhost sed test2
3.4 使用--exclude選項
[root@localhost ~]# mkdir test1
[root@localhost ~]# touch test1/4
[root@localhost ~]# rsync -a --exclude="4" test1/ test2/
[root@localhost ~]# ls test1
4
三、ssh方式同步
第一種方法:把文件推出去
[root@localhost ~]# rsync -avL test2/ 192.168.72.133:/root/xavi/
[email protected]‘s password:
sending incremental file list
./
1
134.txt
2
3
321.txt
111/
111/123.txt
sent 356 bytes received 133 bytes 75.23 bytes/sec
total size is 0 speedup is 0.00
第二章方法:把文件拉過來,測試失敗!!!!未找到原因
[root@localhost ~]# rsync -avP 192.168.72.132:/root/xavi/ ./test1/
ssh: connect to host 192.168.72.132 port 22: No route to host
rsync: connection unexpectedly closed (0 bytes received so far) [Receiver]
rsync error: unexplained error (code 255) at io.c(605) [Receiver=3.0.9]
[root@localhost ~]# rsync -avP 192.168.72.132:/root/xavi/ ./test1/
ssh: connect to host 192.168.72.132 port 22: No route to host
rsync: connection unexpectedly closed (0 bytes received so far) [Receiver]
rsync error: unexplained error (code 255) at io.c(605) [Receiver=3.0.9]
31.Linux下的數據備份工具rsync