Rsync+sersync实现文件实时备份


第一部分:在目标服务器192.168.0.217上操作
一、在OA文件备份服务器安装Rsync服务端
1、关闭SELINUX
vi /etc/selinux/config #编辑防火墙配置文件
代码如下:

#SELINUX=enforcing #注释掉
#SELINUXTYPE=targeted #注释掉
SELINUX=disabled #增加
:wq! #保存,退出

setenforce 0 #立即生效

2、开启防火墙tcp 873端口(Rsync默认端口)

3、安装Rsync服务端软件
yum install rsync xinetd #安装
vi /etc/xinetd.d/rsync #编辑配置文件,设置开机启动rsync(7版本服务器可不配置)

代码如下:

service rsync

{

disable = no

flags = IPv6

socket_type = stream

wait = no

user = root

server = /usr/bin/rsync

server_args = --daemo
n
log_on_failure += USERID

}

:wq! #保存退出

/etc/init.d/xinetd start #启动(CentOS中是以xinetd来管理Rsync服务的)

4.配置rsync,rsync的配置文件是/etc/rsyncd.conf,配置如下:
5.
vi /etc/rsyncd.conf #创建配置文件,添加以下代码

代码如下:

log file = /var/log/rsyncd.log #日志文件位置,启动rsync后自动产生这个文件,无需提前创建
pidfile = /var/run/rsyncd.pid #pid文件的存放位置
lock file = /var/run/rsync.lock #支持max connections参数的锁文件
secrets file = /etc/rsync.pass #用户认证配置文件,里面保存用户名称和密码,后面会创建这个文件
motd file = /etc/rsyncd.Motd #rsync启动时欢迎信息页面文件位置(文件内容自定义)
uid = root #设置rsync运行权限为root
gid = root #设置rsync运行权限为root
port=873 #默认端口
use chroot = no #默认为true,修改为no,增加对目录文件软连接的备份
[Seeyon] #自定义名称
path = /home/Seeyon/ #rsync服务端数据目录路径
comment = Seeyon #模块名称与[Seeyon]自定义名称相同
ignore errors  #忽略错误

read only = no #设置rsync服务端文件为读写权限
list = no #不显示rsync服务端资源列表
max connections = 200 #最大连接数
timeout = 600 #设置超时时间
auth users = root #执行数据同步的用户名,可以设置多个,用英文状态下逗号隔开
hosts allow = * #允许进行数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开(*代表任意地址)
#hosts deny = X.X.X.X #禁止数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开

:wq! #保存,退出

上方需要注意的地方:secrets file这个是配置同步的密码文件的。[Seeyon]这个是配置同步模块的名称,path是配置同步的目录,hosts allow是允许同步的主机,hosts deny:拒绝同步的主机

5.创建同步的用户与密码的文件,既第4步的secrets file这个配置选项中的文件。/etc/rsync.passwd,同进要设置这个文件的权限为600

echo "root:baidu.c0m" >> /etc/rsync.pass
chmod 600 /etc/rsync.pass

6.创建同步的目录:既第4步的path配置选项中的目录。
mkdir /home/Seeyon

7、启动rsync
复制代码
rsync --daemon --config=/etc/rsyncd.conf (后台运行rsync)
接着重启一下xinetd

systemctl restart xinetd (重起)
systemctl stop xinetd(停止)
systemctl start xinetd(启动)

8.查看是否启动成功
netstat –anutp | grep 873
出现如下数据则为启动成功:
Rsync+sersync实现文件实时备份

9.设置开机启动
(方法一)
echo "rsync --daemon --config=/etc/rsyncd.conf" >> /etc/rc.d/rc.local

(方法二)
/usr/bin/rsync –daemon –config=/etc/rsyncd.conf

第二部分:在源服务器192.168.0.218上操作

一、安装Rsync客户端

1、关闭SELINUX
vi /etc/selinux/config #编辑防火墙配置文件

代码如下:

#SELINUX=enforcing #注释掉
#SELINUXTYPE=targeted #注释掉
SELINUX=disabled #增加
:wq! 保存退出

setenforce 0 立即生效

2、开启防火墙tcp 873端口(Rsync默认端口,做为客户端的Rsync可以不用开启873端口)

3、安装Rsync客户端端软件

代码如下:

whereis rsync #查看系统是否已安装rsync,出现下面的提示,说明已经安装
rsync: /usr/bin/rsync /usr/share/man/man1/rsync.1.gz

yum install xinetd #只安装xinetd即可,RHEL7中是以xinetd来管理rsync服务的

yum install rsync xinetd #如果默认没有rsync,运行此命令进行安装rsync和xinetd

vi /etc/xinetd.d/rsync #编辑配置文件,设置开机启动rsync

代码如下:

service rsync

{

disable = no

flags = IPv6

socket_type = stream

wait = no

user = root

server = /usr/bin/rsync

server_args = --daemo
n
log_on_failure += USERID

}

3.1#启动(RHEL7中是以xinetd来管理rsync服务的)
systemctl start xinetd

4、创建密码文件,同OA文件备份服务器一样,不过这个文件只要保存一个密码就行了,不用用户名,权限也是600
vi /etc/rsync.passwd #编辑文件,添加以下内容

代码如下:

baidu.c0m #密码
:wq! 保存退出
chmod 600 /etc/rsync.passwd #设置文件权限,只设置文件所有者具有读取、写入权限即可

5、测试是否能够连接OA备份服务器
rsync –a Seeyon@192.168.0.217:: (结果如下图则为成功)

Rsync+sersync实现文件实时备份

二、安装sersync工具,实时触发rsync进行同步
1、查看服务器内核是否支持inotify
ll /proc/sys/fs/inotify #列出文件目录,出现下面的内容,说明服务器内核支持inotify(无需复制代码进行制作)

代码如下:
-rw-r--r-- 1 root root 0 Mar 7 02:17 max_queued_events
-rw-r--r-- 1 root root 0 Mar 7 02:17 max_user_instances
-rw-r--r-- 1 root root 0 Mar 7 02:17 max_user_watches
备注:Linux下支持inotify的内核最小为2.6.13,可以输入命令:uname -a查看内核
CentOS 5.X 内核为2.6.18,默认已经支持inotify

2、修改inotify默认参数(inotify默认内核参数值太小)

查看系统默认参数值:
sysctl -a | grep max_queued_events
结果是:fs.inotify.max_queued_events = 16384
sysctl -a | grep max_user_watches
结果是:fs.inotify.max_user_watches = 8192
sysctl -a | grep max_user_instances
结果是:fs.inotify.max_user_instances = 128

修改参数:
代码如下:

sysctl -w fs.inotify.max_queued_events="99999999"
sysctl -w fs.inotify.max_user_watches="99999999"
sysctl -w fs.inotify.max_user_instances="65535"

参数说明:
max_queued_events:
inotify队列最大长度,如果值太小,会出现" Event Queue Overflow "错误,导致监控文件不准确
max_user_watches:
要同步的文件包含多少目录,可以用:find /home/Seeyon -type d | wc -l 统计,必须保证max_user_watches值大于统计结果(这里/home/Seeyon为同步文件目录)
max_user_instances:
每个用户创建inotify实例最大值

3、安装sersync
sersync下载地址点击这里。

mkdir /usr/local/sersync
mkdir /usr/local/sersync/conf
mkdir /usr/local/sersync/bin
mkdir /usr/local/sersync/log
tar zxvf sersync2.5_32bit_binary_stable_final.tar.gz
cd GNU-Linux-x86/
cp confxml.xml /usr/local/sersync/conf
cp sersync2 /usr/local/sersync/bin

4、配置sersync

代码如下:
vi confxml.xml 编辑,修改下面的代码

代码如下:

<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
  #设置本地IP和端口
  <host hostip="localhost" port="8008"></host>
  #开启DUBUG模式 
  <debug start="false"/>
  #开启xfs文件系统
  <fileSystem xfs="false"/>
  #同步时忽略推送的文件(正则表达式),默认关闭
  <filter start="false">
      <exclude expression="(.*)\.svn"></exclude>
      <exclude expression="(.*)\.gz"></exclude>
      <exclude expression="^info/*"></exclude>
      <exclude expression="^static/*"></exclude>
  </filter>
  <inotify>
  #设置要监控的事件
      <delete start="true"/>
      <createFolder start="true"/>
      <createFile start="true"/>
      <closeWrite start="true"/>
      <moveFrom start="true"/>
      <moveTo start="true"/>
      <attrib start="true"/>
      <modify start="true"/>
</inotify>
  <sersync>
  # 本地同步的目录路径
      <localpath watch="/home/Seeyon">
  # 远程IP和rsync模块名 
          <remote ip="192.168.0.217" name="Seeyon"/> 
          <!--<remote ip="192.168.8.39" name="tongbu"/>-->
          <!--<remote ip="192.168.8.40" name="tongbu"/>-->
      </localpath>
      <rsync>
  #rsync指令参数
          <commonParams params="-auvzP"/>
  #rsync同步认证
          <auth start="true" users="root" passwordfile="/etc/rsync.passwd"/>
  #设置rsync远程服务端口,远程非默认端口则需打开自定义
          <userDefinedPort start="false" port="873"/><!-- port=873 -->
  #设置超时时间
          <timeout start="true" time="100"/><!-- timeout=100 -->
  #设置rsync+ssh加密传输模式,默认关闭,开启需设置SSH加密证书
          <ssh start="false"/>
      </rsync>
    #sersync传输失败日志脚本路径,每隔60会重新执行该脚本,执行完毕会自动清空。
      <failLog path="/usr/local/sersync/log/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
    #设置rsync+crontab定时传输,默认关闭
      <crontab start="false" schedule="600"><!--600mins-->
          <crontabfilter start="false">
              <exclude expression="*.php"></exclude>
              <exclude expression="info/*"></exclude>
          </crontabfilter>
      </crontab>
  #设置sersync传输后调用name指定的插件脚本,默认关闭
      <plugin start="false" name="command"/>
  </sersync>
  #插件脚本范例
  <plugin name="command">
      <param prefix="/bin/sh" suffix="" ignoreError="true"/>  <!--prefix /opt/tongbu/mmm.sh suffix-->
      <filter start="false">
          <include expression="(.*)\.php"/>
          <include expression="(.*)\.sh"/>
      </filter>
  </plugin>
  #插件脚本范例
  <plugin name="socket">
      <localpath watch="/opt/tongbu">
          <deshost ip="192.168.138.20" port="8009"/>
      </localpath>
  </plugin>
  <plugin name="refreshCDN">
      <localpath watch="/data0/htdocs/cms.xoyo.com/site/">
          <cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/>
          <sendurl base="http://pic.xoyo.com/cms"/>
          <regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/>
      </localpath>
  </plugin>
</head>

:wq! #保存退出

参数说明:
localpath watch="/home/Seeyon.":#源服务器同步目录
192.168.0.217:#目标服务器IP地址
name="Seeyon": #目标服务器rsync同步目录模块名称
users="root": #目标服务器rsync同步用户名
passwordfile="/etc/rsync.passwd": #目标服务器rsync同步用户的密码在源服务器的存放路径
remote ip="192.168.0.217": #目标服务器ip,每行一个
failLog path="/tmp/rsync_fail_log.sh" #脚本运行失败日志记录
start="true" #设置为true,每隔600分钟执行一次全盘同步
6.设置环境变量:
#echo "export PATH=$PATH:/usr/local/sersync/bin/" >> /etc/profile
#source /etc/profile

7..启动sersync
sersync2 -r -d -o /usr/local/sersync/conf/confxml.xml
注:重启操作如下:
killall sersync2 && sersync2 -r -d -o /usr/local/sersync/conf/confxml.xml


8、设置sersync监控开机自动执行
vi /etc/rc.d/rc.local #编辑,在最后添加一行

代码如下:
/usr/local/sersync/bin/sersync2 -d -r -o /usr/local/sersync/conf/confxml.xml #设置开机自动运行脚本
:wq! #保存退出
9、添加脚本监控sersync是否正常运行
vi /home/crontab/check_sersync.sh 编辑,添加以下代码

代码如下:

#!/bin/sh
sersync="/usr/local/sersync/bin/sersync2"
confxml="/usr/local/sersync/conf/confxml.xml"
status=$(ps aux |grep 'sersync2'|grep -v 'grep'|wc -l)
if [ $status -eq 0 ];
then
$sersync -d -r -o $confxml &
else
exit 0;
fi

:wq! #保存退出
chmod +x /home/crontab/check_sersync.sh #添加脚本执行权限
vi /etc/crontab #编辑,在最后添加下面一行

*/1 * * * * root /home/crontab/check_sersync.sh > /dev/null 2>&1 #每隔1分钟执行一次脚本
service crond reload #重新加载服务

本文永久更新链接地址:https://www.bkjia.com/Linux/2018-02/151051.htm

相关内容