Rsync+inotify文件实时同步

Rsync+inotify文件传输同步
目的:把主服务器192.168.2.132上的文件实时同步到备服务器192.168.2.133上。
使用inotify实时监控主服务器/servyou/webroot目录变化,并通过rsync把改变的部分推送给备服务器。
主备服务器安装rsync
主服务器(数据源)安装inotify
1、ssh认证生成密钥对
# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
c2:81:43:6b:4f:cf:03:60:64:b2:d7:04:b6:65:80:f7 root@servyouhome
一路回车,不做任何修改
2、ssh认证copy公钥到备份服务器192.168.2.133
# ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.2.133
输入yes,一路回车
The authenticity of host '192.168.2.133 (192.168.2.133)' can't be established.
RSA key fingerprint is 67:82:61:72:fa:37:06:3f:8e:03:3c:63:ac:1b:d1:d4.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.2.133' (RSA) to the list of known hosts.
root@192.168.2.133's password:
Now try logging into the machine, with "ssh 'root@192.168.2.133' ", and check in:
.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.
3、主备服务器安装rsync
#wget  http://rsync.samba.org/ftp/rsync/src/rsync-3.0.9.tar.gz
#tar  -zxvf  rsync-3.0.9.tar.gz
#cd  rsync-3.0.9
#./configure
#make
#make  install
4、主备服务器以守护进程启动rsync,并设置开机启动
# rsync  --daemon
#chkconfig  rsync  on
5、主服务器(源数据端)安装inotify
#wget  http://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
#tar  -zxvf  inotify-tools-3.14.tar.gz
#cd  inotify-tools-3.14
#./configure
#make
#make  install
6、备服务器配置rsync
#vim  /etc/rsyncd.conf
uid = root
gid = root
use chroot = no
max connections = 10
strict modes = yes
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync/lock
log file = /var/log/rsync.log 
[webroot]
path = /servyou/webroot/
comment = webroot files
ignore errors
read only = no
write only = no
hosts allow = 192.168.2.0/255.255.255.0
#hosts deny = 0.0.0.0/32
list = false
uid = root
gid = root
#auth users = backup
#secrets file = /etc/server.pass
7、在主服务器配置内容发布节点
#!/bin/bash
SRC=/servyou/webroot/
DES=root@192.168.2.133:/servyou/webroot/
HOST=192.168.2.133
/usr/local/bin/inotifywait  -mrq --timefmt '%Y-%m-%d %H:%M:%S' --format  '%T %w%f%e'  -e modify,close_write,delete,create,attrib $SRC | while read files
     do
         /usr/bin/rsync -azvl --delete --progress --exclude-from=/servyou/backup/exclude.list  $SRC $DES
         echo "${files}  was rsynced" >> /var/log/rsync.log 2>&1
     done
将这个脚本命名为rsync.sh,放到/servyou/目录下,然后指定可执行权限,放到后台运行,如下:
#chmod  755  /servyou/rsync.sh
#nohup   /servyou/rsync.sh  & > /dev/null 2>&1
#echo  “nohup  /servyou/rsync.sh  & ”  >> /etc/rc.local
  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值