目录
(2)rsync+inotify-tools 与 rsync+sersync 架构的区别?
一、rsync概述
Rsync(Remote Sync)是Linux系统下的数据镜像备份工具。该工具可以实现远程同步、不同主机之间的同步,也能实现全量备份和增量备份,保持数据链接和权限,并采用优化的同步算法,传输前对数据进行压缩,故该工具非常适合架构集中式备份或异地备份。也支持本地复制或与ssh、rsync同步。
优点:
scp无法备份大量数据,而rsync备份、统计、比较一起进行。
可以备份整个目录树和文件系统,并保持文件原来的权限、时间、软硬链接。
安装较容易,无需特殊权限。
同步快速,首次同步完全备份,再次同步增量备份。
可以使用scp和ssh等方式传输备份文件
支持匿名传输
选择性保持:符号链接、硬链接、文件属性、权限、时间等
传输速度快:压缩再传输、解压再使用,减少带宽。
# 查看版本信息
[root@server ~]# rsync --version
rsync version 3.1.2 protocol version 31
Copyright (C) 1996-2015 by Andrew Tridgell, Wayne Davison, and others.
Web site: http://rsync.samba.org/
Capabilities:
64-bit files, 64-bit inums, 64-bit timestamps, 64-bit long ints,
socketpairs, hardlinks, symlinks, IPv6, batchfiles, inplace,
append, ACLs, xattrs, iconv, symtimes, prealloc
rsync comes with ABSOLUTELY NO WARRANTY. This is free software, and you
are welcome to redistribute it under certain conditions. See the GNU
General Public Licence for details.
备份分类:
完全备份:所有文件进行备份
差异备份:备份自上次完全备份以来所有的修改
增量备份:备份自上次备份依赖所作的修改
二、rsync运行原理
rsync采用C/S模式,即点到点的传输。通过xinetd服务监听873端口,再让xinetd服务下的rsync服务作出响应。
源主机:需要同步数据的服务器
目标主机:存放服务器同步数据的主机
数据同步方式:push 和 pull
推push:主动同步,把数据发送给目标主机。服务器开销大,适合后端服务器较少的情况。【服务器备份推给rsync客户端存放,主动模式】
目的主机配置为 rsync 服务端,源主机周期性的使用 rsync 命令把要同步的目录推过去。
拉pull:所有客户端主机去服务器上面拉数据,导致数据传输缓慢。【rsync客户端去服务器上拉数据,存放到客户端上,被动模式】
源主机配置为 rsync 服务端,目的主机周期性的使用 rsync 命令把要同步的目录拉过来。
三、rsync部署
1、 rsync的配置文件介绍
rsync的配置文件:/etc/rsync.conf
# /etc/rsyncd.conf
#全局参数:对rsync服务器生效,优先级较低
port # rsync占用端口号,默认是873
address # 监听地址,一般是目标主机的IP地址
uid # 运行进程的用户
gid # 运行进程的用户组
max connections # 最大连接数
lock file # 最大连接数的锁文件
motd file # 同步登录后的提示语,填写欢迎同步信息,自行创建
log file # 日志文件
pid file # 进程PID文件,自动生成
hosts allow # 允许同步的主机
#模块参数:针对某一个目录定义的参数,优先级较高
[mod_name] # 同步目录名
comment # 描述信息
path # 同步目录
read only # 同步目录的读写权限
exclude
exclude from
include
include from
auth users # 备份的用户,自动创建,与系统用户无关
secrets file # 存放rsync用户的密码文件
hosts allow
hosts deny
list
timeout
源主机ip:192.168.75.144
目标主机ip:192.168.75.145
2、push:服务器主动推送数据
[root@targetpc ~]# vim /etc/rsyncd.conf
[root@targetpc ~]# cat /etc/rsyncd.conf
port=873
address = 192.168.75.145
uid = root
gid = root
use chroot = yes
max connections = 4
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
motd file = /etc/rsyncd.motd
hosts allow = 192.168.75.0/24
[data]
path = /data/backup
comment = bakcup data
read only = false
list = yes
auth users = rsyncuser
secrets file = /etc/rsync.passwd
#创建文件传输目录
[root@targetpc ~]# mkdir -p /data/backup
#创建密码文件,格式
[root@targetpc ~]# vim /etc/rsync.passwd
rsyncuser:123456
#创建欢迎信息
[root@targetpc ~]# echo "狗蛋,把你资料给你爹" > /etc/rsyncd.motd
#更改密码本权限为600
[root@targetpc ~]# chmod 600 /etc/rsync.passwd
#测试
[root@sourcepc ~]# rsync -avz data/ rsyncuser@192.168.75.145::data
[root@sourcepc ~]# rsync -avz data/ rsyncuser@192.168.75.145::data
狗蛋,把你资料给你爹
Password: 123456
sending incremental file list
sent 40 bytes received 12 bytes 2.67 bytes/sec
total size is 0 speedup is 0.00
#删除那些目标位置有的文件而备份源没有的文件,最大程度的保持一致
[root@sourcepc ~]# rsync -avz --delete data/ rsyncuser@192.168.75.145::data
3、pull:客户机主动拉取数据
#服务器配置
[root@sourcepc ~]# cat /etc/rsyncd.conf
port=873
address = 192.168.75.144
uid = root
gid = root
use chroot = yes
max connections = 4
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
motd file = /etc/rsyncd.motd
hosts allow = 192.168.75.0/24
[data]
path = /data/backup
comment = bakcup data
read only = false
list = yes
auth users = rsyncuser
secrets file = /etc/rsync.passwd
[root@sourcepc ~]# vim /etc/rsync.passwd
[root@sourcepc ~]# chmod 600 /etc/rsync.passwd
[root@sourcepc ~]# echo "不准偷你爹数据" > /etc/rsyncd.motd
#测试(客户机)
[root@targetpc ~]# rsync -avz rsyncuser@192.168.75.144::data /data/backup/
不准偷你爹数据
Password:
receiving incremental file list
./
1.txt
f1
f2
aaa/
aaa/bbb
sent 115 bytes received 314 bytes 171.60 bytes/sec
total size is 0 speedup is 0.00
四、sersync数据同步
1、数据同步原理
(1)为什么要用rsync+sersync
sersync是基于inotify开发的,可以记录目录中发生变化的内容,具体到某个文件或者目录,在使用rsync同步的时候,只同步发生变化的文件或者目录。【增量同步】
(2)rsync+inotify-tools 与 rsync+sersync 架构的区别?
①inotify-tools只能记录目录发生了变化,但是不能具体到某个文件或者目录。
②rsync同步不清楚哪些文件或目录发生了变化,就会整个目录都同步,效率低。
(3)同步过程
①同步服务器上开启sersync记录指定路径的文件系统变化情况。
②目标服务器上使用rsync命令把变化的数据同步到目标服务器上。
③同步服务器上配置sersync服务,目标服务器安装rsync服务。
2、 部署rsync+sersync
源主机ip:192.168.75.144
目标主机ip:192.168.75.145
#下载sersync服务
[root@sourcepc ~]# tar -xvf sersync2.5.4_64bit_binary_stable_final.tar.gz
GNU-Linux-x86/
GNU-Linux-x86/confxml.xml
GNU-Linux-x86/sersync2
[root@sourcepc ~]# cd GNU-Linux-x86/
[root@sourcepc GNU-Linux-x86]# ls
confxml.xml sersync2
#修改配置文件(部分)
[root@sourcepc GNU-Linux-x86]# vim confxml.xml
<inotify>
<delete start="true"/>
<createFolder start="true"/>
<createFile start="true"/>
<closeWrite start="true"/>
<moveFrom start="true"/>
<moveTo start="true"/>
<attrib start="true"/>
<modify start="true"/>
</inotify>
<sersync>
<localpath watch="/root/data/"> #源主机监控目录
<remote ip="192.168.75.145" name="data"/> #目标主机ip和网名
<!--<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="rsyncuser" passwordfile="/etc/rsync.passwd"/> #存放目标主机密码本路径
#创建密码本
vim /etc/rsync.passwd
123456 (目标主机在rsync服务中创建的密码)
#启动sersync数据同步服务
[root@sourcepc GNU-Linux-x86]# ./sersync2 -d -r -o ./confxml.xml
set the system param
execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
parse the command param
option: -d run as a daemon
option: -r rsync all the local files to the remote servers before the sersync work
option: -o config xml name: ./confxml.xml
daemon thread num: 10
parse xml config file
host ip : localhost host port: 8008
will ignore the inotify createFile event
daemon start,sersync run behind the console
use rsync password-file :
user is rsyncuser
passwordfile is /etc/rsync.passwd
config xml parse success
please set /etc/rsyncd.conf max connections=0 Manually
sersync working thread 12 = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads)
Max threads numbers is: 22 = 12(Thread pool nums) + 10(Sub threads)
please according your cpu ,use -n param to adjust the cpu rate
------------------------------------------
rsync the directory recursivly to the remote servers once
working please wait...
execute command: cd /root/data && rsync -artuz -R --delete ./ rsyncuser@192.168.75.145::data --password-file=/etc/rsync.passwd >/dev/null 2>&1
run the sersync:
watch path is: /root/data
3、设置rsync+sersync开机自启
# 设置开机自启
[root@server ~]# vim /etc/rc.d/rc.local
/opt/sersync/sersync2 -d -r -o /opt/sersync/confxml.xml
五、总结
rsync可以进行数据的同步,可以使用推和拉两种方式。推即源主机推数据到目标主机,拉即目标主机从源主机上拉数据。
push:服务器向客户端推送数据,要在目标主机上配置一个共享目录,在服务端上使用rsync命令推送数据给目标主机。
pull:客户端向服务器拉去数据,需要把服务器上的同步目录配置成一个共享目录,然后客户端去这个共享目录上拉去数据。
仅使用rsync同步数据,不会记录数据的变化,每次同步都是同步整个目录,不适合大量数据的同步。
结合使用rsync+sersync,sersync负责监控源主机上同步目录的数据变化,rsync负责同步变化的部分,极大地提高了同步的效率。