Rsync 远程同步

发布时间 2023-10-05 20:04:48作者: Candy独角兽

192.168.175.108  配置rsync源服务器

192.168.175.109  配置rsync源服务器

192.168.175.110  发起端配置 rsync+inotify

1. 配置 rsync 源服务器

1.1 191.168.175.108

vim /etc/rsyncd.conf

uid = root
gid = root
use chroot = yes
max connections = 4
address = 192.168.175.108
port = 873
pid file = /var/run/rsyncd.pid
log file = /var/log/rsyncd.log
dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
hosts allow = 192.168.175.0/24

[wwwroot]
path = /data
comment = Document Root of www
read only = yes
auth users = backuper
secrets file = /etc/rsyncd_users.db

vim /etc/rsyncd_users.db
cat /etc/rsyncd_users.db 


chmod 600 /etc/rsyncd_users.db 
mkdir /data
chmod +r /data
ll -d /data
rsync --daemon
netstat -lnutp | grep rsync

1.2 191.168.175.109

步骤同192.168.175.108

2. 发起端配置 rsync+inotify

  • 准备好安装包inotify-tools-3.14.tar.gz
cd /opt
tar xf inotify-tools-3.14.tar.gz
ls
cd inotify-tools-3.14/
./configure && make && make insatll

cat /proc/sys/fs/inotify/max_queued_events
cat /proc/sys/fs/inotify/max_user_instances
cat /proc/sys/fs/inotify/max_user_watches
vim /etc/sysctl.conf
sysctl -p
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576

cd
vim inotify.sh
#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e modify,create,attrib,move,delete /data"
RSYNC_CMD="rsync -azH --delete --password-file=/etc/server.pass /data/ backuper@192.168.175.108::wwwroot/"
RSYNC_CMD2="rsync -azH --delete --password-file=/etc/server.pass /data/ backuper@192.168.175.109::wwwroot/"
#使用while、read持续获取监控结果,根据结果可以作进一步判断是否读取到输出的监控记录
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
do
    if [ $(pgrep rsync | wc -l) -le 0 ] ; then
        #如果rsync未在执行,则立即启动
        $RSYNC_CMD
        $RSYNC_CMD2
    fi
done

echo abc123 > /etc/server.pass
chmod +x inotify.sh 
mkdir /data
chmod 777 /data
cd /data
ls
cp /var/log/messages /data
ls
messages
cd
cd -
rm -rf messages 
cd -


vim server.pass
chmod 600 /etc/server.pass 
cd
./inotify.sh 

cd /data
ls
cp /var/log/messages /data
ls
echo a > a
ls

  1. 配置rsync源服务器
vim /etc/rsyncd.conf
read only = no

netstat -anpt | grep rsync       
kill 2044
rm -rf /var/run/rsyncd.pid
rsync --daemon
netstat -anpt | grep rsync         
chmod 777 /data
cd /data
ls