centos 6 rsync+inotify 实时同步

主机名、ip:

server 172.31.82.184

client  172.31.82.185

 

需求:

1、server端 ”/data/server“ 做为client端 “/data/client” 的备份目录;

2、实现功能是client端对该目录做增删权限变化操作,server端能保持实时同步;

3、关闭防火墙和selinux

    service iptables stop

    setenforce 0

一、配置server端    

1、安装rsync软件    

yum install -y rsync -y

2、创建rsync.conf配置文件,默认该文件不存在

vim /etc/rsyncd.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
uid = root
gid = root
usechroot = no
max connections = 20
timeout = 600
pid file /var/run/rsyncd.pid
lock file /var/run/rsync.lock
log file /var/log/rsyncd.log
[server]                     ##此处是定义的模块名,和“path = /data/server/”下server有关  rsync 同步文件的时候要使用web_log 自动关联到path路径
path = /data/server/
ignore errors
read only = false
writeonly = false
list = false
hosts allow = *
auth users = backuser
secrets file /etc/rsync.password

3、创建备份目录

mkdir /data/server -p

4、创建rsync用户名和密码文件

echo "backuser:123" >> /etc/rsync.password

5、为/etc/rsync.password 授权为600

chmod 600 /etc/rsync.password

6、启动rsync服务并添加开机自动启动

 /usr/bin/rsync --daemon &

echo "/usr/bin/rsync --daemon" >> /etc/rc.local

二、配置client端

1、安装rsync

yum install rsync -y

2、设置 rsync 客户端的密码文件,客户端只需要设置 rsync 同步的密码即可,不用设置用户名

echo "123" > /etc/rsync.password

3、将密码文件的权限设置成 600       

chmod 600 /etc/rsync.password

 

配置inotyfi

1、安装基础编译包

yum install -y gcc lrzsz

1
tar zxvf inotify-tools-3.14.tar.gz &&cd inotify-tools-3.14 &&./configure &&make &&make install

2、创建client端同步目录

mkdir -p /data/client

3、在client端测试是否可以同步文件

上传文件

rsync -vzrtopg --progress /data/client/ backuser@172.31.82.184::server  --password-file=/etc/rsync.password 

下载文件

rsync -vzrtopg --progress  backuser@172.31.82.184::server /opt/  --password-file=/etc/rsync.password

4、写一个脚本实现当client端  “/data/client”目录文件有变化时,让server节点同步client数据

1
2
3
4
5
6
7
8
9
10
#!/bin/bash
src=/data/client/  ##注意路径
des1=server        ##注意路径
host1=172.31.82.184 ##server端ip
user1=backuser     ##同步数据使用的用户
/usr/local/bin/inotifywait -mrq --timefmt %y/%m'%d %H:/%M' --format '%T %w%f' -emodify,delete,create,attrib $src | while read file
do
/usr/bin/rsync -vzrtopg --delete --progress  $src $user1@$host1::$des1 --password-file=/etc/rsync.password
echo "${files} was rsynced" >> /var/log/rsync.log 2>&1
done

 5、给脚本执行权限

chmod +x /root/inotify.sh 
6、后台执行脚本

/root/inotify.sh &

7、将脚本加入到系统自启文件

echo "/root/inotify.sh" >> /etc/rc.local 
8、向client端加入文件,在server端查看是否有同步

mkdir 11 22 33 44

测试通过

 

三、排除不想同步的文件和目录

1、单个文件排除:比如我不想同步/opt/aa.php文件,直接使用 --exclude “aa.php”

多个文件和目录排除 --exclude-from="/usr/local/src/exclude.list"

脚本写法:

vim /root/inotify.sh

#!/bin/bash
src=/data/client/
des1=server
host1=172.31.82.184
user1=backuser
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -emodify,delete,create,attrib $src | while read file
do
/usr/bin/rsync -vzrtopg --delete --progress --exclude-from="/usr/local/src/exclude.list" $src $user1@$host1::$des1 --password-file=/etc/rsync.password
echo `date +%m.%d.%H%.M`"${files} was rsynced" >> /var/log/rsync.log 2>&1
done

 

2、脚本写完后还需要创建与脚本对应的文件,以下是不同步到server节点的文件和目录:

vim /usr/local/src/exclude.list

exclude
11
22
33

test.php

3、杀死后台运行的脚本进程

ps -elf |pgrep inotify|xargs kill -9

4、启动同步脚本

sh /root/inotify.sh &

5、设置每一个inotify实例相关联的watchs的上限,否则传输的文件过多会报错

echo 30000000 > /proc/sys/fs/inotify/max_user_watches

 

注意:

脚本修改后需要重启后台脚本:

ps -elf |pgrep inotify|xargs kill -9

sh /root/inotify.sh &

本文出自 “david0512” 博客,请务必保留此出处http://gjr0512.blog.51cto.com/6518687/1758117

posted @ 2017-07-18 17:22  wangmo  阅读(503)  评论(0编辑  收藏  举报