rsync 服务

同步命令参数:

-v 详细模式输出,传输时的进度信息
-z 传输时进行压缩以提高传输效率
-a 递归模式保持所有文件属性。等同于-rtopPDl
-e 使用信道协议

服务端部署:

ip:192.168.199.235

1关闭防火墙和selinux

service iptables stop
setenforce 0

2检查rsync 安装没有

[root@zabbixceshi /]# rpm -qa |grep rsync
rsync-3.0.6-12.el6.x86_64

3创建配置文件(默认没有)

touch /etc/rsyncd.conf

uid = root
gid = root
use chroot = no
max connections = 0
pid file = /var/log/rsync/rsyncd.pid
lock file = /var/log/rsync/rsync.lock
log file = /var/log/rsync/rsyncd.log
strict modes =yes

[www]
path = /www/
ignore errors
read only = false
list = false
hosts allow = all
hosts deny = 172.25.0.0/24
auth users = backup
secrets file = /etc/rsyncd.passwd

4创建用户认证文件(虚拟用户名和密码)

echo “backup:123” >/etc/rsyncd.passwd

5给予600权限

chmod 600 /etc/rsyncd.passwd

6启动服务

rsync –daemon

7查看端口

[root@zabbixceshi /]# netstat -anpt |grep rsync
tcp        0      0 0.0.0.0:873                 0.0.0.0:*                   LISTEN      1302/rsync          
tcp        0      0 :::873                      :::*                        LISTEN      1302/rsync

8建立共享的目录

mkdir /www

 

客服端配置(所有同步操作在客户端执行)

1 创建密码文件

echo “123” >/etc/rsyncd.passwd

2 给予600 权限

chmod 600 /etc/rsyncd.passwd

测试 :

3 客户端服务端同步操作:

如果只同步目录里面内容/tmp/ 要加斜线,如果是目录不用加斜线/tmp 即可。

[root@rsync-c etc]# rsync -avz backup@192.168.199.235::www /tmp/ #注:加 --password-file=/etc/rsyncd.passwd 可不用输密码

过程信息:
Password: 
receiving incremental file list
./
aa
bb
cc.txt
sent 115 bytes  received 234 bytes  139.60 bytes/sec
total size is 0  speedup is 0.00

 

或者这样:

rsync -avz rsync://backup@192.168.199.235/www /tmp/ –password-file=/etc/rsyncd.passwd

或者:

[root@rsync-c tmp]# rsync -avz /tmp/ rsync://backup@192.168.199.235/www --password-file=/etc/rsyncd.passwd

使用ssh协议进行推送:

[root@rsync-c tmp]# rsync -avz -e 'ssh -p 22' /etc/hosts root@192.168.199.235:/tmp

过程信息:
The authenticity of host '192.168.199.235 (192.168.199.235)' can't be established.
RSA key fingerprint is 0b:49:b6:27:2b:98:e4:b1:ed:42:77:7b:07:9a:42:0b.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.199.235' (RSA) to the list of known hosts.
root@192.168.199.235's password: 
sending incremental file list
hosts
sent 124 bytes  received 31 bytes  20.67 bytes/sec
total size is 158  speedup is 1.02

 

同步删除操作:

[root@rsync-c ~]rsync -avz --delete -e 'ssh -p 22' /tmp/ root@192.168.23.252:/www/

执行结果: 服务端www目录和本地tmp目录文件完全一致!  #操作过程:删除了服务端www目录中对比本地tmp目录没有的文件,同步tmp中有的文件而服务端中www目录中没有的文件

 

关闭rsync 服务

[root@rsync-s ~]# ps -ef |grep rsync
root       1373      1  0 14:05 ?        00:00:00 rsync --daemon
root       1610   1330  0 15:01 pts/0    00:00:00 grep rsync

[root@rsync-s ~]# pkill  rsync

[root@rsync-s ~]# ps -ef |grep rsync
root       1613   1330  0 15:01 pts/0    00:00:00 grep rsync

 

启动rsync服务

[root@rsync-s ~]# rsync --daemon

[root@rsync-s ~]# ps -ef |grep rsync
root       1615      1  0 15:02 ?        00:00:00 rsync --daemon
root       1617   1330  0 15:02 pts/0    00:00:00 grep rsync

 

--exclude

参数 :排除文件

从客户端往服务端同步,排除客户端的文件

rsync -avz --exclude=aa  /tmp/ backup@192.168.199.235::www --password-file=/etc/rsyncd.passwd

排除多个文件
rsync -avz --exclude={aa,bb} /tmp/backup@192.168.199.235::www --password-file=/etc/rsyncd.passwd

或者
rsync -avz --exclude=aa –exclude=bb /tmp/  backup@192.168.199.235::www --password-file=/etc/rsyncd.passwd

 

排除多个不规律的文件

参数:--exclude-from=文件名

使用方法:把文件名写到一个文件中

[root@rsync-c tmp]# ll

总用量 4
-rw-r--r-- 1 root root  0 7月  11 2016 aa
-rw-r--r-- 1 root root  0 7月  11 2016 bb
-rw-r--r-- 1 root root  0 7月  11 2016 cc
-rw-r--r-- 1 root root  0 7月  11 2016 jin
-rw-r--r-- 1 root root 13 7月   8 16:11 li.log
-rw-r--r-- 1 root root  0 7月   8 16:09 xx

[root@rsync-c tmp]# cat li.log  注意:文件中每行后面不能有空格
aa
bb
jin
cc

[root@rsync-c tmp]# rsync -avz --exclude-from=li.log /tmp/ backup@192.168.199.235::www --password-file=/etc/rsyncd.passwd 

过程信息:
sending incremental file list
./
li.log
xx
sent 174 bytes  received 50 bytes  149.33 bytes/sec
total size is 13  speedup is 0.06

 

服务端配置文件排除参数:

在配置文件中加参数,重启服务。
Exclude=aa bb #使用空格隔开 

如果是目录中文件格式如下:
Exclude=aa bb test/test.txt
分享