inotify+rsync实现实时同步(附解决crontab中无法执行python脚本的问题)
1.准备环境
# 系统支持的话,下面的目录就会存在
ls /proc/sys/fs/inotify/
rpm -qa inotify-tools
yum -y install inotify-tools
2.inotifywait监控目录状态变化
/usr/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e delete,create,close_write /data
# 可以把时间去掉
/usr/bin/inotifywait -mrq --format '%w%f' -e delete,close_write /data
3.循序渐进的同步脚本
#!/bin/bash
Path=/data
Ip=172.16.1.41
/usr/bin/inotifywait -mrq --format '%w%f' -e delete,close_write $Path | while read file
do
cd $Path &&\
rsync -az ./ --delete rsync_backup@172.16.1.41::nfsbackup --password-file=/etc/rsync.password
done
# 上边那个脚本效率很低,效率低的原因在于只要目录出现变化就都会导致我整个目录下所有东西都被推送一遍.
# 因此,我们可以做如下改动提高效率.
#!/bin/bash
Path=/data
backup_Server=172.16.1.41
/usr/bin/inotifywait -mrq --format '%w%f' -e create,close_write,delete /data | while read line
do
if [ -f $line ];then
rsync -az $line --delete rsync_backup@$backup_Server::nfsbackup --password-file=/etc/rsync.password
else
cd $Path &&\
rsync -az ./ --delete rsync_backup@$backup_Server::nfsbackup --password-file=/etc/rsync.password
fi
done
# 脚本可以加入开机启动
echo "/bin/sh /server/scripts/inotify.sh &" >> /etc/rc.local
4.参数优化
cat /proc/sys/fs/inotify/*
max_queued_events max_user_instances max_user_watches
echo "50000000" > /proc/sys/fs/inotify/max_user_watches
echo "326790" > /proc/sys/fs/inotify/max_queued_events
5.inotify优缺点
监控文件系统事件变化,通过同步工具实现实时数据同步;
并发如果大于200个文件(10-100k),同步就会有延迟.
6.通过start/stop控制inotify.sh脚本的启动和停止
#!/bin/bash
#chkconfig: 2345 38 46
. /etc/init.d/functions if [ $# -ne 1 ];then
usage: $0 |start|stop|
exit 1
fi case "$1" in
start)
/bin/sh /server/scripts/inotify.sh &
echo $$ > /var/run/inotify.pid
if [ `ps -ef | grep inotify | grep -v grep | wc -l` -gt 2 ];then
action "inotify service is started" /bin/true
else
action "inotify service is started" /bin/false
fi
;;
stop)
kill -9 `cat /var/run/inotify.pid` > /dev/null 2>&1
pkill inotifywait
sleep 1
if [ `ps -ef | grep inotify | grep -v grep | wc -l` -eq 0 ];then
action "inotify service is stopped" /bin/true
else
action "inotify service is stopped" /bin/false
fi
;;
*)
usage: $0 |start|stop|
exit 1
esac
chkconfig --add syncd
7.解决crontab中无法执行python脚本的问题
a.首先你得知道是环境变量问题还是你的脚本执行有报错,
像一些编码报错、没写全路径、缺少环境变量的问题,就不在这里赘述了;
b.将问题先定位到日志中
*/1 * * * * python绝对路径 脚本绝对路径 >> 日志文件绝对路径 2>&1
查看日志发现脚本被定时执行了,但日志有报错:
TERM environment variable not set.
IndexError: list index out of range
经过排查,是因为脚本中执行了os.popen('top -n 1 | awk xxx')这样的语句,
导致取数据的列表为空,对空列表取值就会报上面的错,替换相应的程序即可解决问题.
参考博客:https://www.cnblogs.com/jackyyou/p/5681126.html
参考博客:https://www.cnblogs.com/chensiqiqi/p/6542268.html
inotify+rsync实现实时同步(附解决crontab中无法执行python脚本的问题)的更多相关文章
- 【转】inotify+rsync实现实时同步
[转]inotify+rsync实现实时同步 1.1 什么是实时同步:如何实现实时同步 要利用监控服务(inotify),监控同步数据服务器目录中信息的变化 发现目录中数据产生变化,就利用rsync服 ...
- Rsync+inotify实现文件实时同步#附shell脚本
强烈推荐先仔细看此文 https://segmentfault.com/a/1190000002427568 实验环境 centos 7.3 vm2:192.168.221.128 同步服务器 vm1 ...
- inotify+rsync实现实时同步
第1章 数据实时同步介绍 1.1 什么是实时同步:如何实现实时同步 A. 要利用监控服务(inotify),监控同步数据服务器目录中信息的变化 B. 发现目录中数据产生变化,就利用rsync服务推送到 ...
- lsyncd替代inotify+rsync实现实时同步
因公司业务需要需要实时同步日志文件,刚一开始使用的是inotify+rsync来实现实时同步,但时间久而久之发现同步的速度越来越慢,往往延迟好几个小时.查了一下网上的inotify+rsync方案基本 ...
- inotify+rsync文件实时同步
原文转自http://dl528888.blog.51cto.com/2382721/771533/ 之前做了“ssh信任与scp自动传输脚本”的技术文档,此方案是作为公司里备份的方法,但在实际的运行 ...
- inotify+rsync实现实时同步部署
1.1.架构规划 1.1.1架构规划准备 服务器系统 角色 IP Centos6.7 x86_64 NFS服务器端(NFS-server-inotify-tools) 192.168.1.14 Cen ...
- inotify +rsync进行实时同步
1.安装rpm -ivh http://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpmyum -y install ino ...
- inotify+rsync实现实时同步并邮件通知
服务器之间文件实时同步,监控文件的变化,发送邮件通知,并实时同步文件. 由于人工同步多台服务器的文件比较吃力,可以借助这样一套软件,自动化的实现这样的工作. 并且可以事实监控变化发送邮件给系统管理人员 ...
- inotify+rsync目录实时同步
两台linux服务器系统CentOS7 一台Apache IP:192.168.155.130(发布文件服务器,也可以叫rsync客户端) 一台nginx IP:192.168.155.131(同步镜 ...
随机推荐
- 在windows7 32ibt安装MongoDB数据库的方法及连接失败解决方案
参考 https://www.cnblogs.com/cnblogs-jcy/p/6734889.html http://yunkus.com/mongodb-install-config-in-wi ...
- poj-2524 ubiquitous religions(并查集)
Time limit5000 ms Memory limit65536 kB There are so many different religions in the world today that ...
- luogu3381 【模板】最小费用最大流
每次选代价最小的流增广 #include <iostream> #include <cstring> #include <cstdio> #include < ...
- Leetcode37--->Sudoku Solver(填充数独)
题目: 给定一个不完整的数独,要求填充好数独:最初给出的数独是有效的,且假设一定有答案: 举例: A sudoku puzzle... 解题思路: 该题与青蛙走迷宫问题很相似,都是用深度优先: 代码如 ...
- Leetcode 464.我能赢吗
我能赢吗 在 "100 game" 这个游戏中,两名玩家轮流选择从 1 到 10 的任意整数,累计整数和,先使得累计整数和达到 100 的玩家,即为胜者. 如果我们将游戏规则改为 ...
- [python工具篇][pycharm安装与配置][1]安装与设置
1 官网下载专业版 2 打开pycharm,选择license server 激活,地址输入:http://idea.imsxm.com 3 新建工程(一个大文件夹) 4 设置字体大小(file-&g ...
- 2016-2017 ACM Central Region of Russia Quarterfinal Programming Contest BHanoi tower
B Hanoi tower It has become a good tradition to solve the “Hanoi tower” puzzle at programming contes ...
- 连通图 poj2186 最受欢迎的牛(求最受欢迎的牛的数量)
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 27531 Accepted: 11077 De ...
- div固定在屏幕底部
项目中需要实现div一直显示在屏幕的底部,不管页面有多长或者多端都需要满足. 在网上找的用js实现的,移动时会闪动,效果不是特别好.也可能是没找到好的. 相比js,我更希望使用css实现 1 < ...
- Codeforces 898E Squares and not squares
题目大意 给定 $n$($n$ 是偶数,$2\le n\le 2\times 10^{5}$)个非负整数 $a_1,\dots, a_n$($a_i\le 10^9$). 要求将其中 $n/2$ 个数 ...