服务器环境
- 服务端: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
hosts deny = 0.0.0.0/32
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 --daemon
netstat -antup | grep rsync
chown -R rsync /home/a_dest
chgrp -R rsync /home/a_dest
ls -ld /home/a_dest
echo "rsync_user1:123456" >/etc/rsync.password
chmod 600 /etc/rsync.password
echo "rsync --daemon" >> /etc/rc.local
如何重启rsync服务?
pkill rsync
rsync --daemon
客户端
yum install rsync
echo "123456" > /etc/rsync.password
chmod 600 /etc/rsync.password
示例
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
同步失败
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
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"/>
<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="false"/>
<closeWrite start="true"/>
<moveFrom start="true"/>
<moveTo start="true"/>
<attrib start="false"/>
<modify start="false"/>
</inotify>
<sersync>
<localpath watch="/var/atlassian/application-data/confluence/attachments/ver003">
<remote ip="172.16.20.10" name="backup"/>
<!--<remote ip="192.168.8.39" name="tongbu"/>-->
<!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>
<rsync>
<commonParams params="-avz"/>
<auth start="true" users="rsync_backup" passwordfile="/etc/rsync.password"/>
<userDefinedPort start="true" port="8787"/>
<timeout start="true" time="100"/>
<ssh start="false"/>
</rsync>
<failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
<crontab start="true" schedule="600"><!--600mins-->
<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>
sersync2 -n 10 -d -o /usr/local/sersync/confxml.xml
sersync -r