sersync+rsync=实时异步备份
环境准备
服务器两台
rsync-server:192.168.1.8 (备份服务器)
sersync-node1:192.168.1.9 (需要备份的服务器)
系统
CentOS7.4
关闭防火墙和selinux
基础优化 略
1.rsync-server(备份服务器)
添加rsync服务的用户,管理本地目录的
useradd -s /sbin/nologin -M rsync
生成rsyncd.conf配置文件
vi /etc/rsyncd.conf
#rsync_config_________start
##rsyncd.conf start#####
uid = rsync #==>用户 远端的命令使用rsync访问共享的目录
gid = rsync #==>用户组
usr 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 #==>rsync的日志文件
[backup] #==>模块名称
path = /backup #==>服务器端提供访问的目录
ignore errors #==>忽略错误信息
read only = false #==>客户端是否可上传
list = false #==>不能列表(不能ls)
hosts allow = 192.168.0.0/16 #==>允许那些服务器连接
(这俩host二者留其一就可以;不然外网IP也能推送数据过来)
#hosts deny = 0.0.0.0/32 #==>拒绝那些机器连接
auth users = rsync_backup #==>虚拟用户
secrets file = /etc/rsync.password #==>存放虚拟账号的用户和密码
#rsync_config ______end#####
配置auth users的密码文件
echo "rsync_backup:123456" >/etc/rsync.password #密码123456
权限600
chmod 600 /etc/rsync.password
创建共享的目录并给rsync属主和组
mkdir /backup -p
chown -R rsync.rsync /backup
启动并加入开机自启
rsync --daemon --config=/etc/rsyncd.conf
echo "rsync --daemon --config=/etc/rsyncd.conf" >>/etc/rc.local
2.sersync-node1 (需要备份的服务器)
生成连接服务器需要的密码文件并给600权限
echo "123456" >/etc/rsync.password
chmod 600 /etc/rsync.password
创建sersync目录
mkdir /usr/local/sersync -p
mkdir /usr/local/sersync/{bin,log,conf} -p
下载sersync解压
tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz
mv GNU-Linux-x86/confxml.xml /usr/local/sersync/conf/
mv GNU-Linux-x86/sersync2 /usr/local/sersync/bin
修改配置
cd /usr/local/sersync/conf
cp confxml.xml confxml.xml.bak
vim confxml.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
<host hostip="localhost" port="8008"></host>
<debug start="false"/>
<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="false"/>
<createFolder start="true"/>
<createFile start="true"/>
<closeWrite start="true"/>
<moveFrom start="true"/>
<moveTo start="true"/>
<attrib start="false"/>
<modify start="false"/>
</inotify>
<sersync>
<localpath watch=" /data/"> # 填写需要备份的目录
<remote ip="192.168.1.8" name="bakup"/> #服务端的IP地址和rsync定义的模块名
<!--<remote ip="192.168.8.39" name="tongbu"/>-->
<!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>
<rsync>
<commonParams params="-artuz"/>
<auth start="true" users="rsync_backup" passwordfile="/etc/rsyncd.password"/> #定义认证用户和密码文件 在服务器端都配置好了的,密码文件是本地刚才配置的
<userDefinedPort start="false" port="874"/><!-- port=874 -->
<timeout start="ture" time="100"/><!-- timeout=100 --> #超时时间
<ssh start="false"/>
</rsync>
<failLog path="/usr/local/sersync/logs/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
# 定时传输
<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>
启动sersync
/usr/local/sersync/bin/sersync2 -d -r -o /usr/local/sersync/conf/confxml.xml
ps -ef|grep sersync #查看进程是否存在
测试
在node端执行
cd /data/
touch {1..10}.txt
在服务端看
cd /backup
ls
如果存在就说明OK了~