rsync+inotify 实现服务器目录自动同步(centos)

使用rsync+inotify的意义:
其实同步服务器之间的目录,只需要安装rsync并通过脚本就可以做到定时同步。但是采用inotify是为了做到更精确的同步,因为inotify是一个文件监控工具,它会对指定文件的状态进行实时监控输出。实时同步的逻辑就是通过inotify对同步内容监控,然后通过记录来使用rsync命令实现实时传输。
本文章只对操作过程做详细记录,具体功能一概而过。
模拟环境:
将服务器1中的a目录,b目录实时同步到服务器2中的c目录,d目录。a→c b→d
服务器1 IP:172.16.1.20 a目录:/alidata/htmlService/ b目录: /alidata/phpService/
服务器2 IP:172.16.8 c目录:/alidata/htmlService/ d目录: /alidata/phpService/
操作注意事项:
*建议用root用户进行安装

安装rsync,测试手动同步

服务器2:
1.安装rsync

yum -y install rsync

2.添加pass文件,pass文件名和位置都可自定义

vim /root/rsync.pass
user:password           #自定义rsync用户名和密码,中间冒号隔开
保存退出
chmod 600 /root/rsync.pass  #修改文件权限,必须要600

3.打开/etc/rsyncd.conf,修改如下:

port = 873      										#rsync服务端口
log file = /var/log/rsync/rsyncd.log
pid file = /var/log/rsync/rsyncd.pid

[htmlService]					                        #自定义模块名称,一个目录对应一个模块
path = /alidata/htmlService/							#被同步的目录,c目录
uid = root												#使用root用户执行rsync
gid = root
read only = no
write only = no
auth users = nginx_user
secrets file = /root/rsync.pass 			            #pass文件位置
hosts allow = 172.16.1.20                               #允许访问的IP,服务器1

[phpService]
path = /alidata/phpService/
uid = root
gid = root
read only = no
write only = no
auth users = nginx_user
secrets file = /root/rsync.pass
hosts allow = 172.16.1.20

4.启动rsync服务,并添加自启动

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

服务器1
1.安装xinet,rsync,inotify

yum -y install rsync xinet

2.添加pass文件,输入服务器1中设置的密码

vim /root/rsync.pass
password  
保存退出
chmod 600 /root/rsync.pass  #修改文件权限,必须要600

3.测试rsync命令是否成功

执行命令:
rsync -avpgolr --progress --delete /alidata/htmlService/ nginx_user@172.16.1.8::nginx --password-file=/root/rsync.pass

/alidata/htmlService/	#服务器要传输的文件目录
user                    #pass文件用户名
172.16.1.8		    
htmlService  	        #rsyncd.conf文件中模块名称
--password-file=/root/rsync.pass    :pass文件位置

安装配置inotify

服务器1
1.安装inotify

wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
tar -zxvf inotify-tools-3.14.tar.gz
./configure
make && make install

2.手动执行inotifywait命令,监控/alidata/htmlService/目录

/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H%M' --format '%T %w%f%e' -e close_write,modify,delete,create,attrib,move /alidata/htmlService/

3.再打开一个终端,运行rsync命令,检查inotifywait命令是否输出操作历史

rsync -avpgolr --progress --delete /alidata/htmlService/ nginx_user@172.16.1.8::nginx --password-file=/root/rsync.pass

4.以上成功后,就可以用下面脚本做到实时同步

htmlService(){
srdir="/alidata/htmlService/"             # 同步的文件夹
user="nginx_user"                         # pass文件用户名
ip="172.16.1.8"           
pass_file="/home/ngariPro/rsync.pass"     #pass文件位置
module_name="htmlService"                 #模块名称
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H%M' --format '%T %w%f%e' -e close_write,modify,delete,create,attrib,move $srdir | while read file
do
rsync -avpgolr --progress --delete $srdir $user@$ip::$module_name --password-file=$pass_file
echo "  ${file} was rsynced" >> /alidata/logs/inotify/inotify.log 2>&1
done
}
phpService(){
srdir="/alidata/phpService/"             #同步的文件夹
user="nginx_user"                        #pass文件用户名
ip="172.16.1.8"                      
pass_file="/home/ngariPro/rsync.pass"    #pass文件位置
module_name="phpService"                 #模块名称
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H%M' --format '%T %w%f%e' -e close_write,modify,delete,create,attrib,move $srdir | while read file
do
rsync -avpgolr --progress --delete $srdir $user@$ip::$module_name --password-file=$pass_file
echo "  ${file} was rsynced" >> /alidata/logs/inotify/inotify.log 2>&1
done
}
htmlService
phpService

5.将脚本放在后台运行,添加自启动

nohup sh /root/xxx.sh &
echo “nohup sh  /root/xxx.sh &” >>/etc/rc.local

以上完成!
备注:
rsync和inotify如需要排除某些文件或者文件夹可用 --exclude 参数,例如:

/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H%M' --format '%T %w%f%e' --exclude '.*.log' -e close_write,modify,delete,create,attrib,move /tmp/test/

--exclude '.*.log'       #指所有的含log的文件
rsync -avpgolr --progress --delete --exclude "*.log" --exclude "ex_file" $srdir $user@$ip::$module_name --password-file=pass_file

--exclude "*.log" --exclude "ex_file"     #可通过多个exclude指定多个文件/文件夹

注意:
inotifywait只支持一个exclude,如需要多个通过(.log|.file)格式。
--exclude参数指定文件时候,文件路径一定要相对路径,绝对路径无法成功。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值