备份方法scp rsync,inotifywait

本文详细介绍了如何利用inotify-tools监听文件系统事件,并结合rsync实现实时文件同步,同时通过日志记录监控同步过程。通过脚本示例,展示了如何设置实时监控目录变更、触发同步操作并记录到日志文件中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考http://blog.leezhong.com/project/2010/12/13/inotify-rsync.html
https://github.com/rvoicilas/inotify-tools/wiki/
远程执行脚本
ssh -l root 10.10.10.10 "/opt/test/stop.sh";
ssh -l root 10.10.10.10 "scp /opt/data/database.tcb 10.10.10.10:/opt/ttserver/test/test.tcb"

rsync+inotifywait
#!/bin/sh
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f %e' \
-e modify,create,move \
/opt/cms5/share/template/cms_page/template/ \
| while read date time file event
do
rsync -av --progress --include=kk --exclude='*' --password-file=/etc/rsyncd.secrets /opt/backuptest/ haha@10.1.224.171::test >>/opt/log/templatenotify.log
#echo "${date} ${time} ${file} ${event}"
done


#!/bin/sh
#/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w %f %e' \
/usr/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w %f %e' \
-e modify,create,move \
/opt/backuptest/ \
| while read date time path file event
do
# rsync -av --password-file=/etc/rsyncd.secrets /opt/rsynctest/ test@10.10.10.10::rsynctest >>/tmp/rsync.log
rsync --include='${file}' --exclude='*' -av ${path} root@10.1.224.171:${path}
echo "${date} ${time} ${path} ${file} ${event}"
done

--------templatenotify.sh----------
#!/bin/sh
#this is a test not used for production ,please look 154
logfile="/opt/log/templatenotify-`date +%Y%m%d`.log"
toip=10.11.10.10
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f %e' \
-e CLOSE_WRITE,delete,create,move \
/opt/template/ \
| while read date time file event
do
case "$event" in
DELETE)
echo "delete ${file} but not rsync ----delete--- over" >>${logfile};
;;
*)
if [ "${file: -4}" != '4913' ] && [ "${file: -1}" != '~' ]; then
echo "rsync -av $file root@${toip}:`dirname $file`" >>${logfile}
rsync -av $file root@${toip}:`dirname $file` >>${logfile} 2>&1
echo "${date} ${time} ${file} ${event} --------over" >>${logfile}
fi
;;
esac
done

定时删除无用文件比如log
crontab -l
0 3 * * * (find /opt/modules/ -type f -mtime +2 -exec rm -f {} \;)

[@tc_157_49 share]# cat /usr/local/src/switch/templatenotify.sh
#!/bin/sh
#this is a test not used for production ,please look 154
logfile="/opt/log/templatenotify-`date +%Y%m%d`.log"
toip=10.11.157.53
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f %e' \
-e CLOSE_WRITE,delete,create,move \
/opt/cms5/share/template/cms_page/template/ \
| while read date time file event
do
case "$event" in
DELETE)
echo "delete ${file} but not rsync ----delete--- over" >>${logfile};
;;
*)
if [ "${file: -4}" != '4913' ] && [ "${file: -1}" != '~' ]; then
echo "rsync -av $file root@${toip}:`dirname $file`" >>${logfile}
rsync -av $file root@${toip}:`dirname $file` >>${logfile} 2>&1
echo "${date} ${time} ${file} ${event} --------over" >>${logfile}
fi
;;
esac
done
[@tc_157_49 share]#
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值