【完美】linux文件同步rsync+inotify(第二部分,inotify)

介绍


第一部分,rsync

下载

源服务器
网址https://github.com/rvoicilas/inotify-tools/wiki
在github上获取download地址
wget https://github.s3.amazonaws.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz

安装

需要安装gcc
yum install -y gcc
解压
tar zxvf inotify-tools-3.14.tar.gz
安装
cd inotify-tools-3.14
./configure --prefix=/usr/local/inotify
make
make install

配置脚本

跳转路径
cd /usr/local/inotify
vi /usr/local/inotify/rsync.sh

----------------------------------------
host=192.168.159.101
srcdir=/home/rsync_file/
module=rsync_file
excludedir=/usr/local/inotify/exclude.list
rsyncuser=user1
rsyncpassdir=/etc/passwd.txt

/usr/local/inotify/bin/inotifywait -mrq --timefmt '%y/%m/%d %H:%M' --format '%T %w%f%e' -e close_write,modify,delete,create,attrib,move $srcdir |  while read files

do
rsync -avH --port=873 --progress --delete $srcdir $rsyncuser@$host::$module --password-file=$rsyncpassdir --exclude-from=$excludedir
echo "  ${files} was rsynced" >> /tmp/rsync.log 2>&1
done

----------------------------------------
:wq 保存退出
字段说明在备注

配置排除的文件夹(可为空)
touch /usr/local/inotify/exclude.list

设置脚本开机自动执行
vi /etc/rc.d/rc.local  #编辑,在最后添加一行
sh /usr/local/inotify/rsync.sh & #设置开机自动在后台运行脚本
:wq!
  #保存退出

获取权限
chmod 764 rsync.sh
chmod +x /usr/local/inotify/rsync.sh 

执行脚本
[root@bogon inotify]# ./rsync.sh

测试
在源服务器的/home/rsync_file/中新增文件夹
观察脚本信息
sending incremental file list
./
ceshi/
sent 121 bytes  received 27 bytes  14.10 bytes/sec
total size is 40  speedup is 0.00

出现以上信息
在目标服务中也同样出现ceshi文件夹
说明同步备份 成功!!

备注

设置系统环境变量,添加软连接(此步可不做)
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

配置脚本的字段说明
host :备份到目标服务器的ip地址,如果是多个目标则用空格分隔,外加双引号,并for in循环
例:
host="192.168.159.101 192.168.159.102"
for ip in $dstip
do
statement...$ip...
done
srcdir:原服务器需要同步的文件夹路径
module:模块名
excludedir: #不需要同步的目录,如果有多个,每一行写一个目录,使用相对于同步模块的路径;
例:不需要同步/home/rsync_file/目录下的a目录和b目录下面的b1目录,exclude.list文件可以这样写

a/
b/b1/

rsyncuser:源服务器同步用户名
rsyncpassdir源服务器同步密码文件路径
/tmp/rsync.log :脚本运行日志记录



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值