rsync同步工具实战

rsync具有增量同步的功能,相对于cp工具来说,效率比较高;同时可以在本地到本地或本地到远程之间,实现镜像备份

环境:分别有机器:server-178/24client-b-179/24client-c-180/24

其中以server-178/24rsync服务端,client-b-179/24client-c-180/24rsync客户端

实战过程:

检查服务端和客户端环境: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/179dirclient-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的守护进程,进行本地和远端的同步

服务端进行以下的配置:

rsync默认的配置文件:/etc/rsyncd.conf  #如果不存在,则手工建立

[root@server ~]# vi /etc/rsyncd.conf

添加一下内容:

#rsync_config-------------------start

#crated by crazy 20151030

#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

[root@client-B tmp]# chmod 600 /etc/rsync.password  #密码文件改成600这样比较安全

[root@client-B tmp]# chown root /etc/rsync.password 

 #上面一行:属主改成和配置文件相同的用户,如果是普通用户则改成普通的用户



client-c-180/24执行命令

[root@client-C tmp]# echo "123456" >/etc/rsync.password

[root@client-C tmp]# cat /etc/rsync.password

123456

[root@client-C tmp]# chmod 600 /etc/rsync.password  #密码文件改成600这样比较安全

[root@client-C tmp]# chown root /etc/rsync.password 

 #上面一行:属主改成和配置文件相同的用户,如果是普通用户则改成普通的用户



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]