rsync ― a fast, versatile, remote (and local) file-copying tool
rsync是一个快速,可靠得远程(和本地)文件拷贝工具,有点像cp命令,但是!cp是全量拷贝,而rsync是增量拷贝。
三种同步方式:
1、Local: rsync [OPTION...] SRC... [DEST
2、远程通道,例如:ssh
Push:
rsync -avz data_192.168.234.131 -e 'ssh' [email protected]:~
Pull:
rsync -avz -e 'ssh' 192.168.234.131:~/data_192.168.234.131 /home/yishine/
3、Access via rsync daemon
服务端 192.168.234.130 A
客户端 192.168.234.131 B
192.168.234.132 C
1、编辑配置文件
#vi /etc/rsyncd.conf
uid = rsync
gid = rsync
use chroot = no
max connections = 200
timeout = 300
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[oldboy]
path = /oldboy/
ignore errors
read only = false
list = false
hosts allow = 192.168.234.0/24
hosts deny = 0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync.password
2、创建共享目录
path = /data1/
3、创建rsync用户,并授权访问共享目录
auth users = rsync_backup
#useradd rsync_backup -s /sbin/nologin
#chown rsync_backup:rsync_backup /data1
4、创建密码文件并修改密码文件权限!(不改权限就要报错)
secrets file = /etc/rsync.password
#echo "rsync_backup:mirror-0" > /etc/rsync.password
#chmod 600 /etc/rsync.password
5、启动rsync服务并加入开机启动
#rsync --daemon
#echo "rsync --daemon" >> /etc/rc.local
6、查看rsync log
log file = /var/log/rsyncd.log
rsync client(多个)
1、创建密码文件(和服务器密码文件没有关系)
#echo "mirror-0" > /etc/rsync.password(内容就是rsync_backup用户的密码)
--password-file=/etc/rsync.password
2、修改密码文件权限!
#chmod 600 /etc/rsync.password
3、同步(主要是客户端→服务器,也就是备份。数据恢复反之)
Pull: rsync [OPTION...] [USER@]HOST::SRC... [DEST]
# rsync -avz [email protected]::data1 /data --password-file=/etc/rsync.password
rsync [OPTION...] rsync://[USER@]HOST[:PORT]/SRC... [DEST]
# rsync -azv rsync://[email protected]/data1 /data --password-file=/etc/rsync.password
Push: rsync [OPTION...] SRC... [USER@]HOST::DEST
# rsync -avz /data/ [email protected]::data1 --password-file=/etc/rsync.password
rsync [OPTION...] SRC... rsync://[USER@]HOST[:PORT]/DEST
# rsync -avz /data0/ rsync://[email protected]/data1 --password-file=/etc/rsync.password
其他的参数测试
rsync客户端排除多个文件--exclude={1.log,10.log}
# rsync -avz --exclude={1.log,10.log} [email protected]:data1 /data --password-file=/etc/rsync.password
无差异同步(慎用!)
# rsync -avz --delete /data0/ [email protected]::data1 --password-file=/etc/rsync.password
下面是一个测试,通过测试我们可以看出,--delete参数到底如何同步数据
结论先说了:加上--delete参数之后,服务器端数据以最后一次同步服务器的客户端数据作为基准
1、在B,C客户端上把数据同步到A服务器上
B#touch /data/{a..f}.log
# rsync -avz /data/ [email protected]::data1 --password-file=/etc/rsync.password
C#touch /data/{9..16}.log
# rsync -avz /data/ [email protected]0::data1 --password-file=/etc/rsync.password
2、查看模块定义的目录
A# ll /data1/
total 0
-rw-r--r-- 1 rsync_backup rsync_backup 0 May 19 22:30 10.log
-rw-r--r-- 1 rsync_backup rsync_backup 0 May 19 22:30 11.log
-rw-r--r-- 1 rsync_backup rsync_backup 0 May 19 22:30 12.log
-rw-r--r-- 1 rsync_backup rsync_backup 0 May 19 22:30 13.log
-rw-r--r-- 1 rsync_backup rsync_backup 0 May 19 22:30 14.log
-rw-r--r-- 1 rsync_backup rsync_backup 0 May 19 22:30 15.log
-rw-r--r-- 1 rsync_backup rsync_backup 0 May 19 22:30 16.log
-rw-r--r-- 1 rsync_backup rsync_backup 0 May 19 22:30 9.log
-rw-r--r-- 1 rsync_backup rsync_backup 0 May 19 21:48 a.log
-rw-r--r-- 1 rsync_backup rsync_backup 0 May 19 21:48 b.log
-rw-r--r-- 1 rsync_backup rsync_backup 0 May 19 21:48 c.log
-rw-r--r-- 1 rsync_backup rsync_backup 0 May 19 21:48 d.log
-rw-r--r-- 1 rsync_backup rsync_backup 0 May 19 21:48 e.log
-rw-r--r-- 1 rsync_backup rsync_backup 0 May 19 21:48 f.log
3、在客户端B上加上--delte参数同步一次
B# rsync -avz --delete /data/ [email protected]::data1 --password-file=/etc/rsync.password
sending incremental file list
deleting f.log
deleting e.log
deleting d.log
deleting c.log
deleting b.log
deleting a.log
4、服务器A上查看目录内容
A# ll /data1/
total 0
-rw-r--r-- 1 rsync_backup rsync_backup 0 May 19 22:30 10.log
-rw-r--r-- 1 rsync_backup rsync_backup 0 May 19 22:30 11.log
-rw-r--r-- 1 rsync_backup rsync_backup 0 May 19 22:30 12.log
-rw-r--r-- 1 rsync_backup rsync_backup 0 May 19 22:30 13.log
-rw-r--r-- 1 rsync_backup rsync_backup 0 May 19 22:30 14.log
-rw-r--r-- 1 rsync_backup rsync_backup 0 May 19 22:30 15.log
-rw-r--r-- 1 rsync_backup rsync_backup 0 May 19 22:30 16.log
-rw-r--r-- 1 rsync_backup rsync_backup 0 May 19 22:30 9.log
5、在客户端C上加上--delte参数同步一次
C# rsync -avz --delete /data/ [email protected]::data1
Password:
sending incremental file list
./
deleting 9.log
deleting 16.log
deleting 15.log
deleting 14.log
deleting 13.log
deleting 12.log
deleting 11.log
deleting 10.log
a.log
b.log
c.log
d.log
e.log
f.log
6、在服务器A上查看目录内容
A# ll /data/
total 0
-rw-r--r-- 1 rsync_backup rsync_backup 0 May 19 21:48 a.log
-rw-r--r-- 1 rsync_backup rsync_backup 0 May 19 21:48 b.log
-rw-r--r-- 1 rsync_backup rsync_backup 0 May 19 21:48 c.log
-rw-r--r-- 1 rsync_backup rsync_backup 0 May 19 21:48 d.log
-rw-r--r-- 1 rsync_backup rsync_backup 0 May 19 21:48 e.log
-rw-r--r-- 1 rsync_backup rsync_backup 0 May 19 21:48 f.log
附上排错:
rsync: failed to connect to 192.168.234.131: Connection refused (111)
该问题是因为服务器端的rsync服务没有起来导致,检查rsync服务
# ps -ef |grep rsync
@ERROR: auth failed on module data1
在模块中修改了密码,没有重启服务
# lsof -i:873
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
rsync 1330 root 4u IPv4 11794 0t0 TCP *:rsync (LISTEN)
rsync 1330 root 5u IPv6 11795 0t0 TCP *:rsync (LISTEN)
# kill 1330