可参考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方式运行
[[email protected] ~]# yum install rsync -y [[email protected] ~]# yum install xinetd -y #修改/etc/xinetd.d/rsync [[email protected] ~]# 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服务 [[email protected] ~]# service xinetd start Starting xinetd: [ OK ] #rsync默认的监听端口是873,查看873号端口是否启动 [[email protected] ~]# 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服务目录 [[email protected] ~]# mkdir /etc/rsyncd # 创建配置文件 [[email protected] ~]# touch /etc/rsyncd/rsyncd.conf # 创建密码文件 [[email protected] ~]# touch /etc/rsyncd/rsyncd.secrets #权限修改 [[email protected] ~]# chown root:root /etc/rsyncd/rsyncd.secrets [[email protected] ~]# chmod 600 /etc/rsyncd/rsyncd.secrets #这里的权限设置必须是600
创建用户和密码
[[email protected] ~]# echo "rsync:test" >>/etc/rsyncd/rsyncd.secrets
创建rsync配置文件
[[email protected] ~]# 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的配置文件路径 [[email protected] ~]# 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服务 [[email protected] ~]# service xinetd restart Stopping xinetd: [ OK ] Starting xinetd: [ OK ] [[email protected] ~]# netstat -anpt |grep 873 tcp 0 0 :::873 :::* LISTEN 1586/xinetd #创建数据目录 [[email protected] ~]# mkdir -p /data/test
三、配置lsyncd服务器
#安装rsync,lsyncd [[email protected] ~]# rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm [[email protected] ~]# sed -i ‘[email protected]#[email protected]@g‘ /etc/yum.repos.d/epel.repo [[email protected] ~]# sed -i ‘[email protected]@#[email protected]‘ /etc/yum.repos.d/epel.repo [[email protected] ~]# yum install rsync lsyncd -y
配置lsyncd服务配置文件
本地同步
1.1 本地目录同步:direct:cp/rm/mv。 适用:500+万文件,变动不大
[[email protected] ~]# 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, } #启动服务 [[email protected] ~]# /etc/init.d/lsyncd start Starting lsyncd: [ OK ] #测试,查看目录内容 [[email protected] ~]# 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 [[email protected] ~]# 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 #创建一个新文件 [[email protected] ~]# touch /data/test/for.py #查看结果 [[email protected] ~]# 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文件 [[email protected] ~]# rm -rf /data/test/*.txt [[email protected] ~]# ll /data/dest/ total 0 -rw-r--r-- 1 root root 0 Jul 30 15:03 for.py
1.2 本地目录同步rsync模式:rsync
#编辑lsyncd配置文件添加如下内容 [[email protected] ~]# vim /etc/lsyncd.conf sync{ default.rsync, source = "/data/test", target = "/tmp/ceshi", #如果目录不存在,会自动创建目录 delete = true, exclude = { "test"}, rsync = { compress = true, verbose = true, archive = true, } } #重启服务 [[email protected] ~]# /etc/init.d/lsyncd restart Stopping lsyncd: [ OK ] Starting lsyncd: [ OK ] #测试 [[email protected] ~]# ll /tmp/ceshi/ total 0 -rw-r--r-- 1 root root 0 Jul 30 15:03 for.py #在/data/test下添加一个文件ceshi.txt [[email protected] ~]# touch /data/test/ceshi.txt [[email protected] ~]# 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
#创建密码文件 [[email protected] ~]# echo "test" > /etc/backserver.pas [[email protected] ~]# chmod 600 /etc/backserver.pas #修改lsyncd的配置文件,添加如下内容 [[email protected] ~]# vim /etc/lsyncd.conf sync{ default.rsync, source = "/data/test", target = "[email protected]::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",} } } [[email protected] ~]# /etc/init.d/lsyncd restart Stopping lsyncd: [ OK ] Starting lsyncd: [ OK ] #测试,查看rsync:192.168.3.72上的test模块下的文件 [[email protected] ~]# hostname rsync [[email protected] ~]# 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 [[email protected] ~]# hostname lsyncd [[email protected] ~]# rm -rf /data/test/for.py #查看结果 [[email protected] ~]# hostname rsync [[email protected] ~]# 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 #测试添加一个文件 [[email protected] ~]# hostname lsyncd [[email protected] ~]# touch /data/test/docker.txt #查看结果 [[email protected] ~]# 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
时间: 2024-10-11 07:14:09