简介:
inotify的优点:
(1) 监控文件系统时间的变化,通过同步工具实现实时同步数据
inotify的缺点:
(1) 并发如果大于200个文件(10-100K),同步就会有延迟
(2)我们前面写的脚本,每次都是全部推送一次,但确实是增量备份额
也可以只同步变化的文件 不变化的文件不理
(3) 监控到事件后,调用rsync同步是单线程的(加&并发)。sersync是多线程同步的
既然有了inotify-tools,为什么还要开发sersync
sersync的功能多
(1)配置文件
(2) 真正的守护进程socket
(3)可以对失败的文件定时重传
(4)第三方的http接口
(5) 默认是多线程的同步
sersync原理图解
本次实战的图解
实验配置
1.Rsync服务(S1和S2的配置,都一样),这个配置文件默认是不存在的(得新建)
[[email protected] ~]# vim /etc/rsyncd.conf
#QQ 31333741 blog:http://oldboy.blog.51cto.com
##rsyncd.conf start##
uid = rsync
gid = rsync
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
ignore errors
read only = false
list = false
hosts allow = 10.0.0.0/24
hosts deny = 0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync.password
[www]
comment = "write by sanlang"
path = /data0/www/www
[bbs]
comment = "write by sanlang"
path = /data0/www/bbs
[blog]
comment = "write by sanlang"
path = /data0/www/blog
"/etc/rsyncd.conf" 27L, 604C 已写入
[[email protected] ~]#
2. Rsync服务(S1和S2的配置,都一样)添加用户
[[email protected] ~]# useradd rsync -s /sbin/nologin -M
3.修改/或者密码文件(默认是不存的)
[[email protected] ~]# vim /etc/rsync.password
rsync_backup:oldboy
[[email protected] ~]# cat /etc/rsync.password
rsync_backup:oldboy
[[email protected] ~]# chmod 600 /etc/rsync.password
[[email protected] ~]#
4. Rsync服务(S1和S2的配置,都一样)创建目录,并把目录赋予相应的权限
[[email protected] ~]# mkdir /data0/www/www -p
[[email protected] ~]# mkdir -p /data0/www/bbs
[[email protected] ~]# mkdir -p /data0/www/blog
[[email protected] ~]# chown -R rsync.rsync /data0
5.以守护进程的方式启动rsync
[[email protected] ~]# rsync --daemon
[[email protected] ~]# echo "/usr/bin/rsync --daemon">>/etc/rc.local
可能会出错错误:
[[email protected] ~]# rsync --daemon
[[email protected] ~]# failed to create pid file /var/run/rsyncd.pid: File exists
解决方法:
[[email protected] ~]# rm -f /var/run/rsyncd.pid
6.Sersync服务(M)端的配置(默认文件不存在自己指定)
[[email protected] ~]# cat /etc/rsync.password
oldboy
[[email protected] ~]# chmod 600 /etc/rsync.password
[[email protected] ~]# ll /etc/rsync.password
-rw-------. 1 root root 7 4月 22 16:18 /etc/rsync.password
[[email protected] ~]#
7.测试(M-SERVER 上执行OK)
[[email protected] ~]# cd /backup
[[email protected] backup]# ls
sanlang01.txt sanlang03.txt sanlang05.txt sanlang07.txt sanlang09.txt
sanlang02.txt sanlang04.txt sanlang06.txt sanlang08.txt sanlang10.txt
[[email protected] backup]# cd
[[email protected] ~]# rsync -avz /backup/ [email protected]::www/ --password-file=/etc/rsync.password
sending incremental file list
./
sanlang01.txt
sanlang02.txt
sanlang03.txt
sanlang04.txt
sanlang05.txt
sanlang06.txt
sanlang07.txt
sanlang08.txt
sanlang09.txt
sanlang10.txt
sent 508 bytes received 201 bytes 1418.00 bytes/sec
total size is 0 speedup is 0.00
[[email protected] ~]#
6.Sersync服务(M)端的配置(可以不加,主要是把安装包放在自己的家目录下)
[[email protected] ~]# useradd oldboy
[[email protected] ~]# passwd oldboy
更改用户 oldboy 的密码 。
新的 密码:
无效的密码: 过于简单化/系统化
无效的密码: 过于简单
重新输入新的 密码:
passwd: 所有的身份验证令牌已经成功更新。
[[email protected] ~]#
新建一个目录用于sersync的安装
[[email protected] ~]# mkdir -p /home/oldboy/tools
[[email protected] ~]#
7.Sersync服务(M)端开始安装sersync
[[email protected] tools]# rz
rz waiting to receive.
zmodem trl+C
[[email protected] tools]# rz
rz waiting to receive.
zmodem trl+C
100% 710 KB 710 KB/s 00:00:01 0 Errorsary_stable_final.tar.gz...
[[email protected] tools]# ls
sersync2.5.4_64bit_binary_stable_final.tar.gz
[[email protected] tools]#
8.Sersync服务(M)端.解压文件(解压到/usr/local/ 下面)
[[email protected] tools]# tar -zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz -C /usr/local/
GNU-Linux-x86/
GNU-Linux-x86/sersync2
GNU-Linux-x86/confxml.xml
[[email protected] tools]# cd /usr/local/
[[email protected] local]# ls
bin etc games GNU-Linux-x86 include lib lib64 libexec sbin share src
[[email protected] local]#
9.Sersync服务(M)端.改名解压文件的名称
[[email protected] local]# mv GNU-Linux-x86 sersync
[[email protected] local]#
[[email protected] local]# tree sersync/
sersync/
├── confxml.xml
└── sersync2
0 directories, 2 files
[[email protected] local]#
10.规范安装包(在 /usr/local/sersync 下操作)
[[email protected] sersync]# mkdir conf bin logs
[[email protected] sersync]# mv confxml.xml conf/
[[email protected] sersync]# mv sersync2 bin/sersync
[[email protected] local]# tree sersync/
sersync/
├── bin
│ └── sersync
├── conf
│ └── confxml.xml
└── logs
3 directories, 2 files
[[email protected] local]#
11.备份配置文件并且配置
[[email protected] sersync]# cd /usr/local/sersync/conf/
[[email protected] conf]# cp confxml.xml confxml.xml.org
[[email protected] conf]# ls
confxml.xml confxml.xml.org
[[email protected] conf]#
①编辑confxml.xml
24 <localpath watch="/opt/tongbu">
25 <remote ip="127.0.0.1" name="tongbu1"/>
26 <!--<remote ip="192.168.8.39" name="tongbu"/>-->
27 <!--<remote ip="192.168.8.40" name="tongbu"/>-->
28 </localpath>
改成:
24 <localpath watch="/data0/www/www"> #data/www/www 代表内地目录
25 <remote ip="10.0.0.182" name="www"/> # www这个是远程服务器的模块
26 <remote ip="10.0.0.183" name="www"/> # 10.0.0.182/183 是远程的Ip地址
27 </localpath>
28
29 <localpath watch="/data0/www/bbs">
30 <remote ip="10.0.0.182" name="bbs"/>
31 <remote ip="10.0.0.183" name="bbs"/>
32 </localpath>
33
34 <localpath watch="/data0/www/blog">
35 <remote ip="10.0.0.182" name="blog"/>
36 <remote ip="10.0.0.183" name="blog"/>
37 </localpath>
②修改认证的部分
<rsync>
40 <commonParams params="-artuz"/>
41 <auth start="false" users="root" passwordfile="/etc/rsync.pas"/>
42 <userDefinedPort start="false" port="874"/><!-- port=874 -->
43 <timeout start="false" time="100"/><!-- timeout=100 -->
44 <ssh start="false"/>
45 </rsync>
改成:
39 <rsync>
40 <commonParams params="-artuz"/>
41 <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.password"/>
42 <userDefinedPort start="false" port="874"/><!-- port=874 -->
43 <timeout start="true" time="100"/><!-- timeout=100 -->
44 <ssh start="false"/>
45 </rsync>
③修fail队列日志
<failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execut
e once-->
改成(这个目录是自己提前创建好的)
<failLog path="/usr/local/sersync/logs/rsync_fail_log.sh" timeToExecute="60"/><!--default
every 60mins execute once-->
最后保存配置文件
12.配置sersync的命令
[[email protected] conf]# echo "export PATH=$PATH:/usr/local/sersync/bin">>/etc/profile
[[email protected] conf]# tail -n 1 /etc/profile
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/sersync/bin
[[email protected] conf]# which rersync
/usr/bin/which: no rersync in (/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin)
[[email protected] conf]# source /etc/profile
[[email protected] conf]# which sersync
/usr/local/sersync/bin/sersync
[[email protected] conf]#
13.启动命令
[[email protected] ~]# sersync -r -d -o /usr/local/sersync/conf/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: -r rsync all the local files to the remote servers before the sersync work
option: -d run as a daemon
option: -o config xml name: /usr/local/sersync/conf/confxml.xml
daemon thread num: 10
parse xml config file
host ip : localhost host port: 8008
daemon start,sersync run behind the console
use rsync password-file :
user is rsync_backup
passwordfile is /etc/rsync.password
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: 32 = 12(Thread pool nums) + 20(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 /data0/www/www && rsync -artuz -R --delete ./ --timeout=100 [email protected]::www --password-file=/etc/rsync.password >/dev/null 2>&1
run the sersync:
watch path is: /data0/www/www
[[email protected] ~]#
-r 客户端和服务端可能第一次数据不一样 开启先做一次同步
-d 在后台以守护进程的方式启动
-o 指定xml 文件
14.测试以后发现,在第11步骤写的模块(localpath )只识别第一个
这个时候进行多实例传送,每个推送数据的目录给一个配置文件下面三个文件,每个文件都有自己的单独日志和<localpath watch="/data0/www/www"> 只有一个模块,这几个文件可以通过拷贝生成,修改后保存
[[email protected] conf]# ls
bbs_confxml.xml blog_confxml.xml tmp www_confxml.xml
[[email protected] conf]#
以blog_confxml.xml 为例
<localpath watch="/data0/www/bbs">
<remote ip="10.0.0.182" name="bbs"/>
<remote ip="10.0.0.183" name="bbs"/>
</localpath>
<failLog path="/usr/local/sersync/logs/bbs_rsync_fail_log.sh" timeToExecute="60"/><!--default
every 60mins execute once-->
其余的密码文件如果自己想修该的话在自行修改
15启动三个配置文件
先杀掉原来配置
[[email protected] conf]# ps -ef |grep rsync
root 2147 1 0 17:30 ? 00:00:00 sersync -r -d -o /usr/local/sersync/conf/confxml.xml
root 2289 2232 0 17:53 pts/1 00:00:00 grep rsync
[[email protected] conf]# pkill sersync
[[email protected] conf]# ps -ef |grep rsync
root 2292 2232 0 17:53 pts/1 00:00:00 grep rsync
[[email protected] conf]#
多实例启动
sersync -r -d -o /usr/local/sersync/conf/www_confxml.xml
sersync -r -d -o /usr/local/sersync/conf/bbs_confxml.xml
sersync -r -d -o /usr/local/sersync/conf/blog_confxml.xml
检查:可以查出总共起了三个进程,每个进行都对应一个文件
[[email protected] conf]# ps -ef |grep sersync
root 2300 1 0 17:57 ? 00:00:00 sersync -r -d -o /usr/local/sersync/conf/www_confxml.xml
root 2320 1 0 17:57 ? 00:00:00 sersync -r -d -o /usr/local/sersync/conf/bbs_confxml.xml
root 2340 1 0 17:58 ? 00:00:00 sersync -r -d -o /usr/local/sersync/conf/blog_confxml.xml
root 2411 2232 0 18:00 pts/1 00:00:00 grep sersync
[[email protected] conf]#
最后主服务器Sersync服务(M)端可能也会重启
所以:防止重启(吧配置文件放在/etc/rc.local中,这个时候可以不加-r文件比较大,没有必要只要第一次即可)
[[email protected] tmp]# cat >>/etc/rc.local<<EOF
> sersync -d -o /usr/local/sersync/conf/www_confxml.xml
> sersync -d -o /usr/local/sersync/conf/bbs_confxml.xml
> sersync -d -o /usr/local/sersync/conf/blog_confxml.xml
> EOF
[[email protected] tmp]#
测试(Sersync服务(M)端)(测试成功)
[[email protected] ~]# cd /data0
[[email protected] data0]# ls
www
[[email protected] data0]# cd www
[[email protected] www]# ls
bbs blog www
[[email protected] www]# cd bbs
[[email protected] bbs]# touch succes.log
[[email protected] bbs]# cd ..
[[email protected] www]# cd blog/
[[email protected] blog]# touch success.log
[[email protected] blog]# cd ..
[[email protected] www]# cd www
[[email protected] www]# touch success.log
[[email protected] www]#
S2-SERVER端查看
[[email protected] ~]# tree /data0
/data0
└── www
├── bbs
│ ├── bbs.log
│ ├── bbs.txt
│ ├── hello
│ ├── succes.log
│ └── world
├── blog
│ ├── blog.log
│ ├── blog.txt
│ └── success.log
└── www
├── success.log
├── www.log
└── www.txt
4 directories, 11 files
[[email protected] ~]#
经验
2710服务器千兆网
每秒可以同步10-100K 能同步 40-50 张
大量文件同步也是受限制的
sersync的参数
-r 开启第一次进行一次完全的同步,保持一致
-o 指定confxml.xml 的文件
-n 指定默认线程池的线程总数,如果不指定默认启动线程池的数量是10,CPU使用过高,可以通过这个参数进行调整
-d 参数为后台启动
-m 不同步,只运行插件
./sersync --help
进行压力测试
监控是否同步不同步报警