如何通过rsync+sersync 实现同步备份

该文档是在很早之前写的,一直存在草稿箱中,最近在整理其它学习资料时发现还没有发布,内容和方法有可能和现在的操作系统版本有些不符合了,但处理思路没有变化,大家可以根据最新版本要求,参照该文档进行配置学习,有些不对的地方还望大家多多理解。

一、为什么要实现同步备份

        服务器上有些重要文件或数据时,可以把他们多备份一份到其他服务器上,这样就不怕数据或文件丢失了。

二、环境的搭建

服务器A:192.168.1.10 源服务器

服务器B:192.168.1.20 目的服务器

我们要实现的就是把A服务器上的文件同步到B服务器上,从而实现备份。我们主要是在B服务器上安装配置rsync,在A服务器上安装配置sersync,通过sersync把文件推送到B服务器上

三、开始搭建

3.1、配置B服务器

3.1.1、关闭selinux

在/etc/sysconfig/selinux 这个文件,设置SELINUX=disable

3.1.2、防火墙开通873端口

可以使用以下命令,也可以把防火墙关闭

-A INPUT -m state --state NEW -m tcp -p tcp --dport 873 -j ACCEPT

3.1.3、开始安装rsync
[root@Test ~]# yum install rsync -y
3.1.4、配置rsync

rsync的配置文件是/etc/rsyncd.conf,配置如下:

#Gobal Setting

uid = root

gid = root

use chroot = no

read only = no

hosts allow = 192.168.1.0/24

hosts deny = *

max connections = 5

pid file = /var/run/rsyncd.pid

secrets file = /etc/rsyncd/rsyncd.password

log file = /var/log/rsyncd.log

motd file = /etc/rsyncd/rsyncd.motd

transfer logging = yes

log format = %t %a %m %f %b

ignore errors = yes

#User Options

[test]

path=/test

list = yes

auth users = tom1

comment = Tom1's home

secrets file:这个是配置同步密码文件的。

[test]:这个是配置同步模块的名称

path:是配置同步的目录

hosts allow:是允许同步的主机

hosts deny:拒绝同步的主机。

3.1.5、创建同步的用户与密码的文件

即上图中的secrets file这个配置选项中的文件。/etc/rsync.passwd,同进要设置这个文件的权限为600

[root@Test ~]# echo "user:password" >> /etc/rsync.passwd

[root@Test ~]# chmod 600 /etc/rsync.passwd
3.1.6、创建同步的目录

即上图中path配置选项中的目录。

[root@Test ~]# mkdir /home/rsynctest
3.1.7、启动rsync
[root@Test ~]# rsync --daemon --config=/etc/rsyncd.conf

接着重启一下xinetd

[root@Test ~]# /etc/init.d/xinetd restart
3.1.8、配置开机启动
 [root@Test ~]# echo "rsync --daemon --config=/etc/rsyncd.conf" >> /etc/rc.d/rc.local

到这B服务器基本就配置完成了。

3.2、配置A服务器

3.2.1、sersync官网下载sersync

官网地址:http://sersync.sourceforge.net

[root@Test ~]# wget http://sersync.googlecode.com/files/sersync2.1_64bit_binary.tar.gz
3.2.1、安装sersync
[root@Test ~]# mkdir /usr/local/sersync

[root@Test ~]# mkdir /usr/local/sersync/conf

[root@Test ~]# mkdir /usr/local/sersync/bin

[root@Test ~]# mkdir /usr/local/sersync/log

[root@Test ~]# tar zxvf sersync2.5_32bit_binary_stable_final.tar.gz

[root@Test ~]# cd GNU-Linux-x86/

[root@Test ~]# cp confxml.xml /usr/local/sersync/conf

[root@Test ~]# cp sersync2 /usr/local/sersync/bin
3.2.3、创建密码文件

同B服务器一样,不过这个文件只要保存一个密码就行了,不用用户名,权限也是600

[root@Test ~]# echo "password" >> /etc/rsync.passwd

[root@Test ~]# chmod 600 /etc/rsync.passwd
3.2.4、配置sersync

配置文件就是上第二步复制的confxml.xml这个文中,路径在/usr/local/sersync/conf中。

<?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="/data">
   # 远程IP和rsync模块名  
           <remote ip="192.168.1.20" name="data"/>  
           <!--<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="user" passwordfile="/etc/rsync.passwd"/>
   # 设置rsync远程服务端口,远程非默认端口则需打开自定义
           <userDefinedPort start="false" port="874"/><!-- port=874 -->
   # 设置超时时间
           <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>
3.2.5、创建同步目录
[root@Test ~]# mkdir /home/rsynctest
3.2.6、设置环境变量
[root@Test ~]# echo "export PATH=$PATH:/usr/local/sersync/bin/" >> /etc/profile
[root@Test ~]# source /etc/profile
3.2.7、启动sersync
[root@Test ~]# sersync2 -r -d -o /usr/local/sersync/conf/confxml.xml

注:重启操作如下:

[root@Test ~]# killall sersync2 && sersync2 -r -d -o /usr/local/sersync/conf/confxml.xml
3.2.8、设置开机启动
[root@Test ~]# echo "sersync2 -r -d -o /usr/local/sersync/conf/confxml.xml" >> /etc/rc.local

好了,两台机器的配置都已经完成,现在你在A服务器的/home/rsynctest这个目录下创建文件,看看B服务器同样目录下是不是也生成了这个文件,如果是,那就恭喜,你成功了!

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值