源服务器 | 192.168.137.101 |
---|---|
目标服务器 | 192.168.137.102 |
Linux系统
[root@shamolang ~]# uname -a
Linux shamolang 3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 23:39:32 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
需要开放873端口
firewall-cmd --permanent --add-port=873/tcp
firewall-cmd --reload
安装Rsync服务
yum install rsync xinetd -y
已安装:
rsync.x86_64 0:3.1.2-6.el7_6.1 xinetd.x86_64 2:2.3.15-13.el7
以守护进程运行
rsync --daemon
查看端口是否正常工作
netstat -anpt | grep 873
[root@shamolang ~]# netstat -anpt | grep 873
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 4189/rsync
tcp6 0 0 :::873 :::* LISTEN 4189/rsync
创建Rsync工作用户
useradd rget1;echo rget1:123456|chpasswd
以上是服务的基本安装,两台服务器都需要执行一遍,下面来模拟使用系统用户同步数据
192.168.137.101
创建同步目录
mkdir -p /var/www/html
使rget1用户获得同步目录777权限
setfacl -R -m u:rget1:rwx /var/www/html/
setfacl -R -m default:rget1:rwx /var/www/html/
使同步目录有数据
cp -rf /etc/b* /var/www/html/
[root@shamolang ~]# cd /var/www/html/
[root@shamolang html]# ls
bash_completion.d bashrc binfmt.d
192.168.137.102
创建待同步目录
mkdir /web-back
使待同步目录所属人和所属组更改为rget1
chown rget1:rget1 -R /web-back/
192.168.137.101
执行推命令,推出数据
rsync -avz --delete /var/www/html/ rget1@192.168.137.102:/web-back
拉取成功
[root@shamolang ~]# rsync -avz --delete /var/www/html/ rget1@192.168.137.102:/web-back
The authenticity of host '192.168.137.102 (192.168.137.102)' can't be established.
ECDSA key fingerprint is SHA256:m5s3MutFAhVN8bVIKEMVWFJT+JQCOCkdCVxoQfNNueo.
ECDSA key fingerprint is MD5:54:e7:29:9b:17:f9:1e:06:ac:97:34:f1:46:65:7a:16.
Are you sure you want to continue connecting (yes/no)? yes #表示同意
Warning: Permanently added '192.168.137.102' (ECDSA) to the list of known hosts.
rget1@192.168.137.102's password: #输入rget1密码“123456”
sending incremental file list
./
bashrc
bash_completion.d/
bash_completion.d/iprutils
binfmt.d/
sent 1,932 bytes received 69 bytes 235.41 bytes/sec
total size is 3,682 speedup is 1.84
192.168.137.102
同步完成
[root@shamolang var]# cd /web-back/
[root@shamolang web-back]# ls
[root@shamolang web-back]# ls
bash_completion.d bashrc binfmt.d
下面模拟使用非系统用户同步数据
192.168.137.102
编辑Rsync配置文件
vim /etc/rsyncd.conf
uid = root #用户ID
gid = root #用户组ID
address = 192.168.137.102 #源服务器IP
port = 873 #监听端口
hosts allow = 192.168.137.0/24 #仅允许此网段访问
use chroot = yes #禁锢家目录
max connections = 10 #最大并发连接数,默认0(无限制)
strict modes=yes #是否检查口令文件的权限,yes时必须为root用户权限
pid file = /var/run/rsyncd.pid #守护进程PID路径
lock file = /var/run/rsync.lock #指定支持max connections的锁文件
log file = /var/run/rsyncd.log #日志输出文件路径
motd file = /etc/rsyncd.motd #指定消息文件,当客户连接服务器时给文件的内容显示给客户,默认没有该文件
[www] #模块名称
path = /web-back/ #待同步目录
comment = ftp area #描述文字
ignore errors #可以忽略一些无关的I/O错误
read only = false #false:客户端可以上传文件 yes:只读
write only = false #false:客户端可以下载文件 yes:不能下载
list = yes #客户端请求可以使用的模块列表时,该模块是否被列出
auth users = backup #用户名backup,与系统用户没有任何关系
secrets file = /etc/server.pass #只有定义了auth users项,该文件才起作用,默认没有此文件,须手动创建(文件格式为:"用户名:密码")
创建motd文件
echo "welcome to backup server" > /etc/rsyncd.motd #引号可以自定义,在同步时会显示你自定义的内容
创建密码文件(文件格式:user:pass)
vim /etc/server.pass
backup:123456
修改密码文件权限
chmod 600 /etc/server.pass
终止Rsync进程
pkill -9 rsync
[root@shamolang web-back]# systemctl start xinetd #启动管理服务
[root@shamolang web-back]# systemctl enable xinetd #假如开机启动项
[root@shamolang web-back]# rsync --daemon --config=/etc/rsyncd.conf #启动Rsync
[root@shamolang web-back]# netstat -anpt | grep 873
tcp 0 0 192.168.137.102:873 0.0.0.0:* LISTEN 19979/rsync
192.168.137.101
rsync -avz --delete /var/www/html/ backup@192.168.137.102::www #www表示模块名称
同步完成
[root@shamolang html]# rsync -avz --delete /var/www/html/ backup@192.168.137.102::www
welcome to backup server #上文自定义的位置
Password: #/etc/server.pass中的密码
sending incremental file list
./
bashrc
bash_completion.d/
bash_completion.d/iprutils
binfmt.d/
sent 1,940 bytes received 73 bytes 575.14 bytes/sec
total size is 3,682 speedup is 1.83
下面模拟无密码交互传输
192.168.137.101
vim /etc/rsync.pass
123456
文件rsync.pass加权600
chmod 600 /etc/rsync.pass
rsync -avz --delete /var/www/html/ backup@192.168.80.101::wwwroot --password-file=/etc/rsync.pass
同步完成
[root@shamolang html]# rsync -avz --delete /var/www/html/ backup@192.168.137.102::www --password-file=/etc/rsync.pass
welcome to backup server
#没有输入密码
sending incremental file list
./
bashrc
bash_completion.d/
bash_completion.d/iprutils
binfmt.d/
sent 1,940 bytes received 73 bytes 4,026.00 bytes/sec
total size is 3,682 speedup is 1.83
下面模拟定时同步数据
192.168.137.101
编写同步脚本
vim autobackup.sh
#!/bin/bash
rsync -avz --delete /var/www/html/ backup@192.168.137.102::www --password-file=/etc/rsync.pass
脚本加入执行权限
chmod +x autobackup.sh
添加计划任务
echo '* * * * * /usr/bin/sh /root/autobackup.sh &' >> /var/spool/cron/root #每分钟执行一次同步命令
等待一分钟会有一封邮件
[root@shamolang ~]#
您在 /var/spool/mail/root 中有邮件
定时同步完成
[root@shamolang ~]# cat /var/spool/mail/root
From root@shamolang.localdomain Thu Aug 1 18:40:02 2019
Return-Path: <root@shamolang.localdomain>
X-Original-To: root
Delivered-To: root@shamolang.localdomain
Received: by shamolang.localdomain (Postfix, from userid 0)
id 9F180200112E; Thu, 1 Aug 2019 18:40:02 +0800 (CST)
From: "(Cron Daemon)" <root@shamolang.localdomain>
To: root@shamolang.localdomain
Subject: Cron <root@shamolang> /usr/bin/sh /root/autobackup.sh &
Content-Type: text/plain; charset=UTF-8
Auto-Submitted: auto-generated
Precedence: bulk
X-Cron-Env: <XDG_SESSION_ID=4>
X-Cron-Env: <XDG_RUNTIME_DIR=/run/user/0>
X-Cron-Env: <LANG=zh_CN.UTF-8>
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/root>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=root>
X-Cron-Env: <USER=root>
Message-Id: <20190801104002.9F180200112E@shamolang.localdomain>
Date: Thu, 1 Aug 2019 18:40:02 +0800 (CST)
welcome to backup server
sending incremental file list
sent 174 bytes received 14 bytes 376.00 bytes/sec
total size is 3,682 speedup is 19.59
From root@shamolang.localdomain Thu Aug 1 18:41:01 2019
Return-Path: <root@shamolang.localdomain>
X-Original-To: root
Delivered-To: root@shamolang.localdomain
Received: by shamolang.localdomain (Postfix, from userid 0)
id E15BE200112E; Thu, 1 Aug 2019 18:41:01 +0800 (CST)
From: "(Cron Daemon)" <root@shamolang.localdomain>
To: root@shamolang.localdomain
Subject: Cron <root@shamolang> /usr/bin/sh /root/autobackup.sh &
Content-Type: text/plain; charset=UTF-8
Auto-Submitted: auto-generated
Precedence: bulk
X-Cron-Env: <XDG_SESSION_ID=5>
X-Cron-Env: <XDG_RUNTIME_DIR=/run/user/0>
X-Cron-Env: <LANG=zh_CN.UTF-8>
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/root>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=root>
X-Cron-Env: <USER=root>
Message-Id: <20190801104101.E15BE200112E@shamolang.localdomain>
Date: Thu, 1 Aug 2019 18:41:01 +0800 (CST)
welcome to backup server
sending incremental file list
sent 174 bytes received 14 bytes 376.00 bytes/sec
total size is 3,682 speedup is 19.59
下面配置Sersync+Rsync实现实时同步
192.168.137.101
关闭Rsync服务
pkill -9 rsync
下载Sersync服务包
wget https://github.com/wsgzao/sersync/raw/master/sersync2.5.4_64bit_binary_stable_final.tar.gz
解压
tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz -C /opt/
复制
cp -rf GNU-Linux-x86/ /usr/local/sersync/
[root@shamolang sersync]# ls
confxml.xml sersync2
备份配置文件(以备不测)
cp confxml.xml confxml.xml.bak
编辑配置文件
24行 localpath watch="/var/www/html" #同步目录
25行 remote ip=“192.168.137.102” name=“www” / #待同步地址,模块名称
31行 auth start=“true” users=“backup” passwordfile="/etc/rsync.pass" /
vim confxml.xml
24 <localpath watch="/var/www/html/">
25 <remote ip="192.168.137.102" name="www"/>
31 <auth start="true" users="backup" passwordfile="/etc/rsync.pass"/>
启动服务
./sersync2 -d -r -o ./confxml.xml
set the system param
execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches #最大监控个数
execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events #最大请求个数
parse the command param
option: -d run as a daemon #以守护进程运行
option: -r rsync all the local files to the remote servers before the sersync work #同步目录所有文件
option: -o config xml name: ./confxml.xml #指定配置文件
daemon thread num: 10 #守护进程个数
parse xml config file #.xml配置文件
host ip : localhost host port: 8008 #主机地址和端口
daemon start,sersync run behind the console #运行在后台的进程
use rsync password-file : #使用的密码文件
user is backup #用户
passwordfile is /etc/rsync.pass #文件
config xml parse success #配置文件检查
please set /etc/rsyncd.conf max connections=0 Manually #最大连接数
sersync working thread 12 = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads) #12个线程 1个主线程,1个错误返回线程和10个守护网络线程
Max threads numbers is: 22 = 12(Thread pool nums) + 10(Sub threads) #最大22个线程 12个线程池和10个网络线程
please according your cpu ,use -n param to adjust the cpu rate
------------------------------------------
rsync the directory recursivly to the remote servers once
working please wait... #工作等待中...
execute command: cd /var/www/html && rsync -artuz -R --delete ./ backup@192.168.137.102::www --password-file=/etc/rsync.pass >/dev/null 2>&1 #执行的命令
run the sersync: #运行
watch path is: /var/www/html #监控目录
[root@shamolang html]# ls
bash_completion.d bashrc binfmt.d
192.168.137.102
[root@shamolang web-back]# ls
bash_completion.d bashrc binfmt.d
两个服务器文件相同,在192.168.137.101的目录中执行rm -rf *
[root@shamolang html]# rm -rf *
[root@shamolang html]# ls
[root@shamolang html]#
192.168.137.102
[root@shamolang web-back]# ls
bash_completion.d bashrc binfmt.d
[root@shamolang web-back]# ls
[root@shamolang web-back]#
实时同步完成,若想监控多个目录,只需要在/usr/local/sersync/目录中添加新的.xml文件即可
192.168.137.101
开机自启
vim /etc/rc.d/rc.local
/usr/local/sersync/sersync2 -o /usr/local/sersync/confxml.xml