可参考http://seanlook.com/2015/05/06/lsyncd-synchronize-realtime/
可参考http://openlinuxfly.blog.51cto.com/7120723/1679279
一、环境
lsyncd 192.168.3.71
rsync 192.168.3.72
二、配置rsync服务器
配置rsync以xinetd方式运行
[root@rsync ~]# yum install rsync -y [root@rsync ~]# yum install xinetd -y #修改/etc/xinetd.d/rsync [root@rsync ~]# 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 ~]# service xinetd start Starting xinetd: [ OK ] #rsync默认的监听端口是873,查看873号端口是否启动 [root@rsync ~]# netstat -tunlp Active Internet connections (only servers) 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 1247/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1324/master tcp 0 0 :::22 :::* LISTEN 1247/sshd tcp 0 0 ::1:25 :::* LISTEN 1324/master tcp 0 0 :::873 :::* LISTEN 1561/xinetd
创建rsync服务目录和配置文件
#创建rsync服务目录 [root@rsync ~]# mkdir /etc/rsyncd # 创建配置文件 [root@rsync ~]# touch /etc/rsyncd/rsyncd.conf # 创建密码文件 [root@rsync ~]# touch /etc/rsyncd/rsyncd.secrets #权限修改 [root@rsync ~]# chown root:root /etc/rsyncd/rsyncd.secrets [root@rsync ~]# chmod 600 /etc/rsyncd/rsyncd.secrets #这里的权限设置必须是600
创建用户和密码
[root@rsync ~]# echo "rsync:test" >>/etc/rsyncd/rsyncd.secrets
创建rsync配置文件
[root@rsync ~]# cat /etc/rsyncd/rsyncd.conf # GLOBAL OPTIONS uid = root gid = root use chroot = no read only = no #我们需要实时同步lsyncd服务器上的资源,这个需要有写权限,或者在模块中赋予写权限 #limit access to private LANs hosts allow=192.168.3.0/255.255.0.0 hosts deny=* max connections = 5 pid file = /var/run/rsyncd.pid secrets file = /etc/rsyncd/rsyncd.secrets #lock file = /var/run/rsync.lock motd file = /etc/rsyncd/rsyncd.motd #This will give you a separate log file log file = /var/log/rsync.log #This will log every file transferred - up to 85,000+ per user, per sync transfer logging = yes log format = %t %a %m %f %b syslog facility = local3 timeout = 300 # MODULE OPTIONS [test] path = /data/test list=yes ignore errors auth users = rsync #客户端连接过来使用的用户是rsync comment = welcome to rsync server
编辑xinetd的rsync配置文件,添加配置文件路径
#添加rsync的配置文件路径 [root@rsync ~]# vim /etc/xinetd.d/rsync service 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 } #重启xinetd服务 [root@rsync ~]# service xinetd restart Stopping xinetd: [ OK ] Starting xinetd: [ OK ] [root@rsync ~]# netstat -anpt |grep 873 tcp 0 0 :::873 :::* LISTEN 1586/xinetd #创建数据目录 [root@rsync ~]# mkdir -p /data/test
三、配置lsyncd服务器
#安装rsync,lsyncd [root@lsyncd ~]# rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm [root@lsyncd ~]# sed -i ‘s@#b@b@g‘ /etc/yum.repos.d/epel.repo [root@lsyncd ~]# sed -i ‘s@mirrorlist@#mirrorlist@g‘ /etc/yum.repos.d/epel.repo [root@lsyncd ~]# yum install rsync lsyncd -y
配置lsyncd服务配置文件
本地同步
1.1 本地目录同步:direct:cp/rm/mv。 适用:500+万文件,变动不大
[root@lsyncd ~]# vim /etc/lsyncd.conf settings { logfile = "/tmp/lsyncd.log", statusFile = "/tmp/lsyncd.status", statusInterval = 5, } sync { default.direct, source = "/data/test", #要同步的源目录 target = "/data/dest", #同步的目的目录 delay = 1, maxProcesses = 1, } #启动服务 [root@lsyncd ~]# /etc/init.d/lsyncd start Starting lsyncd: [ OK ] #测试,查看目录内容 [root@lsyncd ~]# ll /data/test total 0 -rw-r--r-- 1 root root 0 Jul 30 15:00 1.txt -rw-r--r-- 1 root root 0 Jul 30 15:01 test.txt [root@lsyncd ~]# ll /data/dest/ total 0 -rw-r--r-- 1 root root 0 Jul 30 15:00 1.txt -rw-r--r-- 1 root root 0 Jul 30 15:01 test.txt #创建一个新文件 [root@lsyncd ~]# touch /data/test/for.py #查看结果 [root@lsyncd ~]# ll /data/dest/ total 0 -rw-r--r-- 1 root root 0 Jul 30 15:00 1.txt -rw-r--r-- 1 root root 0 Jul 30 15:03 for.py -rw-r--r-- 1 root root 0 Jul 30 15:01 test.txt #删除/data/test目录下的txt文件 [root@lsyncd ~]# rm -rf /data/test/*.txt [root@lsyncd ~]# ll /data/dest/ total 0 -rw-r--r-- 1 root root 0 Jul 30 15:03 for.py
1.2 本地目录同步rsync模式:rsync
#编辑lsyncd配置文件添加如下内容 [root@lsyncd ~]# vim /etc/lsyncd.conf sync{ default.rsync, source = "/data/test", target = "/tmp/ceshi", #如果目录不存在,会自动创建目录 delete = true, exclude = { "test"}, rsync = { compress = true, verbose = true, archive = true, } } #重启服务 [root@lsyncd ~]# /etc/init.d/lsyncd restart Stopping lsyncd: [ OK ] Starting lsyncd: [ OK ] #测试 [root@lsyncd ~]# ll /tmp/ceshi/ total 0 -rw-r--r-- 1 root root 0 Jul 30 15:03 for.py #在/data/test下添加一个文件ceshi.txt [root@lsyncd ~]# touch /data/test/ceshi.txt [root@lsyncd ~]# ll /tmp/ceshi/ #有点延迟,可能是虚拟机的原因 total 0 -rw-r--r-- 1 root root 0 Jul 30 15:14 ceshi.txt -rw-r--r-- 1 root root 0 Jul 30 15:03 for.py
远程同步
2.1 远程同步: rsync模式 + rsyncd daemon
#创建密码文件 [root@lsyncd ~]# echo "test" > /etc/backserver.pas [root@lsyncd ~]# chmod 600 /etc/backserver.pas #修改lsyncd的配置文件,添加如下内容 [root@lsyncd ~]# vim /etc/lsyncd.conf sync{ default.rsync, source = "/data/test", target = "rsync@192.168.3.72::test", #这里的用户是192.168.3.72里设置的用户 delete = true, exclude = { ".tmp"}, delay = 1, rsync = { binary = "/usr/bin/rsync", archive = true, compress = true, verbose = true, password_file = "/etc/backserver.pas", #需要的密码配置文件 _extra = {"--bwlimit=200",} } } [root@lsyncd ~]# /etc/init.d/lsyncd restart Stopping lsyncd: [ OK ] Starting lsyncd: [ OK ] #测试,查看rsync:192.168.3.72上的test模块下的文件 [root@rsync ~]# hostname rsync [root@rsync ~]# ll /data/test/ total 0 -rw-r--r-- 1 root root 0 Jul 30 15:15 1.txt -rw-r--r-- 1 root root 0 Jul 30 15:14 ceshi.txt -rw-r--r-- 1 root root 0 Jul 30 15:03 for.py -rw-r--r-- 1 root root 0 Jul 30 15:23 test #在lsyncd上删除/data/test目录下的for.py [root@lsyncd ~]# hostname lsyncd [root@lsyncd ~]# rm -rf /data/test/for.py #查看结果 [root@rsync ~]# hostname rsync [root@rsync ~]# ll /data/test/ total 0 -rw-r--r-- 1 root root 0 Jul 30 15:15 1.txt -rw-r--r-- 1 root root 0 Jul 30 15:14 ceshi.txt -rw-r--r-- 1 root root 0 Jul 30 15:23 test #测试添加一个文件 [root@lsyncd ~]# hostname lsyncd [root@lsyncd ~]# touch /data/test/docker.txt #查看结果 [root@rsync ~]# ll /data/test/ total 0 -rw-r--r-- 1 root root 0 Jul 30 15:15 1.txt -rw-r--r-- 1 root root 0 Jul 30 15:14 ceshi.txt -rw-r--r-- 1 root root 0 Jul 30 15:31 docker.txt -rw-r--r-- 1 root root 0 Jul 30 15:23 test
本文出自 “ly36843运维” 博客,请务必保留此出处http://ly36843.blog.51cto.com/3120113/1680127
原文地址:http://ly36843.blog.51cto.com/3120113/1680127