标签:rsync inotify 同步 实时同步 linux
rsync有很多种实现实时同步的方式,这里采用inotify-tools监控文件的状态触发rsync进行不同。目标服务器(同步到哪儿)监听rsync(daemon方式)作为服务器端,源客户端(从哪儿同步)采用inotify+rsync进行推送同步。
1. 目标服务器安装rsync服务
wget https://download.samba.org/pub/rsync/src/rsync-3.1.2.tar.gz mkdir /usr/local/rsync -p tar xf rsync-3.1.2.tar.gz cd rsync-3.1.2 ./configure --prefix=/usr/local/rsync make && make install mkdir /usr/local/rsync/conf
2. 目标服务器配置
cat > /usr/local/rsync/conf/rsync.conf <<EFO pid file = /var/run/rsyncd.pid log file = /var/log/rsync.log log format = %t %a %m %f %b timeout = 100 use chroot = yes read only = no uid = root gid = root port=22873 #修改默认端口 [sbv_192] # 客户端需要的使用名字 需要同步多个就在 path=/home/xiaoyou/bak # 同步到的目录 list=yes ignore error auth users = user01 # 同步使用的用户 secrets file = /usr/local/rsync/conf/rsync_server.pw #同步使用的密码 EFO echo "user01:fgaogjpajdfgpasdgadg" > /usr/local/rsync/conf/rsync_server.pw chmod 600 /usr/local/rsync/conf/rsync_server.pw #修改密码权限 必须 /usr/local/rsync/bin/rsync --daemon --config=/usr/local/rsync/conf/rsync_firsh.conf #启动rsync服务
3. 源服务器安装rsync+inotify
wget https://download.samba.org/pub/rsync/src/rsync-3.1.2.tar.gz mkdir /usr/local/rsync -p tar xf rsync-3.1.2.tar.gz cd rsync-3.1.2 ./configure --prefix=/usr/local/rsync make && make install mkdir /usr/local/rsync/conf
wget https://ncu.dl.sourceforge.net/project/inotify-tools/inotify-tools/3.13/inotify-tools-3.13.tar.gz mkdir /usr/local/inotify tar xf inotify-tools-3.13.tar.gz cd inotify-tools-3.13 && ./configure --prefix=/usr/local/inotify && make && make install mkdir /usr/local/inotify/scripts
4.源服务相关配置
4.1 同步使用脚本
cat /usr/local/inotify/scripts/rsync_to_254.sh #!/bin/bash host="192.168.199.254" 目标服务器地址 src=/home/xiaoyou/src/ 源客户端同步的目录 des=sbv_192 目标服务器rsync配置的 user=user01 同步使用账号 port=22873 同步端口 pw=/usr/local/rsync/conf/rsync_clint.pw 同步使用密码文件 /usr/local/inotify/bin/inotifywait -mrq --timefmt ‘%d-%m-%y %H:%M‘ --format ‘%Xe %w%f‘ -e modify,delete,create,attrib $src | while read files do /usr/local/rsync/bin/rsync -auq --port=${port} --delete --progress --password-file=${pw} $src $user@$host::$des echo "${files} was rsyncd" >> /tmp/rsync_${des}.log 2>&1 done
4.2客户端密码文件配置
echo "fgaogjpajdfgpasdgadg" > /usr/local/rsync/conf/rsync_clint.pw chmod 660 /usr/local/rsync/conf/rsync_clint.pw
4.3 启动同步脚本
sh /usr/local/inotify/scripts/rsync_to_254.sh >> /dev/null 2>&1 &
本文出自 “Poor Wretch” 博客,请务必保留此出处http://youprince.blog.51cto.com/9272426/1975019
标签:rsync inotify 同步 实时同步 linux
原文地址:http://youprince.blog.51cto.com/9272426/1975019