1.rsync介绍
rsync(remove sync)是linux系统的数据镜像备份工具,快速增量备份,远程同步,支持本地复制,于其它SSH,rsync主机同步。
2.rsync特性
支持的特性:
- 镜像保存整个目录和文件系统
- 保留文件的权限,时间,软硬链接等
- 优化流程,第一次同步时rsync会复制全部内容,在下一次只会传送修改的内容
- 可以使用rsh,ssh方式来传输文件,也可以通过socket连接
- 支持匿名传输,方便网站镜像在windows有相应版本
3.认证协议
认证过程中要用到的协议有ssh,rsync协议
ssh协议和scp原理一样,不需要配置文件,以及daemon,获取对端用户密码即可使用
4.同步用法
语法:
rsync [OPTION]… SRC [SRC]… [USER@]HOST:DEST
rsync [OPTION]… SRC [SRC]… [USER@]HOST::DEST
rsync [OPTION]… SRC [SRC]… rsync://[USER@]HOST[:PORT]/DEST参数:
-a //归档,时间不变
-z //压缩数据传输
-v //冗长增加冗长,详细信息
-q //静默模式
-r //递归
-p //保持原有的权限属性
-z //传输时压缩,节省带宽,加快速度
-delete //源主机上文件的删除操作,会在目标机器上同步
-c //自动忽略相同的文件
在同步过程中,如果不想输入密码,用免密登录ssh-keygen -t rsa
俩种最常用的方式
#第一种没有-e ssh参数(适合默认22ssh端口)
#安装rsync
[root@zabbix ~]# yum -y install rsync*
#把服务器上的/test/目录同步更新到对端主机上,同时服务端的删除操作也会同步
[root@zabbix test]# pwd
/test
[root@zabbix test]# mkdir 1.1 3.3
[root@zabbix test]# rsync -avz --delete /test/ root@192.168.136.129:/test/
root@192.168.136.129's password:
sending incremental file list
sent 83 bytes received 12 bytes 38.00 bytes/sec
total size is 0 speedup is 0.00
#客户端上查看同步文件
[root@localhost test]# pwd
/test
[root@localhost test]# ls
1.1 3.3
#第二种(适合修改的端口)
rsync -avz test -e ssh -p5555 root@192.168.136.129:/tmp/
5.rsync+inotify
rsync相比于scp,tar备份比较,具有安全性高,备份快,支持增量备份等,可以用在定期备份文化服务器数据到远端服务器,对本地磁盘做数据镜像等。
但是sync也有相应的缺点,比如:
1.数据非常庞大,进行同步的时候会扫描所有文件是非常耗时的,而且只会同步修改的部分,这就很低效
2.rsync不能实时监测,同步数据,虽然可以触发同步,但是次数多了必定会发生延迟,可能会导致数据出现不一样,无法恢复完全的数据,因此inotify出现了
inotify介绍
inotify是细粒度,异步的文件系统事件监控机制,linux内核2.6.13加入了inotify机制,inotify可以对监控的文件进行添加,删除,修改,移动等情况,inotify-tools第三方软件正有此功能。
rsync是可以通过crontab守护进程触发同步,但是数据可能会有差异的变化,而inotify可以监控系统文件的各种变化,只要文件发生改变就触发更新同步。
6.inotify的使用
环境(一对一)
服务器 | ip | 应用 | 系统 |
---|---|---|---|
服务端 | 192.168.136.242 | rsync|inotify-tools|脚本 | centos8 |
客户端 | 192.168.136.230 | rsync rsync-daemon.noarch | centos8 |
要求:把服务端的/test目录实时同步到目标服务器的/tmp下
客户端操作
[root@mysql-slave ~]# systemctl --now disable firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@mysql-slave ~]# sed -r 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
[root@mysql-slave ~]# setenforce 0
#安装rsync服务端软件
[root@mysql-slave ~]# yum -y install rsync
[root@mysql-slave ~]# yum -y install rsync-daemon.noarch
#开启服务
[root@mysql-slave ~]# systemctl --now enable rsyncd
Created symlink /etc/systemd/system/multi-user.target.wants/rsyncd.service → /usr/lib/systemd/system/rsyncd.service.
[root@mysql-slave ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 5 0.0.0.0:873 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 5 [::]:873 [::]:*
LISTEN 0 80 *:3306 *:*
#配置rsyncd.conf文件
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 # 用户认证配置文件,里面保存用户名称和密码,必须手动创建这个文件
[etc_from_client] # 自定义同步名称
path = /tmp/ # 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 =xxx # 允许进行数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
#hosts deny = xxx # 禁止数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
EOF
#创建认证的用户和密码文件
[root@mysql-slave ~]# echo 'admin:123456' > /etc/rsync.pass
[root@mysql-slave ~]# cat /etc/rsync.pass
admin:123456
#设置文件权限(只具备读写权限)
[root@mysql-slave ~]# chmod 600 /etc/rsync*
[root@mysql-slave ~]# ll /etc/rsync*
-rw-------. 1 root root 1845 10月 11 22:13 /etc/rsyncd.conf
-rw-------. 1 root root 13 10月 11 22:14 /etc/rsyncd.pass
#重启服务
[root@mysql-slave ~]# systemctl restart rsyncd.service
服务端操作
#关闭防火墙
[root@mysql-master ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@mysql-master ~]# setenforce 0
[root@mysql-master ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
#安装epel源
[root@mysql-master ~]# yum -y install epel-release
#安装rsync
[root@mysql-master ~]# yum -y install rsync*
#创建认证密码文件(只需要密码)
[root@mysql-master ~]# echo '123456' > /etc/rsync.pass
[root@mysql-master ~]# cat /etc/rsync.pass
123456
#修改文件权限(只读写权限)
[root@mysql-master ~]# chmod 600 /etc/rsync*
#先来简单测试一下
[root@mysql-master ~]# mkdir /test/test01 -p
[root@mysql-master ~]# rsync -avH --port 873 --progress --delete /test/test01 admin@192.168.136.230::etc_from_client --password-file=/etc/rsync.pass
sending incremental file list
test01/
sent 63 bytes received 24 bytes 174.00 bytes/sec
total size is 0 speedup is 0.00
#在客户端上的/tmp目录下查看文件,有则表示测试成功
[root@mysql-slave tmp]# ls
test01
#安装inotify-tools工具实时同步
#有max开头文件,则表示服务器内核是支持inotify的,否则不支持
[root@mysql-master ~]# ll /proc/sys/fs/inotify/
总用量 0
-rw-r--r--. 1 root root 0 10月 12 12:55 max_queued_events
-rw-r--r--. 1 root root 0 10月 12 12:55 max_user_instances
-rw-r--r--. 1 root root 0 10月 12 12:55 max_user_watches
#安装inotify-tools工具
[root@mysql-master ~]# yum -y install inotify-tools
#编写脚本
[root@mysql-master scripts]# vim inotify.sh
#!/bin/bash
host=192.168.136.230 //目标主机ip
src=/test //本机要监控的备份目录(可自定义)
des=etc_from_client //自定义的模块,于目标主机配置文件定义的同步名称一样
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 \ //监控的各类事件,以及格式,对于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@mysql-master scripts]# chmod +x inotify.sh
#执行脚本
[root@mysql-master scripts]# ./inotify.sh
//它会一直卡在这里,等待触发进行同步过程,只有当脚本中的src指定的目录进行修改,添加等操作才会进行同步过程
#本机的/test目录添加内容,触发更新同步,再到目标端的/tmp目录内查看是否成功
#创建内容并且修改权限
[root@mysql-master test]# touch 1.1
[root@mysql-master test]# chmod 770 1.1
[root@mysql-master scripts]# ./inotify.sh
sending incremental file list
test/
test/1.1
0 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=0/2)
sent 118 bytes received 47 bytes 19.41 bytes/sec
total size is 0 speedup is 0.00
sending incremental file list
sent 79 bytes received 25 bytes 208.00 bytes/sec
total size is 0 speedup is 0.00 //此刻控制端脚本触发同步更新
#目标端查看
[root@mysql-slave tmp]# pwd
/tmp
[root@mysql-slave test]# ll021.
-rwxrwx--- 1 root root 0 10月 12 13:28 1.1 //同步成功
服务端上脚本的后台启动
之前脚本的执行是放在前台运行,必须要重新开一个终端,因此很麻烦,所以我们要把脚本放在后台执行,并且还是能够开机自启的那种,才算合格的同步更新功能
#后台运行脚本
[root@mysql-master scripts]# nohup bash inotify.sh &
[2] 190840
[root@mysql-master scripts]# nohup: 忽略输入并把输出追加到'nohup.out'
[root@mysql-master scripts]# ps -ef|grep inotify
root 184470 1469 0 15:07 pts/0 00:00:00 bash inotify.sh
root 184471 184470 0 15:07 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 /test
root 184472 184470 0 15:07 pts/0 00:00:00 bash inotify.sh
root 190840 1469 0 15:09 pts/0 00:00:00 bash inotify.sh
root 190841 190840 0 15:09 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 /test
root 190842 190840 0 15:09 pts/0 00:00:00 bash inotify.sh
root 191719 1469 0 15:09 pts/0 00:00:00 grep --color=auto inotify //很明显放在了后台,而且inotify的进程也在运行
#控制端创建内容
[root@mysql-master test]# echo 'hello world' > 5.5
[root@mysql-master test]# cat 5.5
hello world
#目标端查看内容
[root@mysql-slave test]# cat 5.5
hello world
#查看inotify日志内容
[root@mysql-master scripts]# cat nohup.out
test/5.5
12 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=0/6)
sent 208 bytes received 47 bytes 510.00 bytes/sec
total size is 12 speedup is 0.05
sending incremental file list
sent 153 bytes received 25 bytes 356.00 bytes/sec
total size is 12 speedup is 0.07
服务端上脚本的自启动
/etc/rc.d/rc.local 此文件会随着系统执行而执行,而且是最后一个必须执行的
[root@mysql-master ]# chmod +x /etc/rc.d/rc.local
[root@mysql-master ]# vim /etc/rc.d/rc.local
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# 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 &
#重启
[root@mysql-master test]# reboot
#把服务端/test目录下的文件全部删掉
[root@mysql-master test]#rm -rf *
#目标端的/tmp目录下查看情况
[root@mysql-slave test]# pwd
/tmp/test
[root@mysql-slave test]# ls
7.练习
部署rsync+inotify,同步服务端的/2021-1012目录到客户端的/NAME下实时同步更新以及监控。
#客户端
[root@slave03 ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@slave03 ~]# setenforce 0
[root@slave03 ~]# yum -y install rsync-daemon
[root@slave03 ~]# systemctl --now enable rsyncd
Created symlink /etc/systemd/system/multi-user.target.wants/rsyncd.service → /usr/lib/systemd/system/rsyncd.service.
[root@slave03 ~]# cat /etc/rsyncd.conf
log file = /var/log/rsyncd.log
pidfile = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
secrets file = /etc/rsync.pass
[etc_from_client]
path = /tmp/
comment = sync etc from client
uid = root
gid = root
port = 873
ignore errors
use chroot = no
read only = no
list = no
max connections = 200
timeout = 600
auth users = admin
[root@slave03 ~]# mkdir /NAME
[root@slave03 ~]# echo 'admin:123456' > /etc/rsync.pass
[root@slave03 ~]# chmod 600 /etc/rsync*
[root@slave03 ~]# ll -d /etc/rsync*
-rw-------. 1 root root 1842 10月 12 19:29 /etc/rsyncd.conf
-rw-------. 1 root root 13 10月 12 19:30 /etc/rsync.pass
[root@slave03 ~]# systemctl restart rsyncd
#服务端
[root@master ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@master ~]# setenforce 0
[root@master ~]# yum -y install epel-release rsync
[root@master ~]# echo '123456' > /etc/rsync.pass
[root@master ~]# chmod 600 /etc/rsync.pass
[root@master ~]# yum -y install inotify-tools
[root@master ~]# mkdir /scripts
[root@master ~]# cd /scripts/
[root@master scripts]# cat inotify.sh
host=192.168.136.230
src=/2021-1012
des=etc_from_client
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@master scripts]# chmod +x inotify.sh
[root@master scripts]# chmod +x /etc/rc.d/rc.local
[root@master scripts]# vim /etc/rc.d/rc.local
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# 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 &
#服务端在/2021-1012/目录创建几个文件
[root@master ~]# cd /2021-1012/
[root@master 2021-1012]# touch 1.1 2.2 3.3
#客户端上查看已经存在目录以及文件
[root@slave03 ~]# tree /NAME/2021-1012/
/NAME/2021-1012/
├── 1.1
├── 2.2
└── 3.3 //同步成功
[root@master ~]# reboot
[root@master ~]# ps -ef|grep inotify
root 932 1 0 20:44 ? 00:00:00 /bin/bash /scripts/inotify.sh
root 938 932 0 20:44 ? 00:00:00 /usr/bin/inotifywait -mrq --timefmt %Y%m%d %H:%M --format %T %w%f%e -e modify,delete,create,attrib /2021-1012
root 939 932 0 20:44 ? 00:00:00 /bin/bash /scripts/inotify.sh
root 1249 1125 0 20:45 pts/0 00:00:00 grep --color=auto inotify
//重启自启动脚本实时监控成功