Inotify-tools 可以帮助Liunx快速实现文件同步。
1. 安装Inotify-tools
mkdir /soft chmod 775 /soft cd /soft wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz tar zxvf inotify-tools-3.14.tar.gz cd inotify-tools-3.14 ./configure --prefix=/usr && make && su -c 'make install'
2. 设置系统环境变量,添加软连接
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
3. 修改系统参数,提高队列性能
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"
4 创建Shell脚本
rsync -az --delete /data/ test@172.16.120.252::ftp --password-file=/etc/passwd.txt /usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e close_write,modify,delete,create,attrib,move /data | while read file do rsync -az --delete /data/ test@172.16.120.252::ftp --password-file=/etc/passwd.txt echo " ${file} was rsynced" >> /tmp/rsync.log 2>&1 done ~
5 设置同步密码:
vim /etc/passwd.tx
#写入同步密码
6 设置同步日志
vim /tmp/rsync.log chmod 775 rsync.log
7 设置系统启动执行脚本
vim /etc/rc.d/rc.local #编辑,在最后添加一行 sh /usr/local/inotify/rsync.sh & #设置开机自动在后台运行脚本 :wq! #保存退出