Centos系统使用rsyn实现双主机附件目录实时同步
A主机:192.168.2.18,B主机:192.168.2.20;目录:/data/{dira,dirb}
数据同步os用户名:hhdb,A、B hhdb用户需要配置免密
1.安装rsyn\lsyncd
yum -y install rsync lsyncd
2.A服务器配置
2.1 /etc/lsyncd.conf
配置文件
vi /etc/lsyncd.conf
----
-- User configuration file for lsyncd.
--
-- Simple example for default rsync, but executing moves through on the target.
--
-- For more examples, see /usr/share/doc/lsyncd*/examples/
--
-- sync{default.rsyncssh, source="/var/www/html", host="localhost", targetdir="/tmp/htmlcopy/"}
settings {
logfile ="/var/log/lsyncdA.log",
statusFile ="/usr/local/lsyncdA.status",
inotifyMode = "CloseWrite",
maxProcesses = 8,
-- nodaemon =true,
}
sync {
default.rsync,
source = "/data/dira",
target = "rsync_backup@192.168.2.20::backup",
rsync = {
binary = "/usr/bin/rsync",
password_file = "/etc/rsyncA.password",
archive = true,
compress = true,
verbose = true
}
}
创建虚拟用户密码文件
[root@nfs ~]# touch /etc/rsyncA.password
[root@nfs ~]# echo "1" > /etc/rsyncA.password
chmod 600 /etc/rsync.password
设置服务开机自启
[root@nfs ~]# systemctl restart lsyncd
[root@nfs ~]# systemctl enable lsyncd
2.2配置 /etc/rsyncd.conf
uid = hhdb
gid = hhdb
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 300
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
read only = false
list = false
auth users = rsync_backup
secrets file = /etc/rsyncB.password
[backup]
comment = "backup"
path = /data/dirb
创建密码文件
[root@rsync ~]# touch /etc/rsyncB.password
[root@rsync ~]# echo "rsync_backup:1" >/etc/rsyncB.password
[root@rsync ~]# chmod 600 /etc/rsyncB.password
加入开机自启
[root@rsync ~]# systemctl restart rsyncd
[root@rsync ~]# systemctl enable rsyncd
3.B服务器配置
3.1配置 /etc/rsyncd.conf
uid = hhdb
gid = hhdb
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 300
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
read only = false
list = false
auth users = rsync_backup
secrets file = /etc/rsyncA.password
[backup]
comment = "backup"
path = /data/dira
创建密码文件
[root@rsync ~]# touch /etc/rsyncA.password
[root@rsync ~]# echo "rsync_backup:1" >/etc/rsyncA.password
chmod 600 /etc/rsyncA.password
加入开机自启
[root@rsync ~]# systemctl restart rsyncd
[root@rsync ~]# systemctl enable rsyncd
3.2配置/etc/lsyncd.conf
配置文件
vi /etc/lsyncd.conf
----
-- User configuration file for lsyncd.
--
-- Simple example for default rsync, but executing moves through on the target.
--
-- For more examples, see /usr/share/doc/lsyncd*/examples/
--
-- sync{default.rsyncssh, source="/var/www/html", host="localhost", targetdir="/tmp/htmlcopy/"}
settings {
logfile ="/var/log/lsyncdB.log",
statusFile ="/usr/local/lsyncdB.status",
inotifyMode = "CloseWrite",
maxProcesses = 1,
-- nodaemon =true,
}
sync {
default.rsync,
source = "/data/dirb",
target = "rsync_backup@192.168.2.18::backup",
rsync = {
binary = "/usr/bin/rsync",
password_file = "/etc/rsyncB.password",
archive = true,
compress = true,
verbose = true
}
}
创建虚拟用户密码文件
[root@nfs ~]# touch /etc/rsyncB.password
[root@nfs ~]# echo "1" > /etc/rsyncB.password
chmod 600 /etc/rsyncB.password
设置服务开机自启
[root@nfs ~]# systemctl restart lsyncd
[root@nfs ~]# systemctl enable lsyncd