rsync远程同步

rsync远程同步

rsync简介

rsync是一个开源的快速备份工具,可以在不同的主机之间镜像同步整个目录树,支持增量备份,保持链接和权限,且采用优化的同步算法,在传输前执行压缩,因此非常适用于异地备份、镜像服务器等应用。

在同步任务中,负责发起rsync同步操作的客户机称为发起端,而负责响应来自于客户机的rsync同步操作的服务器称为同步源。在同步过程中,同步源负责提供文档的原始位置,而发起端对该位置具有读取权限,拓扑图如下。
rsync远程同步

配置rsync同步源

rsync作为同步源时以守护进程运行,为其他客户机提供备份源。配置rsync同步源需要建立配置文件rsyncd.conf,首先创建备份账号,然后将rsync程序以--daemon选项运行。

配置/etrc/rsyncd.conf文件

在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配置记录去掉就可以了。

为备份账户创建数据文件

根据上一步的设置,创建账号数据文件,添加一行用户记录,以冒号分隔,用户名称为backuper,密码为abc123.由于账号信息采用明文存放,因此应调整文件权限,避免账号信息泄露。
rsync远程同步

[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组具有读取权限,这样才能进行同步。

启动rsync服务程序,运行参数--daemon

完成上述操作后,执行rsync --daemon命令就可以启动rsync服务,以独立监听服务的方式运行。若要关闭rsync服务,可以采用kill进程的方式。

[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工具执行远程同步了。

rsync命令的基本用法

绝大部分备份程序要求指定原始位置和目标位置,rsync也一样。最简单的用法类似于cp命令。例如可以将文件/etc/fstab和目录/boot/grub同步备份到/opt目录下。
rsync远程同步
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 [email protected]::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

rsync远程同步
或者

[root@localhost ~]# rm -rf /opt/test/*
[root@localhost ~]# rsync -avz rsync://[email protected]/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://[email protected]/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

配置rsync+inotify实时同步

Linux内核中提供了inotify通知接口,用来监控文件系统的各种变化情况,如文件存取、删除、移动、修改等。利用这一机制,可以非常方便地实现文件移动告警,增量备份,并针对目录或者文件的变化做出响应。

将rsync工具与inotify机制相结合,可以实现触发式备份————只要原始位置的文档发生了变化,就立即启动增量备份操作,否则处于静默等待状态,这样就避免了按固定周期备份时存在的延迟性、周期性过密等问题。正因为inotify通知机制由Linux内核提供,因此主要做本机监控,在触发式备份中应用时更适合上行同步。

调整inotify内核参数

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-tools

使用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/ [email protected]::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 
#添加执行权限

rsync远程同步
rsync远程同步
rsync远程同步
rsync远程同步
上述脚本用来检测本机的/var/www/html目录的变动情况,一旦有更新就触发rsync同步操作,上传备份至192.168.58.161(本机是192.168.58.160)的/var/www/html目录下。

猜你喜欢

转载自blog.51cto.com/10693404/2150438