centos 7.9 两台服务器同步目录数据互相同步
rsync + sersync2
两台数据同步服务器IP地址
rsync-1 192.168.5.128
rsync-2 192.168.5.129
两台数据同步服务器目录
/root/updatedir
一、两台服务器配置rsyncd
1、两台服务器都安装rsync服务
yum install rsyncd -y
2、rsync-1修改rsyncd配置文件
[root@rsync-1 ~]# cat /etc/rsyncd.conf
uid = root # 指定运行rsync daemon的用户
gid = root # 指定运行rsync daemon的组
use chroot = no
address = 192.168.5.128 # 指定监听地址
port = 873 # 默认监听端口
max connections = 0 # 最大连接数,0为无限制
pid file = /var/run/rsyncd.pid # 指定pid文件
log file = /var/log/rsyncd.log # 指定日志文件
exclude = lost+found/ # 指定不同步的目录
ignore errors
[update] # update模块,如果数据同步目录有多个,可以复制该模块
path = /root/updatedir # update模块需要同步目录
comment = rsync + sersync # update模块的简要说明
read only = no # 是否只读
list = no # 当用户查询可用模块时是否列出该模块
auth users = rsync_daemon # 同步文件使用到的虚拟用户
secrets file = /etc/rsync_update.passwd # 虚拟用户密码文件,权限为(400)
hosts allow = 192.168.5.129 # 指定可以连接该模块的主机
3、rsync-2修改rsyncd配置文件
[root@rsync-2 ~]# cat /etc/rsyncd.conf
uid = root # 指定运行rsync daemon的用户
gid = root # 指定运行rsync daemon的组
use chroot = no
address = 192.168.5.129 # 指定监听地址
port = 873 # 默认监听端口
max connections = 0 # 最大连接数,0为无限制
pid file = /var/run/rsyncd.pid # 指定pid文件
log file = /var/log/rsyncd.log # 指定日志文件
exclude = lost+found/ # 指定不同步的目录
ignore errors
[update] # update模块
path = /root/updatedir # update模块需要同步目录
comment = test rsync + sersync # update模块的简要说明
read only = no # 是否只读
list = no # 当用户查询可用模块时是否列出该模块
auth users = rsync_daemon # 同步文件使用到的虚拟用户
secrets file = /etc/rsync_update.passwd # 虚拟用户密码文件,权限为(400)
hosts allow = 192.168.5.128 # 指定可以连接该模块的主机
4、两台服务器都创建数据同步目录
mkdir /root/updatedir
echo "rsync_daemon:123456789" > /etc/rsync_update.passwd
chmod 400 /etc/rsync_update.passwd
5、两台服务器都启动服务
systemctl start rsyncd && systemctl enable rsyncd
ss -antuple | grep 873
touch /root/updatedir/file{1..5} && ls /root/updatedir/
6、开机启动并测试
vim /usr/lib/systemd/system/rsyncd.service
...
[Service]
EnvironmentFile=/etc/sysconfig/rsyncd
# 开机延迟启动,因为rsync配置文件的IP地址要先启动
ExecStartPre=/bin/sleep 20
ExecStart=/usr/bin/rsync --daemon --no-detach "$OPTIONS"
...
systemctl daemon-reload rsyncd
systemctl enable rsyncd
rsync-1
rsync -av /root/updatedir/ rsync_daemon@192.168.5.129::update
rsync-2
rsync -av /root/updatedir/ rsync_daemon@192.168.5.128::update
二、两台服务器配置sersync2
1、两台服务器下载安装包并解压
wget https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/sersync/sersync2.5.4_64bit_binary_stable_final.tar.gz
tar zxf sersync2.5.4_64bit_binary_stable_final.tar.gz -C /root/
cp -arp /root/GNU-Linux-x86/confxml.xml /root/GNU-Linux-x86/confxml.xml.bak
2、rsync-1修改sersync2配置文件
vim /root/GNU-Linux-x86/confxml.xml # 修改数据同步目录,对端IP地址和用户名密码。如果数据同步目录有多个,可以新建多个confxml.xml文件,启动的时候指定对应的目录。
...
24
25
...
31
...
echo 123456789 > /etc/rsync.pas
chmod 400 /etc/rsync.pas
3、rsync-2修改sersync2配置文件
vim /root/GNU-Linux-x86/confxml.xml # 修改数据同步目录,对端IP地址和用户名密码
...
24
25
...
31
...
echo 123456789 > /etc/rsync.pas
chmod 400 /etc/rsync.pas
4、启动服务
[root@rsync-1 GNU-Linux-x86]# /root/GNU-Linux-x86/sersync2 -d -r -o /root/GNU-Linux-x86/confxml.xml
[root@rsync-2 GNU-Linux-x86]# /root/GNU-Linux-x86/sersync2 -d -r -o /root/GNU-Linux-x86/confxml.xml
开机自启
vi /etc/rc.d/rc.local
# 开机延迟启动,因为rsync配置文件的IP地址要先启动
/bin/sleep 30 && /root/GNU-Linux-x86/sersync2 -d -r -o /root/GNU-Linux-x86/confxml.xml
chmod +x /etc/rc.d/rc.local