客户端拉取服务端更新
服务端配置
vim /etc/rsyncd.conf
secrets file = /etc/rsyncd.secrets #motd file = /etc/rsyncd.motd read only = yes list = yes uid = nobody gid = nobody hosts allow = 10.51.99.247 #哪些电脑可以访问rsync服务 #hosts deny = 192.168.100.0/24 #哪些电脑不可以访问rsync服务 max connections = 2 log file = /var/log/rsyncd.log pid file = /var/run/rsyncd.pid lock file = /var/run/rsyncd.lock [data] comment = loeye'dir from 10.51.79.40 path = /mnt/wwwroot/data auth users = root
vim /etc/rsyncd.secrets
root:123456
chmod -R 600 /etc/rsyncd.secrets
rsync --daemon --config=/etc/rsyncd.conf
echo "/usr/bin/rsync --daemon --config=/etc/rsyncd.conf" >>/etc/rc.d/rc.local
客户端配置
vim /etc/rsyncd.secrets
123456
vim /mnt/rsyncd.sh
#!/bin/sh rsync -vaAXHzu --progress --delete --exclude={"cache","runtime","*svn"} [email protected]::data /mnt/data/ --password-file=/etc/rsyncd.secrets
crontab -e
*/5 * * * * sh /mnt/rsyncd.sh
服务端推送更新
服务端配置
vim /etc/rsyncd.secrets
123456
以计划任务更新(可选)
vim /mnt/ rsyncd.sh
#!/bin/sh rsync -vaAXHzu --progress --delete --exclude={"cache","runtime","*svn"} /mnt/data/ [email protected] ::data --password-file=/etc/rsyncd.secrets
crontab -e
*/5 * * * * sh /mnt/rsyncd.sh
以Inotify-tools 实时更新(可选)
1、查看服务器内核是否支持inotify
ll /proc/sys/fs/inotify #列出文件目录,出现下面的内容,说明服务器内核支持inotify
-rw-r--r-- 1 root root 0 Mar 7 02:17 max_queued_events
-rw-r--r-- 1 root root 0 Mar 7 02:17 max_user_instances
-rw-r--r-- 1 root root 0 Mar 7 02:17 max_user_watches
备注:Linux下支持inotify的内核最小为2.6.13,可以输入命令:uname -a查看内核
CentOS 5.X 内核为2.6.18,默认已经支持inotify
2、安装inotify-tools
wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
https://codeload.github.com/rvoicilas/inotify-tools/tar.gz/v3.14 (备用)
tar zxvf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14/
./configure --prefix=/usr/local/inotify
make
make install
3、设置系统环境变量,添加软连接
echo "PATH=/usr/local/inotify/bin:$PATH" >>/etc/profile.d/inotify.sh
source /etc/profile.d/inotify.sh #使设置立即生效
echo "/usr/local/inotify/lib" >/etc/ld.so.conf.d/inotify.conf
ln -s /usr/local/inotify/include /usr/include/inotify
4、修改inotify默认参数(inotify默认内核参数值太小)
查看系统默认参数值
sysctl -a | grep max_queued_events
结果是:fs.inotify.max_queued_events = 16384
sysctl -a | grep max_user_watches
结果是:fs.inotify.max_user_watches = 8192
sysctl -a | grep max_user_instances
结果是:fs.inotify.max_user_instances = 128
修改参数:
sysctl -w fs.inotify.max_queued_events="99999999"
sysctl -w fs.inotify.max_user_watches="99999999"
sysctl -w fs.inotify.max_user_instances="65535"
vi /etc/sysctl.conf #添加以下代码
fs.inotify.max_queued_events=99999999
fs.inotify.max_user_watches=99999999
fs.inotify.max_user_instances=65535
参数说明:
max_queued_events:
inotify队列最大长度,如果值太小,会出现"** Event Queue Overflow **"错误,导致监控文件不准确
max_user_watches:
要同步的文件包含多少目录,可以用:find /home/www.osyunwei.com -type d | wc -l 统计,必须保证max_user_watches值大于统计结果(这里/home/www.osyunwei.com为同步文件目录)
max_user_instances:
每个用户创建inotify实例最大值
5、创建脚本,实时触发rsync进行同步
vim /usr/local/inotify/rsync.sh #编辑,添加以下代码
======================================
#!/bin/sh srcdir=/mnt/data/ dstdir=data rsyncuser=root rsyncpassdir=/etc/rsyncd.secrets dstip="192.1681.127 192.168.1.128" for ip in $dstip do rsync -vaAXHzu --progress --delete --exclude={"cache","runtime","*svn"} $srcdir $rsyncuser@$ip::$dstdir --password-file=$rsyncpassdir >> /dev/null 2>&1 done /usr/local/inotify/bin/inotifywait -mrq --exclude='^.*/(cache|runtime|\.svn|download|(index\.php$)|(.*\.(swp|swx)$))' --timefmt '%d/%m/%y %H:%M' --format '%T %w %f %e' --event close_write,modify,delete,create,attrib,move $srcdir | while read file do for ip in $dstip do rsync -vaAXHzu --progress --delete --exclude={"cache","runtime","*svn"} $srcdir $rsyncuser@$ip::$dstdir --password-file=$rsyncpassdir >> /dev/null 2>&1 echo " ${file} was rsynced" >> /tmp/rsync.log 2>&1 done done
======================================
chmod u+x /usr/local/inotify/rsync.sh #添加脚本执行权限
脚本参数说明:
srcdir=/mnd/data/ #源服务器同步目录
dstdir=data #目标服务器rsync同步目录模块名称
rsyncuser=root #目标服务器rsync同步用户名
rsyncpassdir=/etc/rsyncd.secrets #目标服务器rsync同步用户的密码在源服务器的存放路径
dstip="192.168.1.127 192.168.1.128" #目标服务器ip,多个ip用空格分开
/tmp/rsync.log #脚本运行日志记录
6、设置脚本开机自动执行
vim /etc/rc.d/rc.local #编辑,在最后添加一行
/usr/local/inotify/rsync.sh & #设置开机自动在后台运行脚本
7、启动脚本
/usr/local/inotify/rsync.sh &
客户端配置
vim /etc/rsyncd.conf
secrets file = /etc/rsyncd.secrets #motd file = /etc/rsyncd.motd read only = no #list = yes uid = root gid = root hosts allow = 10.51.79.40 #哪些电脑可以访问rsync服务 #hosts deny = 192.168.100.0/24 #哪些电脑不可以访问rsync服务 max connections = 2 log file = /var/log/rsyncd.log pid file = /var/run/rsyncd.pid lock file = /var/run/rsyncd.lock [data] comment = loeye'dir from 10.51.79.40 path = /mnt/wwwroot/data auth users = root
vim /etc/rsyncd.secrets
root:123456
chmod -R 600 /etc/rsyncd.secrets
rsync --daemon --config=/etc/rsyncd.conf
echo "/usr/bin/rsync --daemon --config=/etc/rsyncd.conf" >>/etc/rc.local