一、rsync服务简介
1、安装软件
` [root@splitdns ~]# rpm -q rsync
未安装软件包 rsync
[root@splitdns ~]# yum install -y rsync
[root@splitdns ~]# rpm -q rsync
rsync-3.1.2-10.el7.x86_64
`
rsync在生产环境的使用场景:
1)、用于数据异地备份(7天一完备、每天一增备)
2)、用于web集群中节点数据的批量更新(同步)
3)、用于本地yum服务器与互联网镜像站软件同步
在远程同步任务中,负责发起rsync同步操作的客户机称为发起端,而负责响应来自客户机的rsync同步操作的服务器称为备份源(同步源),同步源负责提供文档的原始位置,发起端应对该位置具有读或写权限。
rsync作为同步源时以守护进程运行,为其他客户机提供备份源。配置rsync同步源需建立配置文件 /etc/rsyncd.conf,备份账号信息,然后将rsync程序以守护进程(--daemon)方式运行。
rsync两种源模式:
·SSH备份源
·rsync备份源
1、建立/etc/rsyncd.conf配置文件
[root@master ~]# yum install -y rsync
[root@master ~]# vim /etc/rsyncd.conf
配置项含义:
基于安全性考虑,对于rsync的备份源最好仅允许只读的方式做下行同步,另外,同步可以
采用匿名的方式,只要将其中的auth users和 secrets file 配置记录去掉就可以。
2、 为备份账户创建数据文件,以冒号分割,密码信息在文件中以明文方式存放,为避免泄露需要做权限调整
服务端:密码文件权限设为600
[root@rsync-master]#vim /etc/rsync_user.db
backuper:pwd123
[root@rsync-master ~]#chmod 600 /etc/rsyncd_user.db
客户端:密码文件权限设为600
[root@rsync-client ~]# ll /etc/rsyncd.pass
-rw-------. 1 root root 5 6月 30 19:59 /etc/rsyncd.pass
3、启动服务命令 rsync --daemon ,以独立方式运行,若关闭可使用可采用 kill $(cat /var/run/rsyncd.pid)
[root@rsync ~]#rsync --daemon --config=/etc/rsyncd.conf
4、关闭服务
[root@rsync ~]#kill -9 $(cat /var/run/rsyncd.pid)
二、使用rsync备份工具
setfacl设置文件的归属
[root@splitdns ~]# getfacl tmp.txt
#file: tmp.txt
#owner: root
#group: root
user::rw-
group::r--
other::r--
getfacl获取文件的归属
rsync备份操作案例
rsync备份脚本
实际生产环境备份工作通常是按计划重复执行
1、rsyn备份源的无交互验证方式
2、通过指定--password-file选项指定存储密码的文件
三、配置rsync+inotify实时同步
1、调整参数
[root@www inotify]# vim max_queued_events ##表示监控事件(16384)
[root@www inotify]# vim max_user_instances ##最多监控实例数(128)
[root@www inotify]# vim max_user_watches ##每个实例最多监控文件数(8192)
当要监控的目录,文件数量较多或者变化频繁,建议将参数设置最大
[root@rsync-client ~]# vim /etc/sysctl.conf
2、安装inotify-tools
inotify-tools主要提供inotify-wait,inotify-watch等工具,用来监控,汇总改动的情况。
官方站点 inotify-tools.sourceforge.net
源码安装
[root@rsync-client ~]# tar -xf inotify-tools-3.14.tar.gz -C /usr/src/
[root@rsync-client ~]# cd /usr/src/inotify-tools-3.14/
[root@rsync-client inotify-tools-3.14]# nohup ./configure --prefix=/usr/local/inotify &&
make && make install &
创建软连接
[root@rsync-client ~]# ln -s /usr/local/inotify/bin/* /usr/sbin/
以监控/var/www/html为例,执行inotifywait命令后,在另一终端中改动/var/www/html目录下的 内容
编写触发式脚本
Sersync 和 Rsync 实现数据文件实时同步
同步原理
客户端配置sersync服务
sersync是基于inotify+rsync的大量文件的多服务器自动同步程序
使用 Linux 2.6 内核的 inotify 监控 Linux 文件系统事件,被监听目录下如果有文件发生修改,sersync 将通过内核自动捕获到事件,并将该文件利用 rsync 同步到多台远程服务器。sersync 仅仅同步发生增、删、改事件的单个文件或目录,不像rsync镜像同步那样需要比对双方服务器整个目录下数千万的文件,并且支持多线程同步,因此效率非常高。
sersync已不在更新,感谢sersync作者周洋
谷歌项目地址:code.google.com/archive/p/s…
64位下载地址:storage.googleapis.com/google-code…
32位下载地址:storage.googleapis.com/google-code…
上不去google的可以直接wget我本地的包,这里只提供64位版本的:down.whsir.com/downloads/s…
1、解包
[root@rsync-client ~]# tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz -C /usr/local/
2、修改配置文件
对第24、25、31行进行修改
3、进行测试
[root@rsync-client ~]# sersync2 -r -d -o /usr/local/sersync/confxml.xml
sersync2选项说明
-r 递归
-o 指定文件
[root@rsync-client html]# touch {1..5}.txt
[root@rsync-sever html]# ls -l {1..5}.txt
-rw-------. 1 nobody nobody 0 6月 30 21:18 1.txt
-rw-------. 1 nobody nobody 0 6月 30 21:18 2.txt
-rw-------. 1 nobody nobody 0 6月 30 21:18 3.txt
-rw-------. 1 nobody nobody 0 6月 30 21:18 4.txt
-rw-------. 1 nobody nobody 0 6月 30 21:18 5.txt
[root@rsync-client html]# mkdir test
[root@rsync-client html]# cd test/
[root@rsync-client test]# touch {1..5}.txt
[root@rsync-sever html]# ls -ld test/
drwx------. 2 nobody nobody 71 6月 30 21:21 test/
[root@rsync-sever html]# ls -l test/{1..5}.txt
-rw-------. 1 nobody nobody 0 6月 30 21:21 test/1.txt
-rw-------. 1 nobody nobody 0 6月 30 21:21 test/2.txt
-rw-------. 1 nobody nobody 0 6月 30 21:21 test/3.txt
-rw-------. 1 nobody nobody 0 6月 30 21:21 test/4.txt
-rw-------. 1 nobody nobody 0 6月 30 21:21 test/5.txt
注意密码文件权限设置为600