rsync工具用於將文件和目錄從一個位置同步到另一個位置。同步的位置可以在本地服務器或遠程服務器上。 |
[root@localhost ~]# yum -y install rsync
要同步本地計算機中的兩個目錄,使用rsync -zvr
命令:
[root@localhost ~]# rsync -zvr /var/log/ /root/temp/ sending incremental file list btmp dnf.librepo.log ... sssd/sssd_implicit_files.log sssd/sssd_nss.log tuned/tuned.log sent 516,136 bytes received 605 bytes 1,033,482.00 bytes/sec total size is 5,451,242 speedup is 10.55
參數解釋:
- -z 啓用壓縮
- -v 輸出詳細信息
- -r 表示遞歸
查看一下/root/temp目錄,發現rsync在同步期間未保留時間戳。
rsync命令的-a
選項表示存檔模式。-a
選項遞歸同步、保留符號鏈接、保留權限、保留時間戳、保留所有者和組。
現在,執行以下命令,然後查看文件的時間:
[root@localhost ~]# rsync -azv /var/log/ /root/temp/ sending incremental file list ./ btmp dnf.librepo.log dnf.log dnf.rpm.log ... sssd/sssd_nss.log tuned/ tuned/tuned.log sent 516,231 bytes received 629 bytes 1,033,720.00 bytes/sec total size is 5,451,789 speedup is 10.55
如下所示,rsync在同步期間保留了時間戳。
rsync允許在本地和遠程系統之間同步文件/目錄,前提是本地和遠程系統都要安裝rsync纔行,否則會提示如下信息:
[root@localhost ~]# rsync -avz /root/temp/ [email protected]:/root/temp [email protected]'s password: sending incremental file list created directory /root/temp ./ btmp dnf.librepo.log dnf.log dnf.rpm.log ... sssd/sssd_nss.log tuned/ tuned/tuned.log sent 516,231 bytes received 662 bytes 206,757.20 bytes/sec total size is 5,451,789 speedup is 10.55
下面是在遠程系統裏面查看已同步的目錄:
上面可以看到同步時需要輸入密碼,有時候不希望將文件從本地服務器備份到遠程服務器時輸入密碼,可以在兩臺主機間設置免密要登錄。
要將文件從遠程系統同步到本地時,如下所示,在源中指定遠程路徑,在目標中指定本地路徑即可:
[root@localhost ~]# rsync -avz [email protected]:/root/temp /root/temp [email protected]'s password: receiving incremental file list temp/ temp/btmp temp/dnf.librepo.log temp/dnf.log ... temp/tuned/ temp/tuned/tuned.log sent 634 bytes received 516,247 bytes 206,752.40 bytes/sec total size is 5,451,789 speedup is 10.55
如果在目標位置修改了文件,我們可能不想用源位置的舊文件覆蓋該文件。使用-u
選項就可以做到這一點。在下面的示例中,在本地將test.txt文件修改了內容。它不會被遠程系統的test.txt文件所覆蓋:
# 查看一下遠程系統temp目錄下的test.txt文件大小 [root@localhost ~]# ssh [email protected] ls -l /root/temp [email protected]'s password: total 4 -rw-r--r--. 1 root root 7 Apr 7 2021 test.txt # 查看一下本機的temp目錄下的test.txt文件大小,本機的test.txt文件已修改,所以比遠程系統裏面的test.txt文件大 [root@localhost ~]# ll /root/temp/ total 4 -rw-r--r--. 1 root root 77 Apr 7 21:10 test.txt # 執行rsync -avzu同步一下 [root@localhost ~]# rsync -avzu [email protected]:/root/temp /root/ [email protected]'s password: receiving incremental file list sent 25 bytes received 76 bytes 40.40 bytes/sec total size is 7 speedup is 0.07
下面查看一下本機的/root/temp目錄裏面的test.txt是否被覆蓋:
發現並沒有被覆蓋。
使用--progress
選項顯示rsync執行的詳細進度,如下所示:
[root@localhost ~]# rsync -avz --progress /root/temp/ [email protected]:/root/temp
如果文件不在源中而是在目標中存在,則可能希望在rsync同步期間刪除目標上的文件。在這種情況下,請使用--delete
選項:
# 查看一下源目錄裏面的文件 [root@localhost ~]# ll /root/temp/ total 0 -rw-r--r--. 1 root root 0 Apr 7 21:46 name.csv # 查看一下目標目錄裏面的文件 [root@localhost ~]# ssh [email protected] ls -l /root/temp [email protected]'s password: total 944 drwxr-xr-x. 2 root root 6 Apr 7 2021 anaconda drwx------. 2 root root 6 Apr 7 2021 audit -rw-------. 1 root root 0 Apr 7 2021 btmp -rw-------. 1 root root 0 Apr 7 2021 btmp-20210406 drwxr-xr-x. 2 root root 6 Apr 7 2021 chrony -rw-------. 1 root root 8432 Apr 7 2021 cron -rw-------. 1 root root 12200 Apr 7 2021 cron-20210221 -rw-------. 1 root root 48130 Apr 7 2021 cron-20210228 -rw-------. 1 root root 3910 Apr 7 2021 cron-20210308 -rw-------. 1 root root 22455 Apr 7 2021 cron-20210406 -rw-------. 1 root root 383369 Apr 7 2021 dnf.librepo.log -rw-------. 1 root root 476949 Apr 7 2021 dnf.librepo.log-20210221 # rsync使用--delete選項刪除目標目錄中不包含源目錄的文件 [root@localhost ~]# rsync -avz --delete /root/temp [email protected]:/root [email protected]'s password: sending incremental file list deleting temp/chrony/ deleting temp/audit/ deleting temp/anaconda/ deleting temp/dnf.librepo.log-20210221 deleting temp/dnf.librepo.log deleting temp/cron-20210406 deleting temp/cron-20210308 deleting temp/cron-20210228 deleting temp/cron-20210221 deleting temp/cron deleting temp/btmp-20210406 deleting temp/btmp temp/ temp/name.csv sent 123 bytes received 281 bytes 161.60 bytes/sec total size is 0 speedup is 0.00
在查看一下目標目錄是否刪除:
rsync允許在進行同步時提供要包括和排除文件或目錄的模式。
[root@localhost ~]# rsync -avz --include 'P*' --exclude '*' [email protected]:/var/lib/rpm/ /root/temp/
在上面的示例中,它僅包括以'P'開頭的文件或目錄,並排除所有其他文件。
可以使用rsync --max-size
選項告訴rsync不要傳輸大於指定大小的文件。
[root@localhost ~]# rsync -avz --max-size='1M' [email protected]:/var/lib/rpm/ /root/temp/
--max-size=1M
使rsync僅傳輸小於或等於1M的文件。單位可以是K,M,G等。
還可以使用--min-size=
參數,指定傳輸最小文件的大小。
rsync工具用於將文件和目錄從一個位置同步到另一個位置。同步的位置可以在本地服務器或遠程服務器上。