一、环境
系统:CentOS6.4x64最小化安装
rsync-1:192.168.3.50
rsync-2:192.168.3.52
二、安装rsync
卸载原有的rsync软件包
[root@rsync-1 ~]# rpm -e `rpm -qa |grep rsync`
下载最新的rsync安装包,并安装
[root@rsync-1 ~]# wget http://pkgs.repoforge.org/rsync/rsync-3.1.1-1.el6.rfx.x86_64.rpm[root@rsync-1 ~]# rpm -ivh rsync-3.1.1-1.el6.rfx.x86_64.rpm warning: rsync-3.1.1-1.el6.rfx.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 6b8d79e6: NOKEYPreparing... ########################################### [100%] 1:rsync ########################################### [100%]
场景:从rsync-1直接推送文件到rsync-2的/root目录下
#查看rsync-2的/root目录下的文件[root@rsync-2 ~]# ll /root/total 56-rw-------. 1 root root 2790 May 4 13:49 anaconda-ks.cfg-rw-r--r--. 1 root root 3305 May 4 13:49 cobbler.ks-rw-r--r--. 1 root root 20504 May 4 13:49 install.log-rw-r--r--. 1 root root 5882 May 4 13:48 install.log.syslog-rw-r--r--. 1 root root 2241 May 4 13:49 ks-post.log-rw-r--r--. 1 root root 1 May 4 13:49 ks-post-nochroot.log-rw-r--r--. 1 root root 978 May 4 13:45 ks-pre.log#在rsync-1上直接推送文件到rsync-2上[root@rsync-1 ~]# rsync -avz rsync-3.1.1-1.el6.rfx.x86_64.rpm 192.168.3.52:/rootroot@192.168.3.52's password: sending incremental file listrsync-3.1.1-1.el6.rfx.x86_64.rpmsent 409,478 bytes received 35 bytes 117,003.71 bytes/sectotal size is 413,760 speedup is 1.01#在rsync-2上查看结果[root@rsync-2 ~]# ll /root/total 464-rw-------. 1 root root 2790 May 4 13:49 anaconda-ks.cfg-rw-r--r--. 1 root root 3305 May 4 13:49 cobbler.ks-rw-r--r--. 1 root root 20504 May 4 13:49 install.log-rw-r--r--. 1 root root 5882 May 4 13:48 install.log.syslog-rw-r--r--. 1 root root 2241 May 4 13:49 ks-post.log-rw-r--r--. 1 root root 1 May 4 13:49 ks-post-nochroot.log-rw-r--r--. 1 root root 978 May 4 13:45 ks-pre.log-rw-r--r-- 1 root root 413760 Jun 23 2014 rsync-3.1.1-1.el6.rfx.x86_64.rpm
三、配置rsync服务
配置rsync已xinetd方式运行
[root@rsync-1 ~]# yum install xinetd -y#修改/etc/xinetd.d/rsync [root@rsync-1 ~]# vim /etc/xinetd.d/rsync service rsync{ disable = no ##将yes改成no socket_type = stream wait = no user = root server = /usr/bin/rsync server_args = --daemon log_on_failure += USERID}#启动xinetd服务[root@rsync-1 ~]# service xinetd startStarting xinetd: [ OK ]#rsync默认的监听端口是873,查看873号端口是否启动[root@rsync-1 ~]# netstat -anptActive Internet connections (servers and established)Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1244/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1321/master tcp 0 52 192.168.3.50:22 192.168.3.2:17985 ESTABLISHED 1812/sshd tcp 0 0 :::22 :::* LISTEN 1244/sshd tcp 0 0 ::1:25 :::* LISTEN 1321/master tcp 0 0 :::873 :::* LISTEN 1957/xinetd
添加防火墙规则,放行873端口
[root@rsync-1 ~]# iptables -I INPUT -p tcp --dport 873 -j ACCEPT[root@rsync-1 ~]# iptables -I INPUT -p udp --dport 873 -j ACCEPT[root@rsync-1 ~]# service iptables saveiptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ][root@rsync-1 ~]# iptables -L -n |grep 873ACCEPT udp -- 0.0.0.0/0 0.0.0.0/0 udp dpt:873 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:873
创建rsync服务目录和配置文件
#创建rsync服务目录[root@rsync-1 ~]# mkdir /etc/rsyncd # 创建配置文件[root@rsync-1 ~]# touch /etc/rsyncd/rsyncd.conf# 创建密码文件[root@rsync-1 ~]# touch /etc/rsyncd/rsyncd.secrets#权限修改[root@rsync-1 ~]# chown root:root /etc/rsyncd/rsyncd.secrets[root@rsync-1 ~]# chmod 600 /etc/rsyncd/rsyncd.secrets #这里的权限设置必须是600
创建用户和密码
[root@rsync-1 ~]# echo "rsync:test" >>/etc/rsyncd/rsyncd.secrets
创建rsync配置文件
[root@rsync-1 ~]# vim /etc/rsyncd/rsyncd.conf# GLOBAL OPTIONSuid = rootgid = rootuse chroot = yes #同步软连接需要使用参数yesread only = yes#limit access to private LANshosts allow=192.168.3.0/255.255.0.0hosts deny=*max connections = 5pid file = /var/run/rsyncd.pidsecrets file = /etc/rsyncd/rsyncd.secrets#lock file = /var/run/rsync.lock motd file = /etc/rsyncd/rsyncd.motd #This will give you a separate log filelog file = /var/log/rsync.log #This will log every file transferred - up to 85,000+ per user, per synctransfer logging = yeslog format = %t %a %m %f %bsyslog facility = local3timeout = 300# MODULE OPTIONS[test]path = /data/testlist=yesignore errorsauth users = rsynccomment = welcome to rsync server
编辑xinetd的rsync配置文件,添加配置文件路径
#添加rsync的配置文件路径[root@rsync-1 ~]# vim /etc/xinetd.d/rsyncservice rsync{ disable = no socket_type = stream wait = no user = root server = /usr/bin/rsync server_args = --daemon --config=/etc/rsyncd/rsyncd.conf #添加配置文件路径 log_on_failure += USERID}[root@rsync-1 ~]# service xinetd restartStopping xinetd: [ OK ]Starting xinetd: [ OK ][root@rsync-1 ~]# netstat -anpt |grep 873tcp 0 0 :::873 :::* LISTEN 2045/xinetd
在客户端rsync-2上查看rsync-1提供了哪些数据服务
[root@rsync-2 ~]# rsync --list-only root@192.168.3.50::test welcome to rsync server#现已有一个名为test的数据服务#将test同步到本地的/root目录下下的test目录下,如果test目录不存在,就会自动创建test目录[root@rsync-2 ~]# rsync -avzP rsync@192.168.3.50::test /root/testPassword: receiving incremental file listcreated directory /root/test./1.txt 0 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=3/5)2.txt 0 100% 0.00kB/s 0:00:00 (xfr#2, to-chk=2/5)docker/docker/docker.txt 0 100% 0.00kB/s 0:00:00 (xfr#3, to-chk=0/5)sent 100 bytes received 281 bytes 108.86 bytes/sectotal size is 0 speedup is 0.00[root@rsync-2 ~]# ll -htotal 4.0Kdrwxr-xr-x 3 root root 4.0K May 4 15:22 test#以上同步过程需要输入密码#创建密码文件,并修改权限[root@rsync-2 ~]# echo "test" >>rsync.password[root@rsync-2 ~]# chmod 600 rsync.password #这里的权限设置必须是600#同步数据[root@rsync-2 ~]# ll /root/total 4-rw------- 1 root root 5 May 4 15:30 rsync.password[root@rsync-2 ~]# rsync -avzP --password-file=/root/rsync.password rsync@192.168.3.50::test /root/testreceiving incremental file listcreated directory /root/test./1.txt 0 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=3/5)2.txt 0 100% 0.00kB/s 0:00:00 (xfr#2, to-chk=2/5)docker/docker/docker.txt 0 100% 0.00kB/s 0:00:00 (xfr#3, to-chk=0/5)sent 96 bytes received 273 bytes 738.00 bytes/sectotal size is 0 speedup is 0.00#整个过程没有输入密码
假如在同步的时候我们需要忽略某些目录或者文件,该怎么做呢
#创建忽略文件,名称随便取,内容根据自己的需要填写,这里我们忽略/1.txt和/docker/docker.txt文件[root@rsync-2 ~]# cat file.txt /1.txtdocker/docker.txt[root@rsync-2 ~]# rsync -avzP --password-file=/root/rsync.password --exclude-from=/root/file.txt rsync@192.168.3.50::test /root/testreceiving incremental file listcreated directory /root/test./2.txt 0 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=1/3)docker/sent 93 bytes received 154 bytes 494.00 bytes/sectotal size is 0 speedup is 0.00#查看结果,忽略文件并没有同步过来[root@rsync-2 ~]# tree test/test/├── 2.txt└── docker1 directory, 1 file