rsync同步工具实战
rsync具有增量同步的功能,相对于cp工具来说,效率比较高;同时可以在本地到本地或本地到远程之间,实现镜像备份
环境:分别有机器:server-178/24,client-b-179/24,client-c-180/24
其中以server-178/24为rsync服务端,client-b-179/24,client-c-180/24为rsync客户端
实战过程:
检查服务端和客户端环境:rpm -aq|grep rsync
[[email protected] ~]# rpm -aq|grep rsync
rsync-2.6.8-3.1
在client-b-179/24的/tmp目录下创建179dir目录在179dir目录下创建一个文件179.txt
[[email protected] ~]# cd /tmp
[[email protected] tmp]# mkdir 179dir
[[email protected] tmp]# touch 179dir/179.txt
[[email protected] tmp]# tree 179dir
179dir
`-- 179.txt
0 directories, 1 file
在client-c-180/24的/tmp目录下创建180dir目录在180dir目录下创建一个文件180txt
[[email protected] ~]# cd /tmp
[[email protected] tmp]# mkdir 180dir
[[email protected] tmp]# touch 180dir/180.txt
[[email protected] tmp]# tree 180dir
180dir/
`-- 180.txt
0 directories, 1 file
在服务器新建一个普通用户crazy密码123456,在客户端上使用rsync命令利用ssh隧道,ssh指定端口5201,把客户端client-b-179/24的/tmp/179dir,client-c-180/24的/tmp/180dir,推送到服务端的/tmp目录下
服务器创建一个用户:
[[email protected] ~]# mkdir crazy
[[email protected] ~]# passwd crazy
Changing password for user crazy.
New UNIX password:
BAD PASSWORD: it is too simplistic/systematic
Retype new UNIX password:
passwd: all authentication tokens updated successfully.
把本地的增量推送到远端,在client-b-179/24执行命令:
rsync -avz -P /tmp/179dir -e ‘ssh -p 5201‘ [email protected]:/tmp
把本地的增量推送到远端,在client-c-180/24执行命令:
rsync -avz -P /tmp/180dir -e ‘ssh -p 5201‘ [email protected]:/tmp
以client-b-179/24为例子,如下:
[[email protected] tmp]# rsync -avz -P /tmp/179dir -e ‘ssh -p 5201‘ [email protected]:/tmp
The authenticity of host ‘192.168.1.178 (192.168.1.178)‘ can‘t be established.
RSA key fingerprint is 1d:8e:6d:4e:63:41:8f:19:c0:dd:7e:1d:c4:dd:9c:8d.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘192.168.1.178‘ (RSA) to the list of known hosts.
[email protected]‘s password:
building file list ...
2 files to consider
179dir/
179dir/179.txt
0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=0/2)
sent 113 bytes received 48 bytes 24.77 bytes/sec
total size is 0 speedup is 0.00
查看服务端的情况:
[[email protected] ~]# tree /tmp/
/tmp/
`-- serverdirB
`-- aa
1 directory, 1 file
[[email protected] ~]# tree /tmp/
/tmp/
|-- 179dir
| `-- 179.txt
|-- 180dir
| `-- 180.txt
`-- serverdirB
`-- aa
3 directories, 3 files
完全同步操作:
-----------------------------推送-----------------------------
把本地同步到远端,远端需要和本地的保持相同,在client-b-179/24执行命令:
rm -f 179dir/179.txt
rsync -avz -P --delete /tmp/179dir -e ‘ssh -p 5201‘ [email protected]:/tmp
把本地同步到远端,远端需要和本地的保持相同,在client-c-180/24执行命令:
rm -f 180dir/180.txt
rsync -avz -P --delete /tmp/180dir -e ‘ssh -p 5201‘ [email protected]:/tmp
以client-b-179/24为例子,如下:
[[email protected] tmp]# rm -f 179dir/179.txt #在本地先把179.txt文件删除
[[email protected] tmp]#rsync -avz -P --delete /tmp/179dir -e ‘ssh -p 5201‘ [email protected]:/tmp
[email protected]‘s password:
building file list ...
1 file to consider
deleting 179dir/179.txt #把远端的179.txt文件删除
179dir/
sent 59 bytes received 26 bytes 24.29 bytes/sec
total size is 0 speedup is 0.00
查看服务端的情况:
[[email protected] ~]# tree /tmp/
/tmp/
|-- 179dir
| `-- 179.txt
|-- 180dir
| `-- 180.txt
`-- serverdirB
`-- aa
3 directories, 3 files
[[email protected] ~]# tree /tmp/
/tmp/
|-- 179dir #客户端的179.txt文件删除了,使用--delete选项进行同步后,服务端这里跟着被删除了
|-- 180dir
`-- serverdirB
`-- aa
3 directories, 1 file
-----------------------------抓取-----------------------------
把远端同步到本地,本地的需要和远端的保持相同,在client-b-179/24执行命令:
rsync -avz -P --delete -e ‘ssh -p 5201‘ [email protected]:/tmp/179dir /tmp/
把远端同步到本地,本地的需要和远端的保持相同,在client-c-180/24执行命令:
rsync -avz -P --delete -e ‘ssh -p 5201‘ [email protected]:/tmp/180dir /tmp/
查看服务端的情况:
[[email protected] ~]# tree /tmp/
/tmp/
|-- 179dir
| `-- 179.log #服务器有179.log文件
|-- 180dir
| `-- 180.log #服务器有180.log文件
`-- serverdirB
`-- aa
3 directories, 3 files
以client-b-179/24为例子,如下:
[[email protected] tmp]# tree 179dir/
179dir/
`-- 179.log
0 directories, 1 file
[[email protected] tmp]# rm -f 179dir/179.log #把本地的179.log文件删除
[[email protected] tmp]# tree 179dir/
179dir/
0 directories, 0 files
[[email protected] tmp]# rsync -avz -P --delete -e ‘ssh -p 5201‘ [email protected]:/tmp/179dir /tmp/
[email protected]‘s password:
receiving file list ...
2 files to consider
179dir/
179dir/179.log
0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=0/2)
sent 48 bytes received 145 bytes 4.15 bytes/sec
total size is 0 speedup is 0.00
[[email protected] tmp]# tree 179dir/
179dir/
`-- 179.log #在服务端把179.log文件抓取回来
0 directories, 1 file
在服务端配置rsync的守护进程,进行本地和远端的同步
服务端进行以下的配置:
rsync默认的配置文件:/etc/rsyncd.conf #如果不存在,则手工建立
[[email protected] ~]# vi /etc/rsyncd.conf
添加一下内容:
#rsync_config__________start
#crated by oldboy 2006-6-5
#rsyncd.conf start##
uid = root
gid = root
use chroot = no
max connetctions = 200
timeout = 300
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyhcd.log
igonre errors
read only = false
list = false
hosts allow = 192.168.1.0/24 #测试改成172.16.1.0/24,发现客户端还能进行访问,这个问题还没找到答案
hosts deny = 0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync.password
[tmp]
path = /tmp/
#rsync_config__________end
[[email protected] ~]# rsync --daemon
[[email protected] ~]# ps -ef|grep rsync
root 4314 1 0 08:07 ? 00:00:00 rsync --daemon
root 4317 3654 0 08:08 pts/1 00:00:00 grep rsync
[[email protected] ~]# echo "rsync_backup:123456" >/etc/rsync.password
[[email protected] ~]# cat /etc/rsync.password
rsync_backup:123456
[[email protected] tmp]# pkill rsync #杀死进程
[[email protected] tmp]# rsync --daemon #启动守护进程
[[email protected] tmp]# ps -ef|grep rsync|grep -v grep
root 2844 1 0 19:13 ? 00:00:00 rsync --daemon
设置开机自启动:
[[email protected] tmp]# echo "#rsync daemon by crazy 20151031" >>/etc/rc.local
[[email protected] tmp]# echo "/usr/bin/rsync --daemon" >>/etc/rc.local
[[email protected] tmp]# tail -2 /etc/rc.local
#rsync daemon by crazy 20151031
/usr/bin/rsync --daemon
客户端进行以下的配置:
在client-b-179/24执行命令
[[email protected] tmp]# echo "123456" >/etc/rsync.password
[[email protected] tmp]# cat /etc/rsync.password
123456
在client-c-180/24执行命令
[[email protected] tmp]# echo "123456" >/etc/rsync.password
[[email protected] tmp]# cat /etc/rsync.password
123456
在client-b-179/24执行推送命令:
rsync -vza -P /tmp/179dir [email protected]::tmp/ --password-file=/etc/rsync.password
[[email protected] ~]#rsync -vza -P /tmp/179dir [email protected]::tmp/ --password-file=/etc/rsync.password
building file list ...
3 files to consider
179dir/
179dir/179.log
0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=1/3)
179dir/.ICE-unix/
sent 179 bytes received 50 bytes 152.67 bytes/sec
total size is 0 speedup is 0.00
服务端查看:
[[email protected] tmp]# tree
.
`-- serverdirB
`-- aa
1 directory, 1 file
[[email protected] tmp]# tree
.
|-- 179dir
| `-- 179.log
`-- serverdirB
`-- aa
2 directories, 2 files
在client-c-180/24执行推送命令:
rsync -vza -P /tmp/180dir [email protected]::tmp/ --password-file=/etc/rsync.password
[[email protected] ~]# rsync -vza -P /tmp/180dir [email protected]::tmp/ --password-file=/etc/rsync.password
building file list ...
2 files to consider
180dir/
180dir/180.log
0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=0/2)
sent 137 bytes received 44 bytes 362.00 bytes/sec
total size is 0 speedup is 0.00
服务端查看:
[[email protected] tmp]# tree
.
|-- 179dir
| `-- 179.log
`-- serverdirB
`-- aa
2 directories, 2 files
[[email protected] tmp]# tree
.
|-- 179dir
| `-- 179.log
|-- 180dir
| `-- 180.log
`-- serverdirB
`-- aa
3 directories, 3 files
模拟错误:
模拟客户端的密码文件权限的错误:
在client-b-179/24执行命令
[[email protected] ~]# ll /etc/rsync.password
-rw------- 1 root root 7 Oct 29 08:05 /etc/rsync.password #rsync.password 文件权限是600
[[email protected] ~]#rsync -vza -P /tmp/179dir [email protected]::tmp/ --password-file=/etc/rsync.password
building file list ...
3 files to consider
sent 128 bytes received 16 bytes 288.00 bytes/sec #成功连接
total size is 0 speedup is 0.00
[[email protected] ~]# chmod 644 /etc/rsync.password
[[email protected] ~]# ll /etc/rsync.password
-rw-r--r-- 1 root root 7 Oct 29 08:05 /etc/rsync.password #rsync.password 文件权限是644
[[email protected] ~]#rsync -vza -P /tmp/179dir [email protected]::tmp/ --password-file=/etc/rsync.password
password file must not be other-accessible #提示密码文件必须不能给其他人访问
continuing without password file #本地密码文件验证失败
Password: #提示输入密码
在服务端执行命令
[[email protected] tmp]# chmod 644 /etc/rsync.password
[[email protected] tmp]# ll /etc/rsync.password
-rw-r--r-- 1 root root 20 Oct 29 08:11 /etc/rsync.password
在client-c-180/24执行命令
[[email protected] ~]# ll /etc/rsync.password
-rw------- 1 root root 7 Oct 29 08:05 /etc/rsync.password
[[email protected] ~]# rsync -vza -P /tmp/180dir [email protected]::tmp/ --password-file=/etc/rsync.password
@ERROR: auth failed on module tmp #提示在模块tmp授权验证失败,由于在服务端修改了密码文件的权限导致
rsync error: error starting client-server protocol (code 5) at main.c(1296) [sender=2.6.8]