rsync是一个开源的快速备份工具,能够在不一样的主机之间镜像同步整个目录树,支持增量备份,保持连接和权限,且采用优化的同步算法,在传输前执行压缩,所以很是适用于异地备份、镜像服务器等应用。html
在同步任务中,负责发起rsync同步操做的客户机称为发起端,而负责响应来自于客户机的rsync同步操做的服务器称为同步源。在同步过程当中,同步源负责提供文档的原始位置,而发起端对该位置具备读取权限,拓扑图以下。算法
rsync做为同步源时以守护进程运行,为其余客户机提供备份源。配置rsync同步源须要创建配置文件rsyncd.conf,首先建立备份帐号,而后将rsync程序以--daemon选项运行。vim
在CentOS7中,rsync软件包是默认安装好的。安全
[root@localhost ~]# rpm -q rsync rsync-3.1.2-4.el7.x86_64 [root@localhost ~]# vim /etc/rsyncd.conf uid = root # 设置运行rsync进程的用户 gid = root # 设置运行rsync进程的用户组 use chroot = yes #禁锢在家目录 address = 192.168.58.160 #监听地址 port 873 #监听端口 pid file = /var/run/rsyncd.pid #存放进程PID文件位置 log file = /var/log/rsyncd.log #存放日志文件位置 hosts allow = 192.168.58.0/24 #容许访问的客户机地址 [wwwroot] #共享模块名称 path = /var/www/html #源目录的实际路径 read only = yes #是否为只读 dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 #同步时再也不压缩的文件类型 auth users = backuper #受权帐户 secrets file = /etc/rsyncd_users.db #存放帐户信息的数据文件
基于安全性考虑,对于rsync的同步源最好仅容许以只读方式作同步。另外,同步能够采用匿名的方式,只要将其中的auth users和secrets file配置记录去掉就能够了。bash
根据上一步的设置,建立帐号数据文件,添加一行用户记录,以冒号分隔,用户名称为backuper,密码为abc123.因为帐号信息采用明文存放,所以应调整文件权限,避免帐号信息泄露。服务器
[root@localhost ~]# vim /etc/rsyncd_users.db backuper:abc123 [root@localhost ~]# chmod 600 /etc/rsyncd_users.db [root@localhost ~]# ls -l /etc/rsyncd_users.db -rw-------. 1 root root 16 7月 26 10:55 /etc/rsyncd_users.db
备份用户backuper须要对源目录/var/www/html有相应的读取权限。实际上,因为咱们这里设置的都是root的超级管理员用户,具备最高权限,若是你设置的uid,gid不是root,那么须要other组具备读取权限,这样才能进行同步。tcp
完成上述操做后,执行rsync --daemon命令就能够启动rsync服务,以独立监听服务的方式运行。若要关闭rsync服务,能够采用kill进程的方式。ide
[root@localhost www]# rsync --daemon [root@localhost www]# netstat -ntap | grep rsync tcp 0 0 192.168.58.160:873 0.0.0.0:* LISTEN 78877/rsync
若是要关闭rsync服务的话,能够采用kill进程的方式。工具
[root@localhost run]# kill $(cat /var/run/rsyncd.pid)
有了同步源服务器以后,就可使用rsync工具执行远程同步了。优化
绝大部分备份程序要求指定原始位置和目标位置,rsync也同样。最简单的用法相似于cp命令。例如能够将文件/etc/fstab和目录/boot/grub同步备份到/opt目录下。
rsync主要命令选项:
-r:递归模式,对子目录以递归模式处理 -l:--links 保留软链结 -v:--verbose 详细模式输出 -a:--archive 归档模式,表示以递归方式传输文件,并保持全部文件属性,等于-rlptgoD -z:--compress 对备份的文件在传输时进行压缩处理 -p:--perms 保持文件权限。 -o:--owner 保持文件属主信息。 -g:--group 保持文件属组信息。 -D:--devices 保持设备文件信息。 -t:--times 保持文件时间信息。 -A:保持ACL属性信息 -D:保留设备文件及其其余特殊文件 --delete:删除目标位置有而原始位置没有的文件
在执行远程同步任务时,rsync命令须要指定同步源服务器中的资源位置。rsync同步源的资源表示方法为“用户名@主机地址::共享模块”或者“rsync://用户名@主机地址/共享模块”,前者为两个冒号分隔形式,后者URL地址形式。例如,执行如下操做将访问rsync同步源,并下载到本地/root/test目录下备份。
[root@localhost ~]# rsync -avz backuper@192.168.58.160::wwwroot /opt/test Password: receiving incremental file list created directory /opt/test ./ index.html test1.txt test2.txt sent 84 bytes received 242 bytes 93.14 bytes/sec total size is 17 speedup is 0.05 [root@localhost ~]# ls /opt/test/ index.html test1.txt test2.txt
或者
[root@localhost ~]# rm -rf /opt/test/* [root@localhost ~]# rsync -avz rsync://backuper@192.168.58.160/wwwroot /opt/test Password: receiving incremental file list ./ index.html test1.txt test2.txt sent 84 bytes received 242 bytes 93.14 bytes/sec total size is 17 speedup is 0.05
为了实现同步过程当中不用输入密码,须要在本地建立一个密码文件,保存backuper用户的密码,在执行同步选项时--password-file=/etc/server.pass,同时须要修改这个密码文件的权限为600.
[root@localhost ~]# vim /etc/server.pass [root@localhost ~]# cat /etc/server.pass abc123 [root@localhost ~]# chmod 600 /etc/server.pass [root@localhost ~]# ls -l /etc/server.pass -rw-------. 1 root root 7 7月 26 11:31 /etc/server.pass [root@localhost ~]# rsync -avz --password-file=/etc/server.pass rsync://backuper@192.168.58.160/wwwroot /opt/test receiving incremental file list ./ a.txt b.txt c.txt sent 84 bytes received 275 bytes 718.00 bytes/sec total size is 17 speedup is 0.05
Linux内核中提供了inotify通知接口,用来监控文件系统的各类变化状况,如文件存取、删除、移动、修改等。利用这一机制,能够很是方便地实现文件移动告警,增量备份,并针对目录或者文件的变化作出响应。
将rsync工具与inotify机制相结合,能够实现触发式备份————只要原始位置的文档发生了变化,就当即启动增量备份操做,不然处于静默等待状态,这样就避免了按固定周期备份时存在的延迟性、周期性过密等问题。正由于inotify通知机制由Linux内核提供,所以主要作本机监控,在触发式备份中应用时更适合上行同步。
vim /etc/sysctl.conf fs.inotify.max_queued_events = 16384 #监控事件队列 fs.inotify.max_user_instances = 1024 #最多监控实例 fs.inotify.max_user_watches = 1048576 #每一个实例最多监控文件数 [root@localhost html]# sysctl -p fs.inotify.max_queued_events = 16384 fs.inotify.max_user_instances = 1024 fs.inotify.max_user_watches = 1048576
使用inotify机制须要安装inotify-tools,以便提供inotifywait和inotifywatch辅助工具程序,用来监控和汇总改动状况。
[root@localhost ~]# tar xf inotify-tools-3.14.tar.gz -C /opt/ [root@localhost ~]# cd /opt/inotify-tools-3.14/ [root@localhost inotify-tools-3.14]# ./configure [root@localhost inotify-tools-3.14]# make && make install
安装结束后,以监控/var/www/html为例,能够限制性inotifywait命令,而后在另一个终端想/var/www/html目录中添加、移动文件,跟踪屏幕输出结果。其中-e用来指定监控哪些事件,选项-m表示持续监控,-r表示递归整个目录,-q简化输出信息。
[root@localhost inotify-tools-3.14]# inotifywait -mrq -e modify,create,move,delete /var/www/html/
在另一个终端向目录中添加文件,
[root@localhost ~]# cd /var/www/html/ [root@localhost html]# touch test01.txt [root@localhost html]# touch test02.txt [root@localhost html]#
在监控终端中会显示变动信息
[root@localhost inotify-tools-3.14]# inotifywait -mrq -e modify,create,move,delete /var/www/html/ /var/www/html/ CREATE test01.txt /var/www/html/ CREATE test02.txt
inotifywait能够监控modify(修改),create(建立),move(移动),delete(删除),attrib(属性变更)等事件,一旦变更就当即输出结果,inotifywatch可用来收集文件系统变更状况,并在运行结束后输出汇总的变化状况。
使用inotifywait输出的监控结果中,每行记录依次包括目录、事件、文件、据此能够识别变更状况,只要检测到变更就执行rsync同步操做便可。
[root@localhost inotify-tools-3.14]# vim /opt/inotify_rsync.sh #!/bin/bash INOTIFY_CMD="inotifywait -mrq -e modify,create,attrib,move,delete /var/www/html/" RSYNC_CMD="rsync -vazH --delete --password-file=/etc/server.pass /var/www/html/ backuper@192.168.58.160::wwwroot/" $INOTIFY_CMD | while read DIRECTORY EVENT FILE do if [ $(pgrep rsync | wc -l) -le 5 ] ; then $RSYNC_CMD fi done [root@localhost inotify-tools-3.14]# chmod +x /opt/inotify_rsync.sh #添加执行权限
上述脚本用来检测本机的/var/www/html目录的变更状况,一旦有更新就触发rsync同步操做,上传备份至192.168.58.161(本机是192.168.58.160)的/var/www/html目录下。