文件同步rsync
通過使用rsync作為文件同步服務(wù)器,通過sersync + inotify-tools監(jiān)控文件變化,調(diào)用rsync實現(xiàn)增量同步至rsync遠(yuǎn)程文件服務(wù)器
1. 環(huán)境:
服務(wù)器:centos 7: 192.168.41.49
客戶端:centos 7:192.168.3.88
2. 安裝部署:
- 安裝:yum install rsync -y
- 配置:
-
rsyncd 配置文件:
vim /etc/rsyncd.conf
-
-
uid = rsync gid = rsync fake super = yes use chroot = no ignore errors = yes max connections = 200 timeout = 600 pid file = /var/run/rsyncd.pid log format = %o %h [%a] %m (%u) %f %l lock file = /var/run/rsync.lock log file = /var/log/rsyncd.log read only = false list = false [app_backup] comment = welcome to app scan backup! path = /opt/xScan/mercury/data/app_wait_parse/ hosts allow = 192.168.3.0/24 auth users = rsync_app_backup secrets file = /etc/rsync.passwd
- [app_backup] : 要備份的模塊名,[...]
- comment: 注釋
- path:同步的文件目錄,注意設(shè)置目錄權(quán)限為uid可讀寫
- hosts allow: 允許同步到此目錄的主機ip地址
- auth users: 同步時使用的賬號
- secrets file:通過使用的密碼所在文件,密碼文件權(quán)限為600
- 配置文件格式與ini類型,外城為全局變量,模塊內(nèi)為模塊局部變量
-
- 配置模塊登陸用戶的密碼文件:/etc/rsync.passwd
echo "rsync_app_backup:app_backup" > /etc/rsync.passwd # 有用戶+”:”+密碼構(gòu)成 chmod 600 /etc/rsync.passwd
- 添加非登陸用戶:
useradd -s /sbin/nologin -M rsync id rsync chown -R rsync.rsync /opt/xScan/mercury/data/app_wait_parse/
- 部署啟動:
# systemctl 啟動
systemctl restart rsyncd
# 設(shè)置開機啟動
systemctl enable rsyncd
# daemon 啟動
rsync --daemon --config=/etc/rsyncd.conf
3. sersync 安裝部署
- 安裝
[root@test02 ~]# cd /opt/ [root@test02 opt]# wget https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/sersync/sersync2.5.4_64bit_binary_stable_final.tar.gz [root@test02 opt]# tar xvf sersync2.5.4_64bit_binary_stable_final.tar.gz [root@test02 opt]# mv GNU-Linux-x86/ /usr/local/sersync/
- 依賴:inotify,centos7 默認(rèn)支持,確認(rèn):
[root@test02 ~]# ll /proc/sys/fs/inotify 總用量 0 -rw-r--r--. 1 root root 0 12月 28 19:35 max_queued_events -rw-r--r--. 1 root root 0 12月 28 17:16 max_user_instances -rw-r--r--. 1 root root 0 12月 28 19:35 max_user_watches [root@test02 ~]#
- 調(diào)整inotify:應(yīng)用于監(jiān)控的目錄中包含文件和目錄數(shù)量較多的情況
- 查看默認(rèn)參數(shù):
- sysctl -a | grep max_queued_events
- sysctl -a | grep max_user_instances
- sysctl -a | grep max_user_watches
- 修改:
- sysctl -w fs.inotify.max_queued_events="99999999"
- sysctl -w fs.inotify.max_user_watches="99999999"
- sysctl -w fs.inotify.max_user_instances="65535"
- 保存生效:
- sysctl -p
- 配置
<?xml version="1.0" encoding="ISO-8859-1"?> <head version="2.5"> <host hostip="localhost" port="8008"></host> <debug start="false"/> <fileSystem xfs="false"/> <filter start="false"> <!-- 過濾 --> <exclude expression="(.*)\.svn"></exclude> <exclude expression="(.*)\.gz"></exclude> <exclude expression="^info/*"></exclude> <exclude expression="^static/*"></exclude> </filter> <inotify> <!-- 監(jiān)控事件 --> <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> <!-- 監(jiān)控待同步目錄 --> <localpath watch="/opt/xScan/mercury/data/app_wait_parse"> <remote ip="192.168.41.49" name="app_backup"/> <!--<remote ip="192.168.8.39" name="tongbu"/>--> <!--<remote ip="192.168.8.40" name="tongbu"/>--> </localpath> <rsync> <commonParams params="-artuz"/> <auth start="true" users="rsync_app_backup" passwordfile="/etc/rsync_app_backup.passwd"/> <userDefinedPort start="false" port="874"/><!-- port=874 --> <timeout start="false" time="100"/><!-- timeout=100 --> <ssh start="false"/> </rsync> <failLog path="/opt/xScan/mercury/data/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once--> <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>
- 配置密碼文件(注意客戶端與服務(wù)器的密碼文件不同):
[root@test03 sersync]# echo 'app_backup' > /etc/rsync_app_backup.passwd [root@test03 sersync]# chmod 600 /etc/rsync_app_backup.passwd
- 部署啟動:
[root@test03 sersync]# nohup /usr/local/sersync/sersync2 -r -d -o /usr/local/sersync/confxml.xml > /var/log/sersync.log 2>&1 &
-d:啟用守護(hù)進(jìn)程模式
-r:在監(jiān)控前,將監(jiān)控目錄與遠(yuǎn)程主機用rsync命令推送一遍
-n:指定開啟守護(hù)線程的數(shù)量,默認(rèn)為10個
-o:指定配置文件,默認(rèn)使用confxml.xml文件

浙公網(wǎng)安備 33010602011771號