[toc]ssh
10.28 rsync工具介紹工具
10.29/10.30 rsync經常使用選項測試
10.31 rsync經過ssh同步.net
Linux系統下有不少數據備份工具,經常使用的是rsync,從字面意思理解爲remote sync(遠程同步)。rsync不只能夠遠程同步數據(相似於scp),並且能夠本地同步數據(相似於cp),但不一樣於cp或者scp的一點是,它不會覆蓋之前的數據(若是數據已經存在),而是先判斷已經存在的數據和新數據的差別,只有數據不一樣時纔會把不相同的部分覆蓋。code
[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/.具體用法以下:ci
[root@localhost ~]# rsync -av /etc/passwd root@192.168.72.133:/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. root@192.168.72.133'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是一個功能很是強大的工具,其命令也有不少功能特點選項。rem
上述選項筆記多,經常使用的有-a,-v,-z,--delete和--exclude.get
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選項用法it
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
第一種方法:把文件推出去
在以前介紹的rsync的5種命令格式中,第二種和第三種(一個冒號)就屬於經過ssh的方式,備份數據,這種方式其實就是讓用戶登入到遠程機器,而後執行rsync的任務
[root@localhost ~]# rsync -av test2/ 192.168.72.133:/tmp/xavi/ root@192.168.72.133's password: sending incremental file list ./ 1 134.txt 2 3 321.txt 111/ 111/123.txt sent 356 bytes received 133 bytes 88.91 bytes/sec total size is 0 speedup is 0.00
上述就是前面介紹的第二種方式了,經過ssh複製的數據,須要輸入另外以太極的root帳戶的密碼。
固然也能夠用第三種方式複製,以下所示:
1.命令語法格式:
[root@localhost ~]# rsync -avP 192.168.72.133:/tmp/xavi/ /tmp/test1 root@192.168.72.133's password: receiving incremental file list created directory /tmp/test1 ./ 1 0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=6/8) 134.txt 0 100% 0.00kB/s 0:00:00 (xfer#2, to-check=5/8) 2 0 100% 0.00kB/s 0:00:00 (xfer#3, to-check=4/8) 3 0 100% 0.00kB/s 0:00:00 (xfer#4, to-check=3/8) 321.txt 0 100% 0.00kB/s 0:00:00 (xfer#5, to-check=2/8) 111/ 111/123.txt 0 100% 0.00kB/s 0:00:00 (xfer#6, to-check=0/8) sent 132 bytes received 361 bytes 89.64 bytes/sec total size is 0 speedup is 0.00 [root@localhost ~]# rsync -avP 192.168.72.133:/tmp/xavi/ /tmp/test1 root@192.168.72.133's password: receiving incremental file list created directory /tmp/test1 ./ 1 0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=6/8) 134.txt 0 100% 0.00kB/s 0:00:00 (xfer#2, to-check=5/8) 2 0 100% 0.00kB/s 0:00:00 (xfer#3, to-check=4/8) 3 0 100% 0.00kB/s 0:00:00 (xfer#4, to-check=3/8) 321.txt 0 100% 0.00kB/s 0:00:00 (xfer#5, to-check=2/8) 111/ 111/123.txt 0 100% 0.00kB/s 0:00:00 (xfer#6, to-check=0/8) sent 132 bytes received 361 bytes 89.64 bytes/sec total size is 0 speedup is 0.00
[root@localhost ~]# rsync -avP -e "ssh -p 22" /etc/passwd 192.168.72.133:/tmp/xavi.txt root@192.168.72.133's password: sending incremental file list passwd 2391 100% 0.00kB/s 0:00:00 (xfer#1, to-check=0/1) sent 2465 bytes received 31 bytes 713.14 bytes/sec total size is 2391 speedup is 0.96