rsync
1. rsync简介
rsync是linux系统下的数据镜像备份工具。使用快速增量备份工具Remote Sync可以远程同步,支持本地复制,或者与其他SSH、rsync主机同步。
2. rsync特性
它的特性如下:
可以镜像保存整个目录树和文件系统。
可以很容易做到保持原来文件的权限、时间、软硬链接等等。
无须特殊权限即可安装。
快速:第一次同步时 rsync 会复制全部内容,但在下一次只传输修改过的文件。rsync 在传输数据的过程中可以实行压缩及解压缩操作,因此可以使用更少的带宽。
安全:可以使用scp、ssh等方式来传输文件,当然也可以通过直接的socket连接。
支持匿名传输,以方便进行网站镜像。
3. rsync的ssh认证协议
rsync命令来同步系统文件之前要先登录remote主机认证,认证过程中用到的协议有2种:
ssh协议
rsync协议
rsync server端不用启动rsync的daemon进程,只要获取remote host的用户名和密码就可以直接rsync同步文件
rsync server端因为不用启动daemon进程,所以也不用配置文件/etc/rsyncd.conf
ssh认证协议跟scp的原理是一样的,如果在同步过程中不想输入密码就用ssh-keygen -t rsa打通通道
//这种方式默认是省略了 -e ssh 的,与下面等价:
rsync -avz /SRC -e ssh root@172.16.12.129:/DEST
-a //文件宿主变化,时间戳不变
-z //压缩数据传输
-v //显示详细信息
//当遇到要修改端口的时候,我们可以:
rsync -avz /SRC -e “ssh -p2222” root@172.16.12.129:/DEST
//修改了ssh 协议的端口,默认是22
4. rsync命令详解
rsync常用的命令格式:
rsync [OPTION]… SRC DEST
rsync [OPTION]… SRC [USER@]HOST:DEST
rsync [OPTION]… [USER@]HOST:SRC DEST
对应于以上三种命令格式,rsync有三种不同的工作模式:
1.拷贝本地文件。当SRC和DES路径信息都不包含有单个冒号":"分隔符时就启动这种工作模式
如:rsync -a /abc /def
相当使用cp命令,将文件重新拷贝一份并重命名
2.使用一个远程shell程序(如rsh、ssh)来实现将本地机器的内容拷贝到远程机器。当DST路径地址包含单个冒号":"分隔符时启动该模式
如:rsync -avz a.sh 192.168.207.130:/root/b.sh
3.使用一个远程shell程序(如rsh、ssh)来实现将远程机器的内容拷贝到本地机器。当SRC地址路径包含单个冒号":"分隔符时启动该模式
如:rsync -avz 192.168.207.130:/root/abc /root/def
rsync命令常用选项:
-a 参数,相当于-rlptgoD
-r 是递归;-l 是链接文件,意思是拷贝链接文件;-p 表示保持文件原有权限;
-t 保持文件原有时间;-g 保持文件原有用户组;-o 保持文件原有属主;-D 相当于块设备文件
-v 传输时的进度等信息
-q 精简输出模式
-z 传输时压缩
–delete 在源服务器上做的删除操作也会在目标服务器上同步
5. rsync+inotify
基于概述中所介绍的 rsync 不足的地方 rsync+inotify 组合出现了。
Inotify 是一个 Linux 内核特性,是一种强大的、细粒度的、异步的文件系统事件监控机制。linux内核从2.6.13起,加入了 Inotify 支持,通过 Inotify 可以监控文件系统中添加、删除,修改、移动等各种细微事件,利用这个内核接口,第三方软件就可以监控文件系统下文件的各种变化情况,而 inotify-tools 就是这样的一个第三方软件
rsync可以实现触发式的文件同步,但是通过crontab守护进程方式进行触发,同步的数据和实际数据会有差异,而inotify可以监控文件系统的各种变化,当文件有任何变动时,就触发rsync同步,这样刚好解决了同步数据的实时性问题
配置详解
环境:
类型 | 主机名 | IP | 应用 |
---|---|---|---|
源服务器 | yei1 | 192.168.116.145 | rsync inotify-tools 脚本 |
目标服务器 | yei2 | 192.168.116.181 | rsync |
1. 在目标服务器上安装rsync服务端软件,并修改配置文件
[root@yei2 ~]# yum -y install rsync
[root@yei2 ~]# cat >> /etc/rsyncd.conf <<EOF
//添加以下内容,并按需修改
# 日志文件位置,启动rsync后自动产生这个文件,无需提前创建
log file = /var/log/rsyncd.log
# pid文件的存放位置
pidfile = /var/run/rsyncd.pid
# 支持max connections参数的锁文件
lock file = /var/run/rsync.lock
# 用户认证配置文件,里面保存用户名称和密码,必须手动创建这个文件
secrets file = /etc/rsync.pass
# 自定义同步名称
[runtime]
# rsync目标服务端数据存放路径,源服务端的数据将同步至此目录
path = /tmp
comment = sync etc from client
# 设置rsync运行权限为root
uid = root
# 设置rsync运行权限为root
gid = root
# 默认端口
port = 873
# 表示出现错误忽略错误
ignore errors
# 默认为true,修改为no,增加对目录文件软连接的备份
use chroot = no
# 设置rsync服务端为读写权限
read only = no
# 不显示rsync服务端资源列表
list = no
# 最大连接数
max connections = 200
# 设置超时时间
timeout = 600
# 执行数据同步的用户名,可以设置多个,用英文状态下逗号隔开
auth users = admin
# 允许进行数据同步的源服务端IP地址,可以设置多个,用英文状态下逗号隔开
hosts allow = 192.168.116.145
# 禁止数据同步的源服务端IP地址,可以设置多个,用英文状态下逗号隔开
hosts deny = 192.168.1.1
EOF
2. 在目标服务器上创建用户认证文件,并启动服务
[root@yei2 ~]# echo 'admin:123' > /etc/rsync.pass
[root@yei2 ~]# chmod 600 /etc/rsync*
[root@yei2 ~]# systemctl enable --now rsyncd
[root@yei2 ~]# ss -antl |grep 873
LISTEN 0 5 *:873 *:*
LISTEN 0 5 :::873 :::*
3. 在源服务器上安装rsync服务端软件,创建认证密码文件
[root@yei1 ~]# yum -y install rsync
[root@yei1 ~]# echo '123' > /etc/rsync.pass
[root@yei1 ~]# chmod 600 /etc/rsync.pass
4. 在源服务器上创建同步目录,并手动同步
[root@yei1 ~]# mkdir -p runtime/test
[root@yei1 ~]# ls rumtime/
test
[root@yei1 ~]# rsync -avH --port 873 --progress --delete /root/runtime/* admin@192.168.116.181::runtime --password-file=/etc/rsync.pass
sending incremental file list
test/
sent 62 bytes received 24 bytes 172.00 bytes/sec
total size is 0 speedup is 0.00
//在目标服务器上查看
[root@yei2 ~]# ls /tmp
test
6. 安装inotify-tools工具
//因为inotify-tools在epel源中,所以我们先配yum源
[root@yei1 ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
[root@yei1 ~]# sed -i s/\$releasever/7/g /etc/yum.repos.d/CentOS-Base.repo
[root@yei1 ~]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[root@yei1 ~]# yum -y install epel-release
//安装inotify-tools
[root@yei1 ~]# yum -y install inotify-tools
7. 编写同步脚本并开机自启
[root@yei1 ~]# mkdir /scripts
[root@yei1 ~]# vim /scripts/inotify.sh
# 目标服务器的ip(备份服务器)
host=192.168.116.181
# 在源服务器上所要监控的备份目
src=/root/runtime
# 自定义的模块名,需要与目标服务器上定义的同步名称一致
des=runtime
# 执行数据同步的密码文件
password=/etc/rsync.pass
# 执行数据同步的用户名
user=admin
inotifywait=/usr/bin/inotifywait
$inotifywait -mrq --timefmt '%Y%m%d %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src \
| while read files;do
rsync -avzP --delete --timeout=100 --password-file=${password} $src $user@$host::$des
echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
done
//启动脚本
[root@yei1 ~]# nohup bash /scripts/inotify.sh &
[1] 11298
[root@yei1 ~]# nohup: ignoring input and appending output to ‘nohup.out’
[root@yei1 ~]# ps -ef |grep inotify
root 11309 1610 0 21:34 pts/1 00:00:00 bash /scripts/inotify.sh
root 11310 11309 0 21:34 pts/1 00:00:00 /usr/bin/inotifywait -mrq --timefmt %Y%m%d %H:%M --format %T %w%f%e -e modify,delete,create,attrib /root/runtime
root 11311 11309 0 21:34 pts/1 00:00:00 bash /scripts/inotify.sh
root 11313 1610 0 21:34 pts/1 00:00:00 grep --color=auto inotify
[root@yei1 ~]# echo 'nohup bash /scripts/inotify.sh &'>> /etc/rc.d/rc.local
[root@yei1 ~]# chmod +x /etc/rc.d/rc.local
8. 验证
[root@yei1 ~]# ls runtime/
test
[root@yei1 ~]# touch runtime/abc
[root@yei1 ~]# ls runtime/
abc test
//目标服务器上查看
[root@yei2 ~]# ls /tmp/runtime/
abc test