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
[root@server ~]# rpm -aq|grep rsync
rsync-2.6.8-3.1
在client-b-179/24的/tmp目录下创建179dir目录在179dir目录下创建一个文件179.txt
[root@client-B ~]# cd /tmp
[root@client-B tmp]# mkdir 179dir
[root@client-B tmp]# touch 179dir/179.txt
[root@client-B tmp]# tree 179dir
179dir
`-- 179.txt
0 directories, 1 file
在client-c-180/24的/tmp目录下创建180dir目录在180dir目录下创建一个文件180txt
[root@client-C ~]# cd /tmp
[root@client-C tmp]# mkdir 180dir
[root@client-C tmp]# touch 180dir/180.txt
[root@client-C 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目录下
服务器创建一个用户:
[root@server ~]# mkdir crazy
[root@server ~]# 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‘ crazy@192.168.1.178:/tmp
把本地的增量推送到远端,在client-c-180/24执行命令:
rsync -avz -P /tmp/180dir -e ‘ssh -p 5201‘ crazy@192.168.1.178:/tmp
以client-b-179/24为例子,如下:
[root@client-B tmp]# rsync -avz -P /tmp/179dir -e ‘ssh -p 5201‘ crazy@192.168.1.178:/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.
crazy@192.168.1.178‘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
查看服务端的情况:
[root@server ~]# tree /tmp/
/tmp/
`-- serverdirB
`-- aa
1 directory, 1 file
[root@server ~]# 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‘ crazy@192.168.1.178:/tmp
把本地同步到远端,远端需要和本地的保持相同,在client-c-180/24执行命令:
rm -f 180dir/180.txt
rsync -avz -P --delete /tmp/180dir -e ‘ssh -p 5201‘ crazy@192.168.1.178:/tmp
以client-b-179/24为例子,如下:
[root@client-B tmp]# rm -f 179dir/179.txt #在本地先把179.txt文件删除
[root@client-B tmp]#rsync -avz -P --delete /tmp/179dir -e ‘ssh -p 5201‘ crazy@192.168.1.178:/tmp
crazy@192.168.1.178‘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
查看服务端的情况:
[root@server ~]# tree /tmp/
/tmp/
|-- 179dir
| `-- 179.txt
|-- 180dir
| `-- 180.txt
`-- serverdirB
`-- aa
3 directories, 3 files
[root@server ~]# 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‘ crazy@192.168.1.178:/tmp/179dir /tmp/
把远端同步到本地,本地的需要和远端的保持相同,在client-c-180/24执行命令:
rsync -avz -P --delete -e ‘ssh -p 5201‘ crazy@192.168.1.178:/tmp/180dir /tmp/
查看服务端的情况:
[root@server ~]# tree /tmp/
/tmp/
|-- 179dir
| `-- 179.log #服务器有179.log文件
|-- 180dir
| `-- 180.log #服务器有180.log文件
`-- serverdirB
`-- aa
3 directories, 3 files
以client-b-179/24为例子,如下:
[root@client-B tmp]# tree 179dir/
179dir/
`-- 179.log
0 directories, 1 file
[root@client-B tmp]# rm -f 179dir/179.log #把本地的179.log文件删除
[root@client-B tmp]# tree 179dir/
179dir/
0 directories, 0 files
[root@client-B tmp]# rsync -avz -P --delete -e ‘ssh -p 5201‘ crazy@192.168.1.178:/tmp/179dir /tmp/
crazy@192.168.1.178‘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
[root@client-B tmp]# tree 179dir/
179dir/
`-- 179.log #在服务端把179.log文件抓取回来
0 directories, 1 file
服务端进行以下的配置:
rsync默认的配置文件:/etc/rsyncd.conf #如果不存在,则手工建立
[root@server ~]# 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
[root@server ~]# rsync --daemon
[root@server ~]# 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
[root@server ~]# echo "rsync_backup:123456" >/etc/rsync.password
[root@server ~]# cat /etc/rsync.password
rsync_backup:123456
[root@server tmp]# pkill rsync #杀死进程
[root@server tmp]# rsync --daemon #启动守护进程
[root@server tmp]# ps -ef|grep rsync|grep -v grep
root 2844 1 0 19:13 ? 00:00:00 rsync --daemon
设置开机自启动:
[root@server tmp]# echo "#rsync daemon by crazy 20151031" >>/etc/rc.local
[root@server tmp]# echo "/usr/bin/rsync --daemon" >>/etc/rc.local
[root@server tmp]# tail -2 /etc/rc.local
#rsync daemon by crazy 20151031
/usr/bin/rsync --daemon
客户端进行以下的配置:
在client-b-179/24执行命令
[root@client-B tmp]# echo "123456" >/etc/rsync.password
[root@client-B tmp]# cat /etc/rsync.password
123456
在client-c-180/24执行命令
[root@client-C tmp]# echo "123456" >/etc/rsync.password
[root@client-C tmp]# cat /etc/rsync.password
123456
在client-b-179/24执行推送命令:
rsync -vza -P /tmp/179dir rsync_backup@192.168.1.178::tmp/ --password-file=/etc/rsync.password
[root@client-B ~]#rsync -vza -P /tmp/179dir rsync_backup@192.168.1.178::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
服务端查看:
[root@server tmp]# tree
.
`-- serverdirB
`-- aa
1 directory, 1 file
[root@server tmp]# tree
.
|-- 179dir
| `-- 179.log
`-- serverdirB
`-- aa
2 directories, 2 files
在client-c-180/24执行推送命令:
rsync -vza -P /tmp/180dir rsync_backup@192.168.1.178::tmp/ --password-file=/etc/rsync.password
[root@client-C ~]# rsync -vza -P /tmp/180dir rsync_backup@192.168.1.178::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
服务端查看:
[root@server tmp]# tree
.
|-- 179dir
| `-- 179.log
`-- serverdirB
`-- aa
2 directories, 2 files
[root@server tmp]# tree
.
|-- 179dir
| `-- 179.log
|-- 180dir
| `-- 180.log
`-- serverdirB
`-- aa
3 directories, 3 files
模拟客户端的密码文件权限的错误:
在client-b-179/24执行命令
[root@client-B ~]# ll /etc/rsync.password
-rw------- 1 root root 7 Oct 29 08:05 /etc/rsync.password #rsync.password 文件权限是600
[root@client-B ~]#rsync -vza -P /tmp/179dir rsync_backup@192.168.1.178::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
[root@client-B ~]# chmod 644 /etc/rsync.password
[root@client-B ~]# ll /etc/rsync.password
-rw-r--r-- 1 root root 7 Oct 29 08:05 /etc/rsync.password #rsync.password 文件权限是644
[root@client-B ~]#rsync -vza -P /tmp/179dir rsync_backup@192.168.1.178::tmp/ --password-file=/etc/rsync.password
password file must not be other-accessible #提示密码文件必须不能给其他人访问
continuing without password file #本地密码文件验证失败
Password: #提示输入密码
在服务端执行命令
[root@server tmp]# chmod 644 /etc/rsync.password
[root@server tmp]# ll /etc/rsync.password
-rw-r--r-- 1 root root 20 Oct 29 08:11 /etc/rsync.password
在client-c-180/24执行命令
[root@client-C ~]# ll /etc/rsync.password
-rw------- 1 root root 7 Oct 29 08:05 /etc/rsync.password
[root@client-C ~]# rsync -vza -P /tmp/180dir rsync_backup@192.168.1.178::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]
本文出自 “疯狂的日子” 博客,请务必保留此出处http://crazyday.blog.51cto.com/9974109/1708395
原文地址:http://crazyday.blog.51cto.com/9974109/1708395