server(同步/vm/template,排除/vm/vm/):
#!/bin/bash
#test rsync exist
/usr/bin/dpkg -l | /bin/grep -P rsync
if [ X"$?" != X"0" ];then
/usr/bin/apt-get install rsync -y
fi
#iptables -I INPUT 6 -p tcp --dport 873 -j ACCEPT
#create rsyncd.conf rsyncd.motd rsyncd.secrets
if [ ! -e /etc/rsyncd.conf && ! -e /etc/rsyncd.motd && ! -e /etc/rsyncd.secrets ];then
echo "file exist"
else
cd /etc/
if [ X"$?" != X"0" ];then
exit
else
cat >> /etc/rsyncd.conf << "EOF"
# This line is required by the /etc/init.d/rsyncd script
pid file = /var/run/rsyncd.pid
port = 873
#address = 127.0.0.1
#uid = nobody
#gid = nobody
uid = root
gid = root
use chroot = yes
read only = no
wirte only = no
#limit access to private LANs
#hosts allow=192.168.1.0/255.255.255.0 10.0.1.0/255.255.255.0
#hosts deny=*
#max connections = 5
motd file = /etc/rsyncd.motd
#This will give you a separate log file
#log file = /var/log/rsync.log
#This will log every file transferred - up to 85,000+ per user, per sync
#transfer logging = yes
#log format = %t %a %m %f %b
#syslog facility = local3
#timeout = 300
[vmTemplate]
path = /vm/
list = yes
ignore errors
auth users = root
secrets file = /etc/rsyncd.secrets
comment = This is Ubuntu template server
exclude = vm/
EOF
cat >> /etc/rsyncd.motd << "EOF"
++++++++++++++++++++++++++++++++++
Welcome to use the rsync services!
++++++++++++++++++++++++++++++++++
EOF
cat >> /etc/rsyncd.secrets << "EOF"
root:YHdfd8&?.%W
EOF
chown root.root /etc/rsyncd.secrets
chmod 600 /etc/rsyncd.secrets
fi
fi
rsync --daemon
netstat -naop | grep -q 873 && netstat -naop | grep -q rsync
if [ X"$?" = X"0" ];then
echo "daemon ok"
fi
client(自动向服务实时同步数据):
内核必须2.6.13以上
安装inotify-tools-3.14.tar.gz:
wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
tar inotify-tools-3.14.tar.gz
cd inotify-tools-3.14
./configure
make && make install
ldconfig
echo "YHdfd8&?.%W" > /etc/rsyncd.secrets
chmod 600 /etc/rsyncd.secrets
less inotifyrsync.sh
#!/bin/bash
hostip=
src=/vm/template
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e close_write,modify,delete,create,attrib $src | while read files
do
rsync -azvP --delete --password-file=/etc/rsyncd.secrets $src root@$hostip::vmTemplate
echo "${files} was rsynced" >> /tmp/rsync.log 2>&1
done
chmod 755 inotifyrsync.sh
./inotifyrsync.sh &
本文转自 freeterman 51CTO博客,原文链接:http://blog.51cto.com/myunix/1401982,如需转载请自行联系原作者