linux服务器使用rsync 和 inotify或者sersync 实现服务器之间文件实时同步

服务器环境

  • 服务端:172.16.57.26 centos6.7 rsync-server 接收文件
  • 客户端:172.16.57.25 centos6.7 sersync+rsync-client 发送文件

服务端rsync

安装
# 首先确认软件是否安装:
rpm -qa rsync
# 未安装
yum install rsync

编辑配置文件 /etc/rsyncd.conf
vim  /etc/rsyncd.conf

##全局配置			
uid = rsync    #用户			
gid = rsync    #用户组	
#监听端口
port = 873
use 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    #日志文件,显示出错信息

##模块配置			
[backup]            #模块名称			
path = /home/a_dest      #模块对应的位置(路径)			
ignore errors       #忽略错误程序			
read only = false    #是否只读			
list = false        #是否可以列表			
hosts allow = 172.16.57.0/24  #准许访问rsync服务器的客户范围			
hosts deny = 0.0.0.0/32      #禁止访问rsync服务器的客户范围			
auth users = rsync_backup    #不存在的用户;只用于认证			
secrets file = /etc/rsync.password  #设置进行连接认证的密匙文件

path = /data 同步的目的路径
auth users = rsync_user1 rsync的用户名为rsync_user1
secrets file = /etc/rsync.password 存储该用户的密码文件为 /etc/rsync.password.

执行配置文件必要准备
# 同步的目的路径
mkdir -p /home/a_dest
# 添加用户
useradd -M -s /sbin/nologin rsync

# 验证
cat /etc/passwd | grep rsync
cat /etc/group | grep rsync

# 启动rsync服务
rsync --daemon
# 验证
netstat -antup | grep rsync

# 更改同步目的路径的拥有者
chown -R rsync /home/a_dest
chgrp -R rsync /home/a_dest
# 验证
ls -ld /home/a_dest

# 添加rsync程序的账户名和密码
echo "rsync_user1:123456" >/etc/rsync.password
chmod 600 /etc/rsync.password		#必须为600,否则失败

# 设置开机启动柜
echo "rsync --daemon" >> /etc/rc.local

如何重启rsync服务?
pkill rsync #关闭rsync服务
rsync --daemon #启动rsync服务

客户端

# 安装
yum install rsync

# 配置rsync的密码文件
echo "123456" > /etc/rsync.password
chmod 600 /etc/rsync.password 		#必须为600,否则失败
示例
# 推送
rsync -avz --delete /home/b_source/ rsync_user1@192.168.197.129::backup --password-file=/etc/rsync.password 

# 拉取
rsync -avzP rsync_user1@192.168.197.129::backup /home/b_source/ --password-file=/etc/rsync.password


同步失败

# 开放873端口
iptables -A INPUT -p tcp --dport 873 -j ACCEPT

lsyncd同步命令

官网地址:https://lsyncd.github.io/lsyncd/
Lsyncd 旨在将缓慢变化的本地目录树同步到远程镜像。Lsyncd 对于将数据从安全区域同步到不太安全的区域特别有用
Lysncd 不会妨碍本地文件系统性能,可以通过配置文件实现细粒度的自定义
Lysncd 实际上是 Lua 语言封装了 inotify 和 rsync 工具

安装lsyncd
yum install lsyncd -y
配置lsyncd
vim /etc/lsyncd.conf 

# logfile         定义日志文件
# stausFile       定义状态文件
# statusInterval  将lsyncd的状态写入上面的statusFile的间隔,默认10秒
# nodaemon=true   表示不启用守护模式,默认
# inotifyMode     指定inotify监控的事件,默认是CloseWrite,还可以是Modify或CloseWrite or Modify
# maxProcesses    同步进程的最大个数。假如同时有20个文件需要同步,而maxProcesses = 8,则最大能看到有8个rysnc进程
# maxDelays       累计到多少所监控的事件激活一次同步,即使后面的delay延迟时间还未到

# sync定义同步参数
# default.rsync        本地目录之间同步,使用rsync,也可以达到使用ssh形式的远程rsync效果,或daemon方式连接远程rsyncd进程;
# default.direct       本地目录之间同步,使用cp、rm等命令完成差异文件备份;
# default.rsyncssh     同步到远程主机目录,rsync的ssh模式,需要使用key来认证
# 目录设置
# source                        同步的源目录,使用绝对路径。
# target                        定义目的地址.对应不同的模式有几种写法:
# /tmp/dest                     本地目录同步,可用于direct和rsync模式
# 172.29.88.223:/tmp/dest       同步到远程服务器目录,可用于rsync和rsyncssh模式
# excludeFrom                   排除选项,后面指定排除的列表文件,如excludeFrom = "/etc/lsyncd.exclude",如果是简单的排除,可以使用exclude = LIST。

# delete 参数为了保持target与souce完全同步,Lsyncd默认会delete = true来允许同步删除。它除了false,还有startup、running
# true         Lsyncd将在目标上删除任何不在源中的内容。 在启动时和正常操作中被删除的内容。
#false        在lsyncd启动后将在目标上不删除任何不在源中的内容, 在启动时和正常操作中被删除的内容。
#startup      启动时将执行一次完全文件同步,保证完全一致;正常运行过程中不会删除target中的文件
#running      启动前,增加的会同步,删除的不同步;正常运行过程中会删除target中的文件

# rsync选项
# bwlimit      限速,单位kb/s,与rsync相同(这么重要的选项在文档里竟然没有标出)
# compress     压缩传输默认为true。在带宽与cpu负载之间权衡,本地目录同步可以考虑把它设为false
# perms        默认保留文件权限。

settings {
    logfile      ="/var/log/lsyncd/lsyncd.log",
    statusFile   ="/var/log/lsyncd/lsyncd.status",
    inotifyMode  = "CloseWrite",
    maxProcesses = 8,
    }

sync {
    default.rsync,
    source    = "/data",
    target    = "rsync_backup@172.16.57.26::backup",  
    excludeFrom="/etc/lsyncd_exclude.lst",
    delete = false,
    delay=1,
    rsync     = {
        binary    = "/usr/bin/rsync",
        archive   = true,
        compress  = true,
        verbose   = true,
        --delete =  true,
        password_file="/etc/rsync.passwd",
        _extra={"--bwlimit=200,--password-file=/etc/rsync.passwd"}
        }
    }

lsyncd.conf可以有多个sync,各自的source,各自的target,各自的模式,互不影响。

启动lsyncd
systemctl start lsyncd
各种模式下配置示例
settings {
    logfile ="/usr/local/lsyncd-2.1.5/var/lsyncd.log",
    statusFile ="/usr/local/lsyncd-2.1.5/var/lsyncd.status",
    inotifyMode = "CloseWrite",
    maxProcesses = 8,
    }


-- I. 本地目录同步,direct:cp/rm/mv。 适用:500+万文件,变动不大
sync {
    default.direct,
    source    = "/tmp/src",
    target    = "/tmp/dest",
    delay = 1
    maxProcesses = 1
    }

-- II. 本地目录同步,rsync模式:rsync
sync {
    default.rsync,
    source    = "/tmp/src",
    target    = "/tmp/dest1",
    excludeFrom = "/etc/rsyncd.d/rsync_exclude.lst",
    rsync     = {
        binary = "/usr/bin/rsync",
        archive = true,
        compress = true,
        bwlimit   = 2000
        } 
    }

-- III. 远程目录同步,rsync模式 + rsyncd daemon
sync {
    default.rsync,
    source    = "/tmp/src",
    target    = "syncuser@172.29.88.223::module1",
    delete="running",
    exclude = { ".*", ".tmp" },
    delay = 30,
    init = false,
    rsync     = {
        binary = "/usr/bin/rsync",
        archive = true,
        compress = true,
        verbose   = true,
        password_file = "/etc/rsyncd.d/rsync.pwd",
        _extra    = {"--bwlimit=200"}
        }
    }

-- IV. 远程目录同步,rsync模式 + ssh shell
sync {
    default.rsync,
    source    = "/tmp/src",
    target    = "172.29.88.223:/tmp/dest",
    -- target    = "root@172.29.88.223:/remote/dest",
    -- 上面target,注意如果是普通用户,必须拥有写权限
    maxDelays = 5,
    delay = 30,
    -- init = true,
    rsync     = {
        binary = "/usr/bin/rsync",
        archive = true,
        compress = true,
        bwlimit   = 2000
        -- rsh = "/usr/bin/ssh -p 22 -o StrictHostKeyChecking=no"
        -- 如果要指定其它端口,请用上面的rsh
        }
    }

-- V. 远程目录同步,rsync模式 + rsyncssh,效果与上面相同
sync {
    default.rsyncssh,
    source    = "/tmp/src2",
    host      = "172.29.88.223",
    targetdir = "/remote/dir",
    excludeFrom = "/etc/rsyncd.d/rsync_exclude.lst",
    -- maxDelays = 5,
    delay = 0,
    -- init = false,
    rsync    = {
        binary = "/usr/bin/rsync",
        archive = true,
        compress = true,
        verbose   = true,
        _extra = {"--bwlimit=2000"},
        },
    ssh      = {
        port  =  1234
        }
    } 

sersync同步命令

wget http://down.whsir.com/downloads/sersync2.5.4_64bit_binary_stable_final.tar.gz
tar zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz
mv GNU-Linux-x86 /usr/local/sersync
echo 'export PATH=$PATH:/usr/local/sersync' >> ~/.bash_profile
source ~/.bash_profile

# 配置文件
cd /usr/local/sersync
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"/>      #监控的是否为xfs文件系统
    <filter start="false">         #是否开启文件类型过滤,开启后如下筛选的文件类型将不监控
        <exclude expression="(.*)\.svn"></exclude>
        <exclude expression="(.*)\.gz"></exclude>
        <exclude expression="^info/*"></exclude>
        <exclude expression="^static/*"></exclude>
    </filter>
    <inotify>   #监控的事件,默认监控的是delete/close_write/moved_from/moved_to/create folder
        <delete start="true"/>
        <createFolder start="true"/>
        <createFile start="false"/>
        <closeWrite start="true"/>
        <moveFrom start="true"/>
        <moveTo start="true"/>
        <attrib start="false"/>
        <modify start="false"/>
    </inotify>
 
    <sersync>   #rsync命令配置段
        <localpath watch="/var/atlassian/application-data/confluence/attachments/ver003">   #监控的数据目录,这里是confluence的附件文件目录
            <remote ip="172.16.20.10" name="backup"/> #备机的ip地址和rsync daemon模块名称,所以备机上需要已daemon模式运行rsync
            <!--<remote ip="192.168.8.39" name="tongbu"/>-->
            <!--<remote ip="192.168.8.40" name="tongbu"/>-->
        </localpath>
        <rsync>
            <commonParams params="-avz"/>  #rsync的参数
            <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.password"/> #开启rsync的认证模式
            <userDefinedPort start="true" port="8787"/> #指定备机上rsync监听的端口号
            <timeout start="true" time="100"/>  #开启认证超时时间
            <ssh start="false"/>  #安全起见,不建议使用ssh的方式认证
        </rsync>
        <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once--> #传输失败时会重新传输,再次失败会写入rsync_fail_log中,每隔一段时间(timeToExecute)执行脚本再次传输
        <crontab start="true" schedule="600"><!--600mins--> #对监控目录与目标服务器每隔一段时间进行一次整体同步,默认600分钟,根据个人情况是否开启
            <crontabfilter start="false">  #如果之前开启了文件过滤,这里也要设置过滤
                <exclude expression="*.php"></exclude>
                <exclude expression="info/*"></exclude>
            </crontabfilter>
        </crontab>
        <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
sersync2 -n 10 -d -o /usr/local/sersync/confxml.xml
# -n 启用线程数量
# -d daemon方式启动
# -o 指定配置文件

sersync -r        #可以全量同步监控的目录

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值