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 [email protected]:/DEST
-a //文件宿主变化,时间戳不变
-z //压缩数据传输
//当遇到要修改端口的时候,我们可以:
rsync -avz /SRC -e "ssh -p2222" [email protected]:/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 nfs.sh a.sh
2.使用一个远程shell程序(如rsh、ssh)来实现将本地机器的内容拷贝到远程机器。当DST路径地址包 \
含单个冒号":"分隔符时启动该模式 。
rsync -avz nfs.sh [email protected]:/root/b.sh
3.使用一个远程shell程序(如rsh、ssh)来实现将远程机器的内容拷贝到本地机器。当SRC地址路径 \
包含单个冒号":"分隔符时启动该模式。
rsync -avz [email protected]:/etc/yum.repos.d /root/
rsync常用选项:
-a, --archive //归档
-v, --verbose //啰嗦模式
-q, --quiet //静默模式
-r, --recursive //递归
-p, --perms //保持原有的权限属性
-z, --compress //在传输时压缩,节省带宽,加快传输速度
--delete //在源服务器上做的删除操作也会在目标服务器上同步
5. rsync+inotify
Inotify是一种强大的、细粒度的、异步的文件系统事件监控机制,linux内核从2.6.13起,加入了Inotify支持,通过Inotify可以监控文件系统中添加、删除,修改、移动等各种细微事件,利用这个内核接口,第三方软件就可以监控文件系统下文件的各种变化情况,而inotify-tools就是这样的一个第三方软件。当文件有任何变动时,就触发rsync同步,这样刚好解决了同步数据的实时性问题。
环境
服务器类型 | IP地址 | 应用 | 名称 |
---|---|---|---|
源服务器 | 192.168.5.128 | rsync inotify-tools 脚本 |
wyt1 |
目标服务器 | 192.168.5.129 | rsync | wyt2 |
需求:把源服务器上/runtime目录至目标服务器的/wyt/下
配置目标服务端
关闭防火墙和SELINUX
[root@wyt2 ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead)
[root@wyt2 ~]# getenforce
Disabled
安装软件包
[root@wyt2 ~]# yum -y install rsync
设置rsyncd.conf配置文件
[root@wyt2 ~]# cat >> /etc/rsyncd.conf <<EOF
> log file = /var/log/rsyncd.log # 日志文件位置,启动rsync后自动产生这个文件,无需提前创 建
> pidfile = /var/run/rsyncd.pid # pid文件的存放位置
> lock file = /var/run/rsync.lock # 支持max connections参数的锁文件
> secrets file = /etc/rsync.pass # 用户认证配置文件,里面保存用户名称和密码,必须手动创建这个文件
>
> [runtime] # 自定义同步名称
> path = /wyt/ # rsync服务端数据存放路径,客户端的数据将同步至此目录
> comment = sync etc from client
> uid = root # 设置rsync运行权限为root
> gid = root # 设置rsync运行权限为root
> port = 873 # 默认端口
> ignore errors # 表示出现错误忽略错误
> use chroot = no # 默认为true,修改为no,增加对目录文件软连接的备份
> read only = no # 设置rsync服务端为读写权限
> list = no # 不显示rsync服务端资源列表
> max connections = 200 # 最大连接数
> timeout = 600 # 设置超时时间
> auth users = admin # 执行数据同步的用户名,可以设置多个,用英文状态下逗号隔开
> hosts allow = 192.168.5.128 # 允许进行数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
[root@wyt2 ~]# mkdir /wyt/ //创建数据存放路径
[root@wyt2 ~]# chmod 777 /wyt //修改权限
drwxrwxrwx 3 root root 21 7月 30 04:17 wyt
创建用户认证文件并修改文件权限
[root@wyt2 ~]# echo 'admin:123456' > /etc/rsync.pass
[root@wyt2 ~]# cat /etc/rsync.pass
admin:123456
[root@wyt2 ~]# chmod 600 /etc/rsync.pass
[root@wyt2 ~]# ll /etc/rsync.pass
-rw------- 1 root root 13 7月 30 03:41 /etc/rsync.pass
启动rsync服务并设置开机自启动
[root@wyt2 ~]# systemctl enable --now rsyncd
Created symlink from /etc/systemd/system/multi-user.target.wants/rsyncd.service to /usr/lib/systemd/system/rsyncd.service.
[root@wyt2 ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 5 *:873 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 [::1]:25 [::]:*
LISTEN 0 5 [::]:873 [::]:*
LISTEN 0 128 [::]:22 [::]:*
配置源服务端
配置yum源
[root@wyt1 ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
[root@wyt1 ~]# sed -i 's/\$releasever/7/g' /etc/yum.repos.d/CentOS-Base.repo
[root@wyt1 ~]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
安装软件包
[root@wyt1 ~]# yum -y install epel-release rsync
[root@wyt1 ~]# mkdir /runtime //创建目录及文件
[root@wyt1 ~]# tree /runtime
/runtime
├── abc
│ ├── ef
│ └── qw
│ └── index.html
├── hehe
│ ├── a
│ ├── c
│ └── w
├── hihi
└── xixi
关闭防火墙和SELINUX
[root@wyt1 ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead)
[root@wyt1 ~]# getenforce
Disabled
创建认证密码文件并修改权限
[root@wyt1 ~]# echo '123456' > /etc/rsync.pass
[root@wyt1 ~]# cat /etc/rsync.pass
123456
[root@wyt1 ~]# chmod 600 /etc/rsync.pass
[root@wyt1 ~]# ll /etc/rsync.pass
-rw------- 1 root root 7 7月 30 03:49 /etc/rsync.pass
在源服务器运行以下命令
[root@wyt1 ~]# rsync -avH --port 873 --progress --delete /runtime [email protected]::runtime --password-file=/etc/rsync.pass
sending incremental file list
runtime/
runtime/hihi
0 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=9/11)
runtime/xixi
0 100% 0.00kB/s 0:00:00 (xfr#2, to-chk=8/11)
runtime/abc/
runtime/abc/ef
0 100% 0.00kB/s 0:00:00 (xfr#3, to-chk=5/11)
runtime/abc/qw/
runtime/abc/qw/index.html
12 100% 0.00kB/s 0:00:00 (xfr#4, to-chk=3/11)
runtime/hehe/
runtime/hehe/a
0 100% 0.00kB/s 0:00:00 (xfr#5, to-chk=2/11)
runtime/hehe/c
0 100% 0.00kB/s 0:00:00 (xfr#6, to-chk=1/11)
runtime/hehe/w/
sent 564 bytes received 158 bytes 1,444.00 bytes/sec
total size is 12 speedup is 0.02
[root@wyt2 ~]# ls /wyt //目标服务器查看同步文件
runtime
实时触发rsync进行同步
//查看服务器内核是否支持inotify
[root@server ~]# ll /proc/sys/fs/inotify/
总用量 0
-rw-r--r-- 1 root root 0 7月 29 06:14 max_queued_events
-rw-r--r-- 1 root root 0 7月 29 06:14 max_user_instances
-rw-r--r-- 1 root root 0 7月 29 06:14 max_user_watches
root@wyt1 ~]# cd /runtime
[root@wyt1 runtime]# tree
.
├── abc
│ ├── ef
│ └── qw
│ └── index.html
├── hehe
│ ├── a
│ ├── c
│ └── w
├── hihi
└── xixi
4 directories, 6 files
[root@wyt1 runtime]# rm -rf hehe/a
[root@wyt1 runtime]# rsync -avH --port 873 --progress --delete /runtime [email protected]::runtime --password-file=/etc/rsync.pass
sending incremental file list
deleting runtime/hehe/a
runtime/hehe/
sent 285 bytes received 46 bytes 220.67 bytes/sec
total size is 12 speedup is 0.04
[root@wyt2 ~]# ls /wyt
runtime
[root@wyt2 ~]# tree /wyt
/wyt
└── runtime
├── abc
│ ├── ef
│ └── qw
│ └── index.html
├── hehe
│ ├── c
│ └── w
├── hihi
└── xixi
5 directories, 5 files
写同步脚本
//安装inotify-tools
[root@localhost ~]# yum -y install make gcc gcc-c++
[root@localhost ~]# yum -y install inotify-tools
[root@wyt1 ~]# cd /scripts/
[root@wyt1 scripts]# vim inotify.sh
[root@wyt1 scripts]# cat inotify.sh
/#!/bin/bash
host=192.168.5.129
src=/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@wyt1 scripts]# chmod +x inotify.sh
启动脚本
root@wyt1 scripts]# nohup /bin/bash /scripts/inotify.sh &
[1] 1441
[root@wyt1 scripts]# nohup: 忽略输入并把输出追加到"nohup.out"
[root@wyt1 scripts]# ps -ef|grep inotify
root 1441 1206 0 05:39 pts/0 00:00:00 /bin/bash /scripts/inotify.sh
root 1442 1441 0 05:39 pts/0 00:00:00 /usr/bin/inotifywait -mrq --timefmt %Y%m%d %H:%M --format %T %w%f%e -e modify,delete,create,attrib /runtime
root 1443 1441 0 05:39 pts/0 00:00:00 /bin/bash /scripts/inotify.sh
root 1445 1206 0 05:39 pts/0 00:00:00 grep --color=auto inotify
在源服务器上生成一个新文件
[root@wyt1 runtime]# echo 'hello word' > xixi
[root@wyt1 runtime]# echo 'haha' > test
[root@wyt1 runtime]# rm -rf hihi
查看inotify生成的日志
[root@wyt1 ~]# tail /tmp/rsync.log
20200730 05:43 /runtime/xixiMODIFY was rsynced
20200730 05:45 /runtime/testMODIFY was rsynced
20200730 05:45 /runtime/hihiDELETE was rsynced
设置脚本开机自动启动
[root@wyt1 ~]# chmod +x /etc/rc.d/rc.local
[root@wyt1 ~]# ll /etc/rc.d/rc.local
-rwxr-xr-x. 1 root root 473 8月 8 2019 /etc/rc.d/rc.local
[root@wyt1 ~]# echo 'nohup /bin/bash /scripts/inotify.sh' >> /etc/rc.d/rc.local
[root@wyt1 ~]# tail /etc/rc.d/rc.local
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.
touch /var/lock/subsys/local
nohup /bin/bash /scripts/inotify.sh //添加此行
同步数据
3 directories, 4 files
[root@wyt1 runtime]# rm -rf hehe/w
[root@wyt1 runtime]# tree
.
├── abc
│ └── ef
├── hehe
│ └── c
├── test
└── xixi
2 directories, 4 files
[root@wyt2 runtime]# ls
abc hehe test xixi
[root@wyt2 runtime]# tree
.
├── abc
│ └── ef
├── hehe
│ └── c
├── test
└── xixi