1、环境及相关软件介绍
操作系统:Ubuntu12.04_x64
源服务器(推送):192.168.18.10
目标服务器(备份):192.168.18.20
rsync:
rsync是unix系统下的数据镜像备份工具,是一款快速增量备份工具(远程同步),支持本地复制,或者与其他SSH(安全传输)、rsync主机同步。
rsync有以下常用的参数:
-v,--verbose 显示同步过程的详细信息
-a,--archive 归档模式,表示以递归方传输文件,并保持所有文件属性,等同于-rlptgoD
-r,--recursive 对子目录以递归模式处理
-z,--compress 对备份的文件在传输时进行压缩处理
-l,--links 保留软连接
-H,--hard-links 保留硬链接
--delete 删除那些DST中SRC没有的文件(源服务器删除文件,目标服务器也同时删除)
--progress 显示备份过程,等同于-P
--port=PORT 指定rsync服务端口
--exclude=FILE 排除一个目录或文件(排除多个目录时,可以写多个--exclude)
--exclude-from=FILE 排除多个目录或文件,FILE里面写多个要排除的目录
inotify:
inotify是一个Linux特性,它监控文件系统操作,比如读取、写入和创建,当文件系统有变化时,则会触发inotify。inotify提供inotify-tools工具,这个工具包含两个功能,一个是inotifywait,用来监控文件系统变化的事件,另一个是inotifywatch,用来统计文件系统访问的次数。我们这次使用inotifywait结合rsync实现实时同步功能。
inotifywait有以下常用参数:
-m,--monitor 一直保持监听事件
-r,--recursive 递归监控目录
-q,--quiet 只打印触发的事件
-e,--event 指定监视时间
--timefmt 指定时间格式,用于-format选型中的%T格式
--format 指定输出格式。
%w 表示发生事件的目录
%f 表示发生事件的文件
%e 表示发生的事件
%T 使用由—timefmt定义的时间格式
inotifywait常用监视事件:
access 文件或目录读取
modify 文件或目录更改
attrib 文件或目录属性更改
move 文件或目录移动
create 文件或目录创建
delete 文件或目录删除
2、目标服务器安装与配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
log file = /var/log/rsyncd .log
pid file = /var/run/rsyncd .pid
lock file = /var/lock/rsyncd
[home]
comment = sync rsync /home
path = /home/rsync
use chroot=no
read only = no
uid=root
gid=root
max connections=10
auth users = rsync
secrets file = /etc/rsyncd .pass
hosts allow=192.168.18.0 /24
ignore errors = yes
timeout = 600
|
#创建认证文件
1
2
3
4
5
6
7
8
9
|
rsync :123456
* rsync daemon not enabled in /etc/default/rsync , not starting...
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 29605 /rsync
|
博客地址:http://lizhenliang.blog.51cto.com
3、源服务器安装与配置
#此时基本配置完成,测试下是否能推送吧!
1
2
3
4
5
6
|
sending incremental file list
rsync /a
0 100% 0.00kB /s 0:00:00 (xfer
sent 354 bytes received 126 bytes 960.00 bytes /sec
total size is 0 speedup is 0.00
|
#出现以上信息说明同步正常,接下来,就该编写Shell脚本,使用inotifywait做实时监控源目录,加个while循环判断源目录是否有触发,如果有变化则执行rsync同步,并记录日志:
1
2
3
4
5
6
7
8
9
|
SRC= '/home/rsync'
DST= 'rsync@192.168.18.20::home'
/usr/bin/inotifywait -mrq --timefmt '%y-%m-%d %H:%M' -- format '%T %w %f %e' -e create,delete,move,modify $SRC| while read files
do
rsync -avzP --password- file = /etc/rsyncd .pass --delete $SRC $DST
echo "$files was rsynced." >> /tmp/rsync .log
done
|
4、测试实时同步
#先打印形式查看脚本执行情况
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
+ SRC= /home/rsync/
+DST= rsync @192.168.18.213::home
+ read files
+ /usr/bin/inotifywait -mrq --timefmt '%y-%m-%d %H:%M' -- format '%T %w%f %e' -e create,delete,move,modify,attrib /home/loongtao
+ rsync -avzP --password- file = /etc/rsyncd .pass --delete /home/rsync rsync @@192.168.18.213::home
sending incremental file list
rsync /
rsync /test .txt
0 100% 0.00kB /s 0:00:00 (xfer
sent 349 bytes received 32 bytes 762.00 bytes /sec
total size is 9380 speedup is 24.62
+ echo '15-04-24 13:33/home/rsync/test.txt CREATE was rsynced.'
+ read files
+ rsync -avzP--password- file = /etc/rsyncd .pass --delete /home/rsync rsync @192.168.18.213::home
sending incremental file list
sent 310 bytes received 10 bytes 640.00 bytes /sec
total size is 9380 speedup is 29.31
+ echo '15-04-24 13:33/home/rsync/test.txt ATTRIB was rsynced.'
+ read files
|
#可以看到以上信息,无任何报错,说明已经推送成功。
然后,把脚本放到后台运行:
#此时当源服务器/home/rsync目录文件有更新,则会同步到目标服务器/home/rsync目录