标签:inotfiy rsync inotfy_rsync_dirs.sh
系统:centos6.5
ip:10.19.21.241 被同步端
10.19.21.242 同步端
一.配置rsync(同步端)
1.安装rsync
# yum install -y rsync
2.写rsync配置
# vi /etc/rsyncd.conf uid = root gid = root use chroot = no max connections = 0 #最大连接数不限制 log file = /var/log/rsyncd.log pid file = /var/run/rsyncd.pid lock file = /var/run/rsyncd.lock hosts allow = * #允许所有主机 [test1] path = /home/test1/ comment = rsync from everyone read only = no list = no auth users = rsyncuser secrets file = /etc/rsync.pas [test2] path = /home/test2/ comment = rsync from everyone read only = no list = no auth users = rsyncuser secrets file = /etc/rsync.pas
3.创建密码文件
# echo "rsyncuser:123456" > /etc/rsync.pas
4.更改密码文件权限
# chmod 600 /etc/rsync.pas
5.启动服务
# /usr/bin/rsync --daemon --config=/etc/rsyncd.conf
二.配置inotify(被同步端)
1.安装rsync
# yum install -y rsync
2.安装inotify
# wget http://nchc.dl.sourceforge.net/project/inotify-tools/inotify-tools/3.13/inotify-tools-3.13.tar.gz # tar zxf inotify-tools-3.13.tar.gz # cd inotify-tools-3.13 # ./configure --preifx=/usr/local/inotify # make && make install
3.创建inotify_rsync_dirs.sh脚本
#!/bin/bash inotify_rsync_fun () { dir=`echo $1 | awk -F"," ‘{print $1}‘` ip=`echo $1 | awk -F"," ‘{print $2}‘` des=`echo $1 | awk -F"," ‘{print $3}‘` user=`echo $1 | awk -F"," ‘{print $4}‘` /usr/local/inotify/bin/inotifywait -mr --timefmt ‘%d/%m/%y %H:%M‘ --format ‘%T %w %f‘ -e modify,delete,create,attrib ${dir} |while read DATE TIME DIR FILE do FILECHAGE=${DIR}${FILE} /usr/bin/rsync -av --progress --delete --password-file=/etc/rsync.pas ${dir} ${user}@${ip}::${des} && echo "At ${TIME} on ${DATE}, file $FILECHAGE was backed up via rsync" >> /var/log/rsyncd.log done } count=2 # localdir,host,rsync_module,user of rsync_module, sync1="/home/test1/,10.19.21.242,test1,rsyncuser" sync2="/home/test2/,10.19.21.242,test2,rsyncuser" ############################################################# #main i=0 while [ ${i} -lt ${count} ] do i=`expr ${i} + 1` tmp="sync"$i eval "sync=\$$tmp" inotify_rsync_fun "$sync" & done
相关注释如下:
/usr/local/inotify/bin/inotifywait -mr
-m 是保持一直监听
-r 是递归查看目录
-q 是打印出事件
-e modify,delete,create,attrib 是指“监听 创建 移动 删除 写入权限”
/usr/bin/rsync -av --progress --delete --password-file=/etc/rsync.pas
-a 存档模式
-delete 删除多余的文件
--password-file 密码文件
4.添加密码文件
# echo 123456 > /etc/rsync.pas
5.添加权限
# chmod +x inotify_rsync_dirs.sh
6.执行命令
# ./inotify_rsync_dirs.sh
三.测试
241上创建目录 [root@VM-241 home]# mkdir /home/test1/dir{1,2,3} 242上查看 [root@VM-242 home]# ll test1 总用量 12 drwxr-xr-x 2 root root 4096 5月 13 18:33 dir1 drwxr-xr-x 2 root root 4096 5月 13 18:33 dir2 drwxr-xr-x 2 root root 4096 5月 13 18:33 dir3 241上创建文件 [root@VM-241 home]# touch /home/test2/file{1,2,3} 242上查看 [root@VM-242 home]# ll test2 总用量 0 -rw-r--r-- 1 root root 0 5月 14 10:49 file1 -rw-r--r-- 1 root root 0 5月 14 10:49 file2 -rw-r--r-- 1 root root 0 5月 14 10:49 file3
本文出自 “charlie_cen” 博客,请务必保留此出处http://charlie928.blog.51cto.com/3741218/1651187
标签:inotfiy rsync inotfy_rsync_dirs.sh
原文地址:http://charlie928.blog.51cto.com/3741218/1651187