Rsync多模塊複製、排除指定文件及目錄以及數據無差異複製的應用實例

在我的博客《Rsync 數據複製軟件應用》中,拉取數據訪問的都是服務器端的/backup 目錄,當然我們在其他目錄下拉取數據。而實現這種操作就是指多模塊複製。

要實現多模塊複製首先需要修改rsync的配置文件rsyncd.conf(要記得此處的配置文件在服務器端哦!)

[root@localhost ~]# vim /etc/rsyncd.conf
[root@localhost ~]# cat /etc/rsyncd.conf
#rsync_config____________________start
#created by wj root

uid=rsync
gid=rsync
fake super = yes
use chroot = no
max connections = 200
timeout = 600
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
log file = /var/run/rsyncd.log
ignore errors
read only = false
list = false
hosts allow = 192.168.146.0/24
hosts deny =0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync.password
[backup]
comment = welcome to backup!
path = /backup/
[data]       #加入的內容
path = /data/

建立並授權目錄

[root@localhost ~]# mkdir -p /data

[root@localhost ~]# chown -R rsync.rsync /data

[root@localhost ~]# ls -ld /data
drwxr-xr-x. 2 rsync rsync 6 5月 5 15:47 /data

重啟Rsync服務(只要修改過配置文件,就要重啟服務)
[root@localhost ~]# systemctl restart rsyncd

現在就可以從客戶端訪問測試了

[root@web1 ~]# rsync -avz /wjtest [email protected]::data
Password:
@ERROR: auth failed on module data
rsync error: error starting client-server protocol (code 5) at main.c(1649) [sender=3.1.2]       #出現這種情況是由於服務端的驗證賬戶密碼輸入錯誤導致的,一定要輸入正確的密碼

[root@web1 ~]# rsync -avz /wjtest [email protected]::data
Password:
sending incremental file list
wjtest/
wjtest/wj.txt

sent 123 bytes received 47 bytes 37.78 bytes/sec
total size is 0 speedup is 0.00
[root@web1 ~]#

登錄服務端驗證

[root@localhost ~]# ls /data
wjtest

排除指定目錄和文件的數據複製

注意首先我們需要在服務器端創建測試文件與目錄

[root@localhost backup]#mkdir {a..d}

[root@localhost backup]#touch a/1 b/2 c/3 d/4

[root@localhost backup]# tree /backup/
/backup/
├── a
│   └── 1
├── b
│   └── 2
├── c
│   └── 3
└── d
└── 4

4 directories, 4 files

我創建人四個目錄a、b、c、d,目錄下分別存放1、2、3、4,其中/backup目錄為Rsync服務器端指定的備份同步目錄

以在Rsync客戶端(192.168.146.110)上執行拉取文件操作,及從服務器端同步文件到客戶端

在同步數據過程中,假設要排除a、c目錄(包括下面的文件)及b目錄下的2文件

[root@web1 ~]# ls -l /mnt
總用量 0

[root@web1 ~]# rsync –exclude=a –exclude=b/2 –exclude=c -avzrtopgP \ [email protected]::backup/ /mnt –password-file=/etc/rsync.password     #注意這裡不需要空格,有空格會出錯的
@ERROR: auth failed on module backup
rsync error: error starting client-server protocol (code 5) at main.c(1649) [Receiver=3.1.2]
[root@web1 ~]# rsync –exclude=a –exclude=b/2 –exclude=c -avzrtopgP \[email protected]::backup/ /mnt –password-file=/etc/rsync.password
receiving incremental file list
./
b/
d/
d/4
0 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=0/4)

sent 81 bytes received 175 bytes 512.00 bytes/sec
total size is 0 speedup is 0.00
[root@web1 ~]#

 

在客戶端查看

[root@web1 ~]# tree /mnt
/mnt
├── b
└── d
└── 4

2 directories, 1 file

主機之間數據無差異複製

要實現這種同步方式就要使用Rsync的參數–delete,所謂無差異複製,就是不管是拉取還是推送,都要保持兩邊的數據完全一致。

本地推送式刪除如下

[root@web1 ~]# mkdir null
[root@web1 ~]# rsync -avzP –delete null/ /mnt/
sending incremental file list
deleting d/4
deleting d/
deleting b/
./

sent 43 bytes received 38 bytes 162.00 bytes/sec
total size is 0 speedup is 0.00

檢查/mnt中文件是否被刪除

[root@web1 ~]# tree /mnt
/mnt

0 directories, 0 files

拉取式數據無差異同步方式

[root@web1 ~]# rsync -avz –delete [email protected]::backup /wjtest/ –password-file=/etc/rsync.password
receiving incremental file list
deleting wj.txt
./
a/
a/1
b/
b/2
c/
c/3
d/
d/4

sent 123 bytes received 363 bytes 972.00 bytes/sec
total size is 0 speedup is 0.00

查看/wjtest

[root@web1 ~]# ls /wjtest
a b c d

登錄到服務端服務器查看/kackup

[root@localhost mnt]# ls /backup
a b c d

發現此刻,在服務端服務器/kackup與客戶端服務器 /wjtest目錄下的文件一致