Rsync+Sersync 目录双向同步

科长
2023-12-22 / 0 评论 / 50 阅读 / 正在检测是否收录...
服务器1:192.168.5.131 监听(同步)目录:/root/data/upload

服务器2:192.168.5.132 监听(同步)目录:/root/data/upload

安装步骤

1、两台机器安装rsync

yum install rsync -y
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

2、修改机器1的配置

修改 /etc/rsyncd.conf 配置文件

# /etc/rsyncd: configuration file for rsync daemon mode

# See rsyncd.conf man page for more options.

# configuration example:

uid = root
gid = root
use chroot = no
address = 192.168.5.131
port = 873
max connections = 0
pid file = /var/run/rsyncd.pid
log file = /var/log/rsyncd.log
exclude = lost+found/
ignore errors

[update]
path = /root/data/upload
comment = test rsync + sersync
read only = no
list = no
auth users = root
secrets file = /etc/rsync_update.passwd
hosts allow = *

3、修改机器2的配置

修改 /etc/rsyncd.conf 配置文件

# /etc/rsyncd: configuration file for rsync daemon mode

# See rsyncd.conf man page for more options.

# configuration example:

uid = root
gid = root
use chroot = no
address = 192.168.5.132
port = 873
max connections = 0
pid file = /var/run/rsyncd.pid
log file = /var/log/rsyncd.log
exclude = lost+found/
ignore errors

[update]
path = /root/data/upload
comment = test rsync + sersync
read only = no
list = no
auth users = root
secrets file = /etc/rsync_update.passwd
hosts allow = 192.168.5.131 

4、创建虚拟用户

在两台机器上同时执行

#用户名:密码 用户名需要跟  rsyncd.conf 中的 auth users 一致
echo "root:123456789" > /etc/rsync_update.passwd
chmod 600 /etc/rsync_update.passwd

5、启动服务

在两台机器上同时执行

systemctl enable rsyncd
systemctl start rsyncd

6、测试文件同步

在第一台服务器中执行

cd ~/data/upload/
touch 1.txt
# 192.168.5.132 为第二台机器的ip
rsync -av /root/data/upload/ root@192.168.5.132::update
# 输入/etc/rsync_update.passwd 中配置的密码 
# 查看第二台指定目录中是否存在文件

在第二台服务器中执行

cd ~/data/upload/
touch 2.txt
# 192.168.5.131 为第一台机器的ip
rsync -av /root/data/upload/ root@192.168.5.131::update
# 输入/etc/rsync_update.passwd 中配置的密码 
# 查看第一台指定目录中是否存在文件

7、rsync 启动命令

systemctl statr rsyncd   # 启动服务
systemctl stop rsyncd    # 停止服务
systemctl restart rsyncd # 重启服务

8、安装监听器

两台机器都要执行安装 不同之处在于confxml.xml的配置

wget https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/sersync/sersync2.5.4_64bit_binary_stable_final.tar.gz
tar -zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz 
cd GNU-Linux-x86 

修改第一台机器的confxml.xml

<!-- 找到这个节点  -->
<localpath watch="/root/data/upload">
    <remote ip="192.168.5.132" name="update"/>
</localpath>
  • watch:监听的目录
  • remote:要同步的远程服务器
<!-- 找到这个节点  -->
<auth start="true" users="root" passwordfile="/etc/rsync.pas"/>
  • start:远程rsync开启了密码访问
  • users:访问的虚拟用户的用户名
  • passwordfile:密码文件(存放虚拟用户的密码)

修改第二台机器的confxml.xml

<!-- 找到这个节点  -->
<localpath watch="/root/data/upload">
    <remote ip="192.168.5.131" name="update"/>
</localpath>

9、创建密码文件

两台服务均要执行

echo 123456789 > /etc/rsync.pas
chmod 600 /etc/rsync.pas

10、开启监听

/root/GNU-Linux-x86/sersync2 -d -r -o /root/GNU-Linux-x86/confxml.xml
  • -d 以后台daemon的方式运行
  • -r 第一次启动时,使用rsync将本地文件全部同步至远程服务器。
  • -o 加载配置文件
0

评论 (0)

取消