一、安装rsync软件
https://rsync.samba.org/download.html
yum install xxhash* zstd* libzstd* lz4* -y
# tar -zxvf rsync-3.1.1pre1.tar.gz
# ./configure
# make && make install
#echo "/usr/local/lib/">>/etc/ld.so.conf
#/sbin/ldconfig
二、Linux主服务器配置编辑主要配置文件/etc/rsyncd.conf (不存在则自己创建)
vi /etc/rsyncd.conf
uid = root
gid = root
use chroot = no
chroot max connections = 4
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log
[ file ]
path = /home/file
ignore errors
read only = no
list = false
hosts allow = 192.168.1.101,192.168.2.0/24
hosts deny = 0.0.0.0/32
auth users = backup
secrets file = /etc/backup.pass
编辑服务器的密码文件 /etc/backup.pass,文件只能有可读权限 400
vi /etc/backup.pass
backup:123456
chmod 400 /etc/backup.pass
启动rsync
/usr/local/bin/rsync --daemon --config=/etc/rsyncd.conf
指定端口加:--port 8730
/etc/rc.local 增加可自动启动
三、Linux从服务器配置
创建rsync连接时的密码文件/etc/rsync_client.pass,文件权限为只读400
vi /etc/rsync_client.pass
123456
chmod 400 /etc/rsync_client.pass
使用 rsync 命令连接服务器,实现文件同步
MAILTO=""
* * * * * /usr/local/bin/rsync -vzrtopg --progress --delete --password-file=/etc/rsync_client.pass backup@192.168.1.100::file /home/file
指定端口加:--port 8730
参数说明
-vzrtopg 说明
v是verbose,
z是压缩,
r是recursive,
topg都是保持文件原有属性如属主、时间的参数
—-progress
是指显示出详细的进度情况
–delete
是指如果服务器端删除了这一文件,那么客户端也相应把文件删除,保持真正的一致
–exclude=”*.tmp” 不包含某些文件
–execlude-from= 排除不需要同步的目录
--exclude-from=/home/excludelist/excludelist
/home/excludelist/excludelist
ROOT/WEB-INF/logs/
ROOT/export/
ROOT/WEB-INF/imagecache/
ROOT/WEB-INF/jsp/
ROOT/WEB-INF/publish-queue/
windows从服务器配置
https://www.itefix.net/cwrsync
双击 cwrsync[.cmd]进行安装
打开后,将cmd的路径切换到cwrysnc的安装目录的bin目录下,作为工作目录
rsync.exe -vzrtopg --progress --delete --password-file=rsync_client.pass backup@211.66.86.129::file /new
定时更新
https://www.cnblogs.com/janas/p/3321087.html
四、inotify实时同步(主从端依次操作)
yum install epel-release -y
yum install inotify-tools -y
vi /etc/sysctl.conf
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576
sysctl -p
监控脚本
vi /home/inotify.sh
#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e create,delete,modify,attrib,move,close_write /home/wpcms/wp-content/"
RSYNC_CMD="/usr/local/bin/rsync -vzrtopg --progress --delete --exclude="*.swp" --exclude="*.swx" --password-file=/etc/rsync_client.pass /home/wpcms/wp-content/ backup@10.168.4.200::content"
#读取输出的监控记
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
do
#如果rsync未在执行,则立即启动
if [ $(pgrep rsync | wc -l) -ge 1 ]; then
$RSYNC_CMD
fi
done
加入到自动启动
echo '/home/inotify.sh >/dev/null 2>&1 &' >> /etc/rc.local
再重复设置另一端就能实现双向实时同步