默認未安裝,使用yum install -y rsyncpython
拷貝一個目錄的數據到另外一個目錄,拷貝的同時目錄的內容處在不斷更新中,在這裏使用cp命令就可能會有些問題:沒法實現增量拷貝,浪費時間;屢次全量拷貝,對磁盤的讀寫壓力大;bash
rsync命令就十分使用於這樣的狀況,能夠實現增量備份ssh
[root@localhost system]# rysnc -av /etc/passwd root@192.168.65.133:/tmp/1.txt -bash: rysnc: 未找到命令 [root@localhost system]# rsync -av /etc/passwd root@192.168.65.133:/tmp/1.txt The authenticity of host '192.168.65.133 (192.168.65.133)' can't be established. ECDSA key fingerprint is 42:50:a7:09:91:db:af:77:a5:3a:b3:67:1c:8a:5b:99. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.65.133' (ECDSA) to the list of known hosts. root@192.168.65.133's password: sending incremental file list passwd sent 1156 bytes received 31 bytes 67.83 bytes/sec total size is 1082 speedup is 0.91
# 指定遠程主機的格式能夠不指定用戶名,默認是指遠程主機上的當前用戶 [root@localhost ~]# rsync -av /etc/passwd root@192.168.65.130:/tmp/1.txt root@192.168.65.133's password: sending incremental file list sent 31 bytes received 12 bytes 3.19 bytes/sec total size is 1128 speedup is 26.23
[root@localhost ~]# rsync -av /root/111/ /tmp/111_dest/ sending incremental file list created directory /tmp/111_dest ./ file.py test -> /etc/passwd try.py 222/ sent 576 bytes received 60 bytes 1272.00 bytes/sec total size is 382 speedup is 0.60 [root@localhost ~]# ls -l 111/ 總用量 8 drwxr-xr-x. 2 root root 6 10月 27 19:49 222 -rw-r--r--. 1 root root 146 10月 27 19:45 file.py lrwxrwxrwx. 1 root root 11 10月 27 19:49 test -> /etc/passwd -rw-r--r--. 1 root root 225 10月 27 19:45 try.py [root@localhost ~]# ls -l /tmp/111_dest/ 總用量 8 drwxr-xr-x. 2 root root 6 10月 27 19:49 222 -rw-r--r--. 1 root root 146 10月 27 19:45 file.py lrwxrwxrwx. 1 root root 11 10月 27 19:49 test -> /etc/passwd -rw-r--r--. 1 root root 225 10月 27 19:45 try.py
[root@localhost ~]# rsync -avL /root/111/ /tmp/111_dest/ sending incremental file list test sent 1265 bytes received 32 bytes 2594.00 bytes/sec total size is 1499 speedup is 1.16 [root@localhost ~]# ls -l /tmp/111_dest/ 總用量 12 drwxr-xr-x. 2 root root 6 10月 27 19:49 222 -rw-r--r--. 1 root root 146 10月 27 19:45 file.py -rw-r--r--. 1 root root 1128 10月 23 20:22 test -rw-r--r--. 1 root root 225 10月 27 19:45 try.py [root@localhost ~]# ls -l 111/ 總用量 8 drwxr-xr-x. 2 root root 6 10月 27 19:49 222 -rw-r--r--. 1 root root 146 10月 27 19:45 file.py lrwxrwxrwx. 1 root root 11 10月 27 19:49 test -> /etc/passwd -rw-r--r--. 1 root root 225 10月 27 19:45 try.py
[root@localhost ~]# rsync --delete -avL /root/111/ /tmp/111_dest/ sending incremental file list ./ deleting 1.txt sent 97 bytes received 16 bytes 226.00 bytes/sec total size is 1499 speedup is 13.27
[root@localhost ~]# rsync -avL --exclude "*.py" /root/111/ /tmp/111_dest/ sending incremental file list created directory /tmp/111_dest ./ test 222/ sent 1244 bytes received 38 bytes 2564.00 bytes/sec total size is 1128 speedup is 0.88
[root@localhost ~]# rsync -aP /root/111/ /tmp/111_dest/ sending incremental file list created directory /tmp/111_dest ./ file.py 146 100% 0.00kB/s 0:00:00 (xfer#1, to-check=3/5) test -> /etc/passwd try.py 225 100% 219.73kB/s 0:00:00 (xfer#2, to-check=1/5) 222/ sent 576 bytes received 60 bytes 1272.00 bytes/sec total size is 382 speedup is 0.60
# 修改目標的文件內容,使目標的文件比源新 [root@localhost ~]# vi /tmp/111_dest/try.py # 使用-u參數,防止源中文件同步過來 [root@localhost ~]# rsync -avu /root/111/ /tmp/111_dest/ sending incremental file list ./ sent 113 bytes received 16 bytes 258.00 bytes/sec total size is 382 speedup is 2.96 # 驗證 # 目標文件添加一行註釋,較源文件更新 [root@localhost ~]# cat /tmp/111_dest/try.py #!/usr/bin/env python # test try: filename = raw_input('Enter file name: ') fobj = open(filename, 'r') for eachLine in fobj: print eachLine, fobj.close() except IOError as e: print "file open error:", e [root@localhost ~]# cat 111/try.py #!/usr/bin/env python try: filename = raw_input('Enter file name: ') fobj = open(filename, 'r') for eachLine in fobj: print eachLine, fobj.close() except IOError as e: print "file open error:", e
[root@localhost ~]# rsync -avPz /root/111/ /tmp/111_dest/ sending incremental file list try.py 225 100% 0.00kB/s 0:00:00 (xfer#1, to-check=1/5) sent 297 bytes received 32 bytes 658.00 bytes/sec total size is 382 speedup is 1.16
其餘的幾個參數(因爲-a默認包括了不少參數,因此幾乎使用-a替代了)工具
P、L等參數配合-a參數使用,會替代默認的p、l參數功能spa
前提:本機和遠程主機都須要安裝rsynccode
經過ssh訪問來同步文件/目錄的幾種寫法:ci
[root@localhost ~]# rsync -av /etc/passwd 192.168.65.130:/tmp/1.txt
[root@localhost ~]# rsync -av 192.168.65.130:/etc/passwd /tmp/1.txt
# 這裏假設遠端ssh端口爲66 [root@localhost ~]# rsync -av -e "ssh -p 66" /etc/passwd 192.168.133.130:/tmp/1.txt