在我的博客《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 rsync_backup@192.168.146.100::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 rsync_backup@192.168.146.100::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 \ rsync_backup@192.168.146.100::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 \rsync_backup@192.168.146.100::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 rsync_backup@192.168.146.100::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目录下的文件一致
Rsync多模块复制、排除指定文件及目录以及数据无差异复制的应用实例
原文:https://www.cnblogs.com/luncy/p/14731854.html