rsync+sersync实现文件实时同步
server作为备份服务器,rsyncd,客户端client使用工具连接
rsync 的服务端配置(rsyncd.conf
)仅对服务器端的 rsyncd 服务生效,必须在服务器端重启服务才能加载新配置。客户端的 rsyncd 服务与当前连接的服务器端无关,重启客户端服务既不会让服务器端配置生效,也不影响客户端用rsync
命令发起连接(客户端只需安装 rsync 工具,无需运行 rsyncd 服务)。
改服务端配置,就重启服务端的 rsyncd;客户端不需要运行 rsyncd 服务,也不用重启。
#安装软件包[root@server ~ 10:14:10]# yum install -y rsync# 准备同步目录,该目录任何用户都可以读写。[root@server ~ 08:43:26]# mkdir -m 777 /backup# 配置rsync,不验证用户,直接同步[root@server ~ 10:11:06]# vim /etc/rsyncd.conf#vim添加命令行# 添加如下配置[backup]# 描述信息comment = backup# 备份路径path = /backup# 设置可写read only = no# 指定用户名auth users = rsync# 指定用户密码文件secrets file = /etc/rsyncd.secrets#编辑root权限[root@server ~ 10:26:58]# vim /etc/rsyncd.confuid=rootgid=root#重启[root@server ~ 10:29:23]# systemctl restart rsyncd --now# 创建用户凭据文件[root@server ~ 10:13:16]# echo 'rsync:redhat' > /etc/rsyncd.secrets[root@server ~ 10:13:35]# chmod 400 /etc/rsyncd.secrets# 启用并启动rsyncd服务[root@server ~ 10:13:43]# systemctl enable rsyncd --nowCreated symlink from /etc/systemd/system/multi-user.target.wants/rsyncd.service to /usr/lib/systemd/system/rsyncd.service.# 配置防火墙[root@server ~ 10:13:50]# firewall-cmd --add-service=rsyncdsuccess[root@server ~ 10:14:01]# firewall-cmd --add-service=rsyncd --permanentsuccess#关闭seLinux[root@server ~ 10:28:59]# setenforce 0
客户端配置和测试
# 准备密码文件[root@client ~ 09:21:27]# echo redhat > rsyncd.secrets[root@client ~ 10:20:48]# chmod 400 rsyncd.secrets# 传输测试[root@client ~ 10:30:04]# rsync -av --password-file=./rsyncd.secrets /etc/hostname rsync@server::backupsending incremental file listhostnamesent 110 bytes received 35 bytes 290.00 bytes/sectotal size is 17 speedup is 0.12# 验证结果[root@server ~ 10:32:35]# ls /backup/hostname
安装软件包
# 下载软件[root@client ~]# wget https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/sersync/sersync2.5.4_64bit_binary_stable_final.tar.gz#或者将sersync2.5.4_64bit_binary_stable_final.tar.gz拖入窗口安装[root@client ~ 10:41:09]# rz -Erz waiting to receive.# 解压文件[root@client ~ 10:54:48]# tar xf sersync2.5.4_64bit_binary_stable_final.t
配置文件
[root@client ~ 10:57:18]# vim confxml.xml<?xml version="1.0" encoding="ISO-8859-1"?><head version="2.5"><host hostip="localhost" port="8008"></host><debug start="false"/><fileSystem xfs="true"/><filter start="true"><exclude expression="^cache/*"></exclude></filter><inotify><delete start="true"/><createFolder start="true"/><createFile start="false"/><closeWrite start="true"/><moveFrom start="true"/><moveTo start="true"/><attrib start="false"/><modify start="false"/></inotify><sersync><localpath watch="/app_data"><remote ip="10.1.8.10" name="backup"/></localpath><rsync><commonParams params="-azP"/><auth start="true" users="rsync" passwordfile="/root/rsyncd.secrets"/><userDefinedPort start="false" port="873"/><timeout start="false" time="100"/><ssh start="false"/></rsync><failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><crontab start="false" schedule="600"><!--600mins--><crontabfilter start="false"><exclude expression="*.php"></exclude><exclude expression="info/*"></exclude></crontabfilter></crontab><plugin start="false" name="command"/></sersync><plugin name="command"><param prefix="/bin/sh" suffix="" ignoreError="true"/><filter start="false"><include expression="(.*)\.php"/><include expression="(.*)\.sh"/></filter></plugin></head>
运行Sersync
[root@client ~ 11:01:20]# cp GNU-Linux-x86/sersync2 /usr/local/bin/[root@client ~ 11:02:24]# sersync2 -hset the system paramexecute:echo 50000000 > /proc/sys/fs/inotify/max_user_watchesexecute:echo 327679 > /proc/sys/fs/inotify/max_queued_eventsparse the command param_______________________________________________________参数-d:启用守护进程模式参数-r:在监控前,将监控目录与远程主机用rsync命令推送一遍c参数-n: 指定开启守护线程的数量,默认为10个参数-o:指定配置文件,默认使用confxml.xml文件参数-m:单独启用其他模块,使用 -m refreshCDN 开启刷新CDN模块参数-m:单独启用其他模块,使用 -m socket 开启socket模块参数-m:单独启用其他模块,使用 -m http 开启http模块不加-m参数,则默认执行同步程序_____________________________________________________________# 运行 Sersync,-d 后台运行[root@client ~ 11:02:35]# sersync2 -o confxml.xml -dset the system paramexecute:echo 50000000 > /proc/sys/fs/inotify/max_user_watchesexecute:echo 327679 > /proc/sys/fs/inotify/max_queued_eventsparse the command paramoption: -o config xml name: confxml.xmloption: -d run as a daemondaemon thread num: 10parse xml config filehost ip : localhost host port: 8008now the filter work ,if you set the crontab,you have to set crontab filter WARNING XFS FILE SYSTEM WORKdaemon start,sersync run behind the console use rsync password-file :user is rsyncpasswordfile is /root/rsyncd.secretsconfig xml parse successplease set /etc/rsyncd.conf max connections=0 Manuallysersync working thread 12 = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads) Max threads numbers is: 22 = 12(Thread pool nums) + 10(Sub threads)please according your cpu ,use -n param to adjust the cpu raterun the sersync: watch path is: /app_data
测试文件实时同步
# server 端监控目录 /backup[root@server ~ 10:41:27]# watch -n 1 ls /backup#在客户端创建文件和目录[root@client ~ 11:03:42]# echo hello world > /app_data/welcome.txt[root@client ~ 11:04:22]# mkdir /app_data/dbdata# 客户端删除文件[root@client ~ 11:04:37]# rm -fr /app_data/*
[root@client ~ 11:04:55]# cp confxml.xml /etc/sersyncd.conf[root@client ~ 11:23:24]# vim /etc/systemd/system/sersyncd.service[Unit]Description=SerSync server daemon[Service]Type=forkingExecStart=/usr/local/bin/sersync2 -o /etc/sersyncd.conf -d[Install]WantedBy=multi-user.target[root@client ~ 11:24:22]# systemctl daemon-reload [root@client ~ 11:24:27]# systemctl enable sersyncd.serviceCreated symlink from /etc/systemd/system/multi-user.target.wants/sersyncd.service to /etc/systemd/system/sersyncd.service.