rsync+inotify-tools实时同步
官方网站:http://rsync.samba.org/
https://download.samba.org/pub/rsync/rsync.html
https://download.samba.org/pub/rsync/rsyncd.conf.html
https://download.samba.org/pub/rsync/src/rsync-3.1.1.tar.gz
http://sourceforge.net/projects/inotify-tools/
https://github.com/rvoicilas/inotify-tools/wiki
操作系统: CentOS6.5 x64
server1.example.com:192.168.192.101(rsync client+inotify-tools)
server2.example.com:192.168.192.102(rsync server)
server3.example.com:192.168.192.103(rsync server)
简介:inotify是内核的一个监控文件变化的模块,提供监控文件变化的API,需要安装inotify-tools工具来调用这个API 。inotify用来监控需要同步目录发生的变化,可以实现文件的新增,删除,修改,改变属性等,功能很强大。
一.目标服务器(server2,server3)
说明:被同步的服务器,接收者
1.安装rsync
serveryum -y install gcc gcc-c++
wget
tar -xvf /mnt/samba/rsync-3.1.1.tar.gz -C /usr/local/src/
cd /usr/local/src/rsync-3.1.1/
./configure && make -j4 && make install
2.配置
1.全局配置文件/etc/rsyncd.conf
cat >/etc/rsyncd.conf <<HERE
uid = nobody
gid = nobody
port = 873
hosts allow = server1.example.com
hosts deny = *
max connections = 100
timeout= 300
use chroot = no
list = false
read only = no
syslog facility = local5
transfer logging = yes
##config file
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
#motd file = /etc/rsyncd.motd
##global config
[web]
path = /var/www/html
comment = Sync the web root
ignore errors = yes
read only = no
write only = no
hosts allow = server1.example.com
hosts deny = *
list = false
auth users = apache foo
secrets file = /etc/rsyncd.secrets
[image]
path = /var/ftp/pub/image
comment = Images
HERE
官方示例:auth users = joe:deny @guest:deny admin:rw @rsync:ro susan joe sam
2.rsync client/server通信口令
cat >/etc/rsyncd.secrets <<HERE
apache:myapache
HERE
chmod 600 /etc/rsyncd.secrets
注意:other不能有r权限
3.banner(可选)
echo"Welcome > /etc/rsyncd.motdto use the rsync services"
4.开机启动
echo "/usr/local/bin/rsync --daemon --config=/etc/rsyncd.conf" >>/etc/rc.local
source /etc/rc.local
二.分发服务器(server1)
1.rsync client
yum -y install gcc gcc-c++
wgethttps://download.samba.org/pub/rsync/src/rsync-3.1.1.tar.gz /root/test/),则只同步目录中的文件;
tar -xvf /mnt/samba/rsync-3.1.1.tar.gz -C /usr/local/src/
cd /usr/local/src/rsync-3.1.1/
./configure && make -j4 && make install
2.inotify-tools
tar -xvf /mnt/samba/inotify-tools-3.13.tar.gz -C /usr/local/src/
cd /usr/local/src/inotify-tools-3.13/
./configure && make -j4 && make install
3.监控脚本
#############################################
#!/bin/sh
#To rsync real time automatically
#############################################
#Define variables
dest_hosts="server2.example.com"
dest_module="web"
password_file="/etc/rsyncd.secrets"
exclude_list="/etc/rsyncd.exclude"
auth_user1="apache"
auth_user2=""
source_dir1="/root/test/"
source_dir2=""
inotifywait_bin="/usr/local/bin/inotifywait"
rsync_log="/var/log/rsync.log"
#Rsync
for dest_host in $(echo $dest_hosts)
do
source_dir=$source_dir1
auth_user=$auth_user1
$inotifywait_bin -mr --timefmt '%Y/%m/%d %H:%M:%S' --format '%T %w %f' -e close_write,modify,delete,create,attrib $source_dir1|
while read DATE TIME DIR FILE
do
filechanged=$DIR$FILE
rsync -auzS --delete --ignore-errors --exclude-from=$exclude_list --password-file=$password_file $source_dir rsync://$auth_user@$dest_host:$dest_port/$dest_module 2>/dev/null
echo -e "At e[31;1m$TIMEe[0m on e[34;1m$DATEe[0m, file e[32;1m$filechangede[0m was rsynced to e[33;1m$dest_hoste[0m"|tee -a $rsync_log
done
done
注意:源路径的最后有斜杠(
没有斜杠(/root/test),则不但会同步目录中的文件,还会同步目录本身
4.rsync client/server通信口令
cat >/etc/rsyncd.secrets <<HERE
myapache
HERE
chmod 600 /etc/rsyncd.secrets
注意:和目标服务器的通信口令不一样,只有密码,没有用户名三.测试