rsync
# rsync同步
监控目录变化 rsync+inotify rsync+sersync
实时备份和定时备份 支持增量备份 cp,scp,rm实现这三个命令的功能
生产场景集群架构服务器备份方案 cron+rsync 备份全网数据的解决方案
etc/rsyncd.conf
uid = rsync
gid = rsync
hosts allow = 192.168.0.0/16
hosts deny = 0.0.0.0/32
use chroot = no
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
max connections = 2000
timeout = 600
ignore errors
read only = false
list= false
auth users = rsync_backup
secrets file = /etc/rsync.password
fake super = yes
[backup]
comment = backup
path = /backup
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
rsync命令是客户端命令 rsync -avz /etc/hosts /tmp rsync -avz --delete /null /tmp rsync -avzP -e 'ssh -p 22' /tmp/ root@192.168.200.248:/tmp
执行无差异备份 rsync -avz --delete /backup/ rsync_backup@172.16.1.41::backup/ --password-file=/etc/rsync.password
# 1、配置文件
fake super = yes
# 2、创建rsync用户,及共享的目录
useradd rsynd -s /sbin/nologin -M mkdir /backup chown -R rsync /backup
# 3、创建密码文件
echo "rsync_backup:oldboy" >/etc/rsync.password chmod 600 /etc/rsync.password
# 4、rsync --daemon
netstat -lntup|grep rsync ps -ef|grep rsync|grep -v grep
# 5、加入开机自启动
echo "rsync --daemon" >>/etc/rc.local cat /etc/rc.local
# rsync客户端
# 1、创建密码文件
echo "oldboy" /etc/rsync.password chmod 600 /etc/rsync.password
# 2、rsync
push: rsync -avz /tmp rsync_backup@192.168.200.248::backup --password-file=/etc/rsync.password
pkill rsync
rsync --deamon --address=192.168.200.249
kill pid
kill -HUP cat /var/run/rsyncd.pid
rsync -avz rsync://rsync_backup@192.168.200.248/backup /tmp
rsync优点 1、增量备份同步,支持socket(daemon),集中备份
rsync缺点 1、大量小文件时候同步的时候,比对时间较长,有的时候,rsync进程停止 解决:打包后同步 。drbd (文件系统同步复制block) 2、同步大文件,10G这样的大文件有时也会有问题,中断。未完整同步前,是隐藏文件,同步完成改为正常文件。
无差异同步 --delete,尽量不要用
排除参数 --exclude --exclude-from=文件
# inotifywait
#!/bin/bash
#
# inotifywait监控目录文件变化并同步到备份服务器
inotify=/usr/local/bin/inotifywait
$inotify -mrq --format '%w%f' -e create,delete,close_write /data \
|while read file
do
cd / &&
rsync -az /data --delete rsync_backup@10.0.0.5::backup \
--password-file=/etc/rsync.password
done
2
3
4
5
6
7
8
9
10
11
12
13
#!/bin/bash
host=192.168.200.240
src=/data/
dest=data
passwd=/etc/rsync.passwd
user=rsync_backup
inotifywait=/opt/inotify/bin/inotifywait
$inotifywait -mrq --timefmt '%Y%m%d %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src \
| while read files ;do
rsync -avz --delete --timeout=100 --password-file=${passwd} $src $user@$host::$dest
echo "${files} was rsyncd" >>/tmp/rsync.log 2>&1
done
2
3
4
5
6
7
8
9
10
11
12
13
14
# inotifywait 监控目录变化
inotifywatch
/usr/local/inotify-tools/bin/inotifywait -mrq --format '%T %w%f' -e create,delete,move /backup
inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e create,delete /data
# inotify参数优化
调用rsync同步是单进程的 paste 合并文件内容显示split 分割文件 inotify缺点1、并发不能大于200个文件(10-100K)2、脚本每次都是全部推送一次
# sersync配置
# 1、查看服务器内核是否支持inotify
列出文件目录 ll /proc/sys/fs/inotify 出现下面的内容、说明服务器内核支持inotify -rw-r--r-- 1 root root 0 Dec 25 12:03 max_queued_events -rw-r--r-- 1 root root 0 Dec 25 15:05 max_user_instances -rw-r--r-- 1 root root 0 Dec 25 12:03 max_user_watches 备注:centos6.9默认支持inotify
# 2.修改inotify默认参数(inotify默认内核参数值太小)
sysctl -a | grep max_queued_events sysctl -a | grep max_user_watches sysctl -a | grep max_user_instances 修改参数 sysctl -w fs.inotify.max_queued_events="99999999" sysctl -w fs.inotify.max_user_watches="99999999" sysctl -w fs.inotify.max_user_instances="65535" 生效 sysctl -p 参数说明: max_queued_events: inotify队列最大长度,如果值太小,会出现” Event Queue Overflow “错误,导致监控文件不准确 max_user_watches: 要同步的文件包含多少目录,可以用:find /var/www/synctest -type d | wc -l 统计,必须保证max_user_watches值大于统计结果(这里/var/www/synctest为同步文件目录) max_user_instances: 每个用户创建inotify实例最大值
# 3、最好更改最大连接数、最大文件描述符。
vi /etc/pam.d/login session required /lib64/security/pam_limits.so vi /etc/security/limits.conf
soft nproc 65535
hard nproc 65535
soft nofile 65535
hard nofile 65535 重启服务器
# 4、安装sersync
cd /usr/local/src wget https://sersync.googlecode.com/files/sersync2.5.4_64bit_binary_stable_final.tar.gz 如果下载不了,用github这个 wget --no-check-certificate https://raw.githubusercontent.com/orangle/sersync/master/release/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 cd /usr/local/sersync
# 5、配置sersync
cp confxml.xml confxml.xml-bak vi confxml.xml 设置如下代码:(如果需要同步多目录,就再建一个如confxmldata.xml,port端口8008不能冲突(测试好像不改也没关系),改成其他端口。localpath watch 改下,failLog path这个脚本文件也改下,每个xml文件中只保留自己的localpath watch )
<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="/home/xyz">
<remote ip="192.168.1.189" name="test"/>
</localpath>
<rsync>
<commonParams params="-artuz"/>
<auth start="true" users="root" passwordfile="/etc/rsyncd.passwd"/>
<userDefinedPort start="false" port="874"/><!-- port=874 -->
<timeout start="false" time="100"/><!-- timeout=100 -->
<ssh start="false"/>
</rsync>
<failLog path="/tmp/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>
<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>
```
<?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="/home/xyz">
<remote ip="192.168.1.189" name="test"/>
</localpath>
<rsync>
<commonParams params="-artuz"/>
<auth start="true" users="root" passwordfile="/etc/rsyncd.passwd"/>
<userDefinedPort start="false" port="874"/><!-- port=874 -->
<timeout start="false" time="100"/><!-- timeout=100 -->
<ssh start="false"/>
</rsync>
<failLog path="/tmp/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>
<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>
```
<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="/home/xyz">
<remote ip="192.168.1.189" name="test"/>
</localpath>
<rsync>
<commonParams params="-artuz"/>
<auth start="true" users="root" passwordfile="/etc/rsyncd.passwd"/>
<userDefinedPort start="false" port="874"/><!-- port=874 -->
<timeout start="false" time="100"/><!-- timeout=100 -->
<ssh start="false"/>
</rsync>
<failLog path="/tmp/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>
<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>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?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="/home/xyz">
<remote ip="192.168.1.189" name="test"/>
</localpath>
<rsync>
<commonParams params="-artuz"/>
<auth start="true" users="root" passwordfile="/etc/rsyncd.passwd"/>
<userDefinedPort start="false" port="874"/><!-- port=874 -->
<timeout start="false" time="100"/><!-- timeout=100 -->
<ssh start="false"/>
</rsync>
<failLog path="/tmp/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>
<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>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62