阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Rsync+sersync实现文件实时备份

212次阅读
没有评论

共计 7668 个字符,预计需要花费 20 分钟才能阅读完成。

第一部分:在目标服务器 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 #重新加载服务

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-21发表,共计7668字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中