新聞中心
rsync是一款實(shí)現(xiàn)遠(yuǎn)程同步功能的軟件,在同步文件的同時(shí),可以保持原來(lái)文件的權(quán)限,時(shí)間,軟硬鏈接等附加信息。Rsync是使用rsync算法提供一個(gè)客戶機(jī)和遠(yuǎn)程文件服務(wù)器的文件同步的快速算法,而且可以同步ssh的方式來(lái)傳輸,本篇文章將重點(diǎn)講解一下使用rsync備份數(shù)據(jù)具體方法。

10年專注成都網(wǎng)站制作,企業(yè)網(wǎng)站設(shè)計(jì),個(gè)人網(wǎng)站制作服務(wù),為大家分享網(wǎng)站制作知識(shí)、方案,網(wǎng)站設(shè)計(jì)流程、步驟,成功服務(wù)上千家企業(yè)。為您提供網(wǎng)站建設(shè),網(wǎng)站制作,網(wǎng)頁(yè)設(shè)計(jì)及定制高端網(wǎng)站建設(shè)服務(wù),專注于企業(yè)網(wǎng)站設(shè)計(jì),高端網(wǎng)頁(yè)制作,對(duì)服務(wù)器租用等多個(gè)方面,擁有豐富設(shè)計(jì)經(jīng)驗(yàn)。
在Centos中使用下面命令安裝rsync:
[root@localhost ~]# yum -y install rsync
實(shí)例一:本機(jī)中的兩個(gè)目錄進(jìn)行同步
要同步本地計(jì)算機(jī)中的兩個(gè)目錄,使用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
參數(shù)解釋:
-
-z 啟用壓縮
-
-v 輸出詳細(xì)信息
-
-r 表示遞歸
查看一下/root/temp目錄,發(fā)現(xiàn)rsync在同步期間未保留時(shí)間戳。
實(shí)例二:使用rsync -a在同步期間保留時(shí)間戳
rsync命令的-a選項(xiàng)表示存檔模式。-a選項(xiàng)遞歸同步、保留符號(hào)鏈接、保留權(quán)限、保留時(shí)間戳、保留所有者和組。
現(xiàn)在,執(zhí)行以下命令,然后查看文件的時(shí)間:
[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在同步期間保留了時(shí)間戳。
實(shí)例三:將文件從本地同步到遠(yuǎn)程目錄
rsync允許在本地和遠(yuǎn)程系統(tǒng)之間同步文件/目錄,前提是本地和遠(yuǎn)程系統(tǒng)都要安裝rsync才行,否則會(huì)提示如下信息:
[root@localhost ~]# rsync -avz /root/temp/ root@192.168.43.137:/root/temp
root@192.168.43.137'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
下面是在遠(yuǎn)程系統(tǒng)里面查看已同步的目錄: 上面可以看到同步時(shí)需要輸入密碼,有時(shí)候不希望將文件從本地服務(wù)器備份到遠(yuǎn)程服務(wù)器時(shí)輸入密碼,可以在兩臺(tái)主機(jī)間設(shè)置免密要登錄。
實(shí)例四:將文件從遠(yuǎn)程目錄同步到本地
要將文件從遠(yuǎn)程系統(tǒng)同步到本地時(shí),如下所示,在源中指定遠(yuǎn)程路徑,在目標(biāo)中指定本地路徑即可:
[root@localhost ~]# rsync -avz root@192.168.43.137:/root/temp /root/temp
root@192.168.43.137'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
Linux中rsync備份數(shù)據(jù)使用實(shí)例Linux中rsync備份數(shù)據(jù)使用實(shí)例
實(shí)例五:不要覆蓋目標(biāo)位置上已修改的文件
如果在目標(biāo)位置修改了文件,我們可能不想用源位置的舊文件覆蓋該文件。使用-u選項(xiàng)就可以做到這一點(diǎn)。在下面的示例中,在本地將test.txt文件修改了內(nèi)容。它不會(huì)被遠(yuǎn)程系統(tǒng)的test.txt文件所覆蓋:
# 查看一下遠(yuǎn)程系統(tǒng)temp目錄下的test.txt文件大小
[root@localhost ~]# ssh root@192.168.43.137 ls -l /root/temp
root@192.168.43.137's password: total 4 -rw-r--r--. 1 root root 7 Apr 7 2021 test.txt # 查看一下本機(jī)的temp目錄下的test.txt文件大小,本機(jī)的test.txt文件已修改,所以比遠(yuǎn)程系統(tǒng)里面的test.txt文件大 [root@localhost ~]# ll /root/temp/ total 4 -rw-r--r--. 1 root root 77 Apr 7 21:10 test.txt # 執(zhí)行rsync -avzu同步一下 [root@localhost ~]# rsync -avzu root@192.168.43.137:/root/temp /root/ root@192.168.43.137's password:
receiving incremental file list
sent 25 bytes received 76 bytes 40.40 bytes/sec
total size is 7 speedup is 0.07
下面查看一下本機(jī)的/root/temp目錄里面的test.txt是否被覆蓋: 發(fā)現(xiàn)并沒(méi)有被覆蓋。
實(shí)例六:在傳輸過(guò)程中查看rsync進(jìn)度
使用--progress選項(xiàng)顯示rsync執(zhí)行的詳細(xì)進(jìn)度,如下所示:
[root@localhost ~]# rsync -avz --progress /root/temp/ root@192.168.43.137:/root/temp
Linux中rsync備份數(shù)據(jù)使用實(shí)例Linux中rsync備份數(shù)據(jù)使用實(shí)例
實(shí)例七:在目標(biāo)目錄中刪除源目錄不存在的文件
如果文件不在源中而是在目標(biāo)中存在,則可能希望在rsync同步期間刪除目標(biāo)上的文件。在這種情況下,請(qǐng)使用--delete選項(xiàng):
# 查看一下源目錄里面的文件
[root@localhost ~]# ll /root/temp/
total 0
-rw-r--r--. 1 root root 0 Apr 7 21:46 name.csv
# 查看一下目標(biāo)目錄里面的文件
[root@localhost ~]# ssh root@192.168.43.137 ls -l /root/temp
root@192.168.43.137'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選項(xiàng)刪除目標(biāo)目錄中不包含源目錄的文件 [root@localhost ~]# rsync -avz --delete /root/temp root@192.168.43.137:/root root@192.168.43.137'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
在查看一下目標(biāo)目錄是否刪除:
實(shí)例八:文件傳輸過(guò)程中的include和exclude模式
rsync允許在進(jìn)行同步時(shí)提供要包括和排除文件或目錄的模式。
[root@localhost ~]# rsync -avz --include 'P*' --exclude '*' root@192.168.43.137:/var/lib/rpm/ /root/temp/
在上面的示例中,它僅包括以’P’開頭的文件或目錄,并排除所有其他文件。
實(shí)例九:不傳輸大文件
可以使用rsync --max-size選項(xiàng)告訴rsync不要傳輸大于指定大小的文件。
[root@localhost ~]# rsync -avz --max-size='1M' root@192.168.43.137:/var/lib/rpm/ /root/temp/
--max-size=1M使rsync僅傳輸小于或等于1M的文件。單位可以是K,M,G等。
還可以使用--min-size=參數(shù),指定傳輸最小文件的大小。
總結(jié)
rsync工具用于將文件和目錄從一個(gè)位置同步到另一個(gè)位置。同步的位置可以在本地服務(wù)器或遠(yuǎn)程服務(wù)器上。
網(wǎng)站欄目:使用rsync備份數(shù)據(jù)具體方法
鏈接分享:http://www.dlmjj.cn/article/cogecoh.html


咨詢
建站咨詢
