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 [email protected]::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 [email protected]$backup_Server::nfsbackup --password-file=/etc/rsync.password
    else
        cd $Path &&        rsync -az ./ --delete [email protected]$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

原文地址:https://www.cnblogs.com/fawaikuangtu123/p/10045862.html

时间: 2024-08-05 21:25:36

inotify+rsync实现实时同步(附解决crontab中无法执行python脚本的问题)的相关文章

inotify+rsync实现实时同步部署

1.1.架构规划 1.1.1架构规划准备 服务器系统 角色 IP Centos6.7 x86_64 NFS服务器端(NFS-server-inotify-tools) 192.168.1.14 Centos6.7 x86_64 rsync服务器端(rsync热备服务器) 192.168.1.17 1.1.2架构图 2.1 部署前检查 2.1.1 检查rsync热备服务器daemon是否起来 1 [[email protected] data]# lsof -i:873 2 COMMAND PID

CentOS 6.5 inotify+rsync做实时同步-企业实例(1.0)

inotify+rsync实时同步起源及简介 inotify服务机制 inotify实施准备 inotify配置是建立在rsync服务的基础上 inotify,sersync,lsyncd sersync功能更多,可以做各种过滤,但从性能上看,通过测试得出inotify性能更高,每秒钟能同步好几百张图片,inotify 150张就不能实时了 实施前检查rsync daemon是正常 [[email protected] oldboy]# ps -ef |grep daemon root     

inotify+rsync实现实时同步

引言:rsync实现数据备份,inotify监控文件系统提供通知功能,二者结合能够实现服务器之间文件的实时同步. rsync 特性: 可以镜像保存整个目录树和文件系统: 增量同步数据,文件传输效率高: 可以保持原有文件的权限.时间等属性: 加密传输数据,保证了数据的安全性: 支持断点续传: 可以使用rcp.ssh等方式传输文件,也可以通过socket连接传输文件: 支持匿名传输: rsync4种模式: 本地shell 远程shell 查询(列表)模式 服务器模式 本地shell模式: rsync

inotify+rsync做实时同步

一.配置inotify随机启动 vi /etc/rc.local nohup /home/jenkins/inotify/inotify.sh > nohup.out 2>&1 & 二.inotify同步脚本 #!/bin/bash #param src=/home/jenkins/conf/ dst_module=/home/jenkins/conf/ user=jenkins /usr/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:

inotify+rsync监控实时同步

监控到Centos7-001机器上的网站内容发生变化时,就同步Centos7-001:/var/www/html/目录到Centos7-002上 Centos7-001上所做的操作 安装 rsync # yum install rsync -y 生成密钥对 [[email protected] ~]# ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh

sersync基于rsync+inotify实现数据实时同步

一.环境描述 需求:服务器A与服务器B为主备服务模式,需要保持文件一致性,现采用sersync基于rsync+inotify实现数据实时同步 主服务器A:192.168.1.23 从服务器B:192.168.1.243 实时同步/var/atlassian目录到从服务器. 二.实施 1.从服务器192.168.1.243 rsync服务搭建 1.1安装软件包 wget http://rsync.samba.org/ftp/rsync/src/rsync-3.1.1.tar.gz tar xf r

rsync、inotify实现web实时同步

. rsync.inotify实现web实时同步,布布扣,bubuko.com

利用rsync+inotify实现数据实时同步脚本文件

将代码放在Server端,实现其它web服务器同步.首先创建rsync.shell,rsync.shell代码如下: #!/bin/bash host1=133.96.7.100 host2=133.96.7.101 host3=133.96.7.102 src=/data/www/ dst1=web1 dst2=web2 dst3=web3 user1=web1 user2=web2 user3=web3 /usr/local/inotify/bin/inotifywait -mrq --ti

20190308 samba服务、inotify和rsync实现实时同步、防火墙

Samba服务[root@centos7 ~]#yum install samba[root@centos7 ~]#systemctl start smb[root@centos7 ~]#ss -ntluNetid State Recv-Q Send-Q Local Address:Port Peer Address:Port tcp LISTEN 0 50 :139 : tcp LISTEN 0 50 :445 端口已开启 : 可以做共享了. 主配置文件:/etc/samba/smb.conf