rsync数据镜像备份服务

rsync

1、rsync简介

rsync是linux系统下的数据镜像备份工具。使用快速增量备份工具Remote Sync可以远程同步,支持本地复制,或者与其他SSHrsync主机同步。

Rsync监听端口:

Rsync工具默认运行在TCP的873端口。

Rsync工作模式:

Rsync三种工作模式:
1.本地模式:不常用,类似cp命令;
2.远程模式:常用,在不同主机之间传输数据;
3.后台服务模式:常用,用于实时同步数据,安全性高。

2、rsync特性

rsync支持很多特性:

  • 可以镜像保存整个目录树和文件系统
  • 可以很容易做到保持原来文件的权限、时间、软硬链接等等
  • 无须特殊权限即可安装
  • 快速:第一次同步时rsync会复制全部内容,但在下一次只传输修改过的文件。rsync在传输数据的过程中可以实行压缩及解压缩操作,因此可以使用更少的带宽
  • 安全:可以使用scpssh等方式来传输文件,当然也可以通过直接的socket连接
  • 支持匿名传输,以方便进行网站镜像

3、rsync的ssh认证协议

rsync命令来同步系统文件之前要先登录remote主机认证,认证过程中用到的协议有2种:

  • ssh协议
  • rsync协议

rsync server端不用启动rsyncdaemon进程,只要获取remote host的用户名和密码就可以直接rsync同步文件。
rsync server端因为不用启动daemon进程,所以也不用配置文件/etc/rsyncd.conf

ssh认证协议跟scp的原理是一样的,如果在同步过程中不想输入密码就用ssh-keygen -t rsa打通通道

//这种方式默认是省略了 -e ssh 的,与下面等价:
rsync -avz /SRC -e ssh root@192.168.92.130:/DEST 
    -a  //文件宿主变化,时间戳不变
    -z  //压缩数据传输
 
//当遇到ssh端口修改了的时候,我们可以:
rsync -avz /SRC -e "ssh -p2222" root@192.168.92.130:/DEST  
//修改了ssh 协议的端口,默认是22

4、rsync本地模式与远程模式

环境准备

操作系统IP地址
Server端:Centos8
Client端:Centos8
192.168.92.131
192.168.92.132

4.1 安装rsync工具

//在服务端和客户端都安装rsync工具
[root@server ~]# dnf -y install rsync
[root@client ~]# dnf -y install rsync

4.2 rsync命令选项

//Rsync的命令格式常用的有以下三种:
    rsync [OPTION]... SRC DEST					//本地
    rsync [OPTION]... SRC [USER@]HOST:DEST		//远程推
    rsync [OPTION]... [USER@]HOST:SRC DEST		//远程拉

-a	//归档模式传输,等于-tropglD
-v	//详细模式输出,显示速率,文件数量等
-z	//传输时进行压缩,提高效率
-r	//递归传输,传输目录,传输目录时目录名称后加"/"表示传输目录下的所有文件
-t	//保持文件时间信息
-o	//保持文件属主信息
-g	//保持文件属组信息
-p	//保持文件权限
-l	//保留软链接
-H	//保持硬链接
-n 	//进行试运行,不作任何更改
-D	//保持设备文件信息
-P	//显示同步的过程及传输时的进度等信息
-L	//保留软连接指向的目标文件
-e ssh 					//使用SSH加密隧道传输
--delete				//让目标目录和源目录数据保持一致
--delete-excluded 		//指定要在目的端删除的文件
--delete-after 			//默认情况下,rsync是先清理目的端的文件再开始数据同步;如果使用此选项,则rsync会先进行数据同步,都完成后再删除那些需要清理的文件。
--bwlimit				//限速传输
--exclude=PATTERN		//指定排除不需要传输的文件模式
--exclude-from=FILE		//排除FILE中指定模式的文件

4.3 本地模式用法

拷贝本地文件。当SRC和DES路径信息都不包含有单个冒号":"分隔符时就启动这种工作模式。

	rsync [OPTION...] SRC... [DEST]

示例:
//备份本地文件至其他位置
[root@localhost ~]# touch file1
[root@localhost ~]# mkdir dir1
[root@localhost ~]# rsync -avz file1 dir1/
sending incremental file list
file1

sent 84 bytes  received 35 bytes  238.00 bytes/sec
total size is 0  speedup is 0.00
[root@localhost ~]# ls dir1/
file1

//备份目录至其它位置
[root@localhost ~]# mkdir dir2
[root@localhost ~]# touch dir1/file{2..6}
[root@localhost ~]# ls dir1
file1  file2  file3  file4  file5  file6
[root@localhost ~]# ll dir2
total 0
#这里需注意!源目录名后面不带/是把目录本身及目录下的复制过去,带/是复制目录下的但不包含目录本身。
[root@localhost ~]# rsync -avz dir1/ dir2/
sending incremental file list
./
file1
file2
file3
file4
file5
file6

sent 350 bytes  received 133 bytes  966.00 bytes/sec
total size is 0  speedup is 0.00
[root@localhost ~]# ls dir2
file1  file2  file3  file4  file5  file6
//在dir1增加内容,然后使用rsync增加备份至dir2
[root@localhost ~]# touch dir1/abc
[root@localhost ~]# ls dir1/
abc  file1  file2  file3  file4  file5  file6
[root@localhost ~]# ls dir2/
file1  file2  file3  file4  file5  file6
[root@localhost ~]# rsync -avx dir1/ dir2/
sending incremental file list
./
abc

sent 192 bytes  received 38 bytes  460.00 bytes/sec
total size is 0  speedup is 0.00
[root@localhost ~]# ls dir2/
abc  file1  file2  file3  file4  file5  file6

//备份目录时,使目标目录与源目录数据保持一致;若目标目录有多余的文件则会被删除
[root@localhost ~]# rm -f dir1/file{3..5}
[root@localhost ~]# ls dir1/
abc  file1  file2  file6
[root@localhost ~]# ls dir2/
abc  file1  file2  file3  file4  file5  file6
[root@localhost ~]# rsync -avz --delete dir1/ dir2/
sending incremental file list
deleting file5
deleting file4
deleting file3
./

sent 116 bytes  received 46 bytes  324.00 bytes/sec
total size is 0  speedup is 0.00
[root@localhost ~]# ls dir2/
abc  file1  file2  file6

4.4 远程模式用法

使用一个远程shell程序(如rsh、ssh)来实现将本地机器的内容拷贝到远程机器。当DST路径地址包含单个冒号":"分隔符时启动该模式。

	rsync [OPTION...] [USER@]HOST:SRC... [DEST]
	rsync [OPTION...] SRC... [USER@]HOST:DEST

示例:
//将本地文件备份至其他主机
[root@client ~]# rsync -avz abc.txt root@192.168.92.131:/root/
The authenticity of host '192.168.92.131 (192.168.92.131)' can't be established.
ECDSA key fingerprint is SHA256:41MUAgoOJ7cipkGboXt2n0BlrxuPxp2IVlgXn0ahNgg.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes	//首先连接需要回答yes
Warning: Permanently added '192.168.92.131' (ECDSA) to the list of known hosts.
root@192.168.92.131's password:				//输入目标主机用户的密码
sending incremental file list
abc.txt

sent 85 bytes  received 35 bytes  18.46 bytes/sec
total size is 0  speedup is 0.00
//在目标主机上验证是否备份成功
[root@server ~]# ls
abc.txt  anaconda-ks.cfg  dir1  dir2  file1

//将其他主机文件备份至本地
[root@client ~]# rsync -avz root@192.168.92.131:/root/file1 /root/
root@192.168.92.131's password:			//输入对方主机用户的密码
receiving incremental file list
file1

sent 43 bytes  received 84 bytes  28.22 bytes/sec
total size is 0  speedup is 0.00
[root@client ~]# ls
abc.txt  anaconda-ks.cfg  file1

//传输时限制传输速度
//先用dd命令生成一个大小为1G的文件
[root@client ~]# dd if=/dev/zero of=/root/1G.txt bs=1024M count=1
1+0 records in
1+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 11.5039 s, 93.3 MB/s
[root@client ~]# ll -h 1G.txt
-rw-r--r-- 1 root root 1.0G Aug 27 12:01 1G.txt
[root@client ~]# rsync -avzP 1G.txt root@192.168.92.131:/root/ --bwlimit=10
root@192.168.92.131's password:
sending incremental file list
1G.txt
  1,073,741,824 100%   10.67MB/s    0:01:35 (xfr#1, to-chk=0/1)

sent 1,044,299 bytes  received 35 bytes  9,805.95 bytes/sec
total size is 1,073,741,824  speedup is 1,028.16
//目标主机验证是否备份
[root@server ~]# ll -h
total 1.1G
-rw-r--r--  1 root root 1.0G Aug 27 12:01 1G.txt

4.5 注意事项

注意事项:
//在没有用yum安装rsync-daemon的情况下,可能会无法使用systemctl的方式启动守护进程和开机自启,可采用以下方式
rsync --daemon --config=/etc/rsyncd.conf		//启动rsync的守护进程
echo 'rsync --daemon --config=/etc/rsyncd.conf' >> /etc/rc.d/rc.local	  //加入开机自启

//备份数据时,若不限速会导致带宽被占满
rsync -avzP --bwlimit=10 1G.txt root@192.168.92.131:/root/

//备份目录时,目录名称后不加"/"表示备份整个目录包含目录本身
rsync -avz dir root@192.168.92.131:/root/

//备份目录时,目录名称后加"/"表示只备份目录中的文件,不备份目录本身
rsync -avz dir/ root@192.168.92.131:/root/

//使用"--delete"选项时,请注意千万不要写错目标目录的名称
//下面命令会导致目标系统"/"目录被清空
rsync -avz --delete dir root@192.168.92.131:/ root/

5、rsync服务模式

远程模式在传输数据时使用ssh远程登录,使用的是系统用户(不安全);服务模式以守护进程的方式运行,使用的是虚拟用户,用户不存在操作系统中。

环境准备

操作系统IP地址
Server端:Centos8
Client端:Centos8
192.168.92.131
192.168.92.132

5.1 服务端配置

  • 确保rsync服务已经安装
  • 修改服务配置文件/etc/rsyncd.conf
  • 创建用户以及数据存放目录
  • 创建虚拟用户密码文件并设置权限
  • 关闭防火墙、selinux
  • 重启服务并将服务设置开机自启
  • 检查服务端口是否开启
//安装rsync服务
[root@server ~]# yum -y install rsync rsync-daemon

//编辑服务配置文件/etc/rsyncd.conf
[root@server ~]# vim /etc/rsyncd.conf
uid = rsync							//运行服务的用户
gid = rsync							//运行服务的组
address = 192.168.92.131			//服务监听地址
port = 873							//服务监听端口
fake super = yes					//服务无需使用root用户身份,即可接收文件的完整属性
use chroot = yes					//禁锢数据目录,不允许跳出
max connections = 4					//最大连接数
timeout = 900						//超时时间
ignore errors						//忽略某些错误信息
read only = false					//用户是否只读
list = false						//不允许查看模块信息
auth users = rsync_yf				//定义虚拟用户,用户数据传输
secrets file = /etc/rsync.passwd	//定义虚拟用户密码认证文件

[backup]							//定义模块名称
path = /backup						//数据存放目录
comment = backup					//模块的注释信息

//创建用户以及数据存放目录
[root@server ~]# useradd -r rsync
[root@server ~]# id rsync
uid=995(rsync) gid=992(rsync) groups=992(rsync)
[root@server ~]# mkdir /backup
[root@server ~]# chown -R rsync.rsync /backup/
[root@server ~]# ll -d /backup/
drwxr-xr-x 2 rsync rsync 6 Aug 27 14:11 /backup/

//创建虚拟用户密码文件并设置权限
[root@server ~]# echo "rsync_yf:123456" > /etc/rsync.passwd
[root@server ~]# chmod 600 /etc/rsync.passwd
[root@server ~]# ll /etc/rsync.passwd
-rw------- 1 root root 16 Aug 27 14:12 /etc/rsync.passwd
[root@server ~]# cat /etc/rsync.passwd
rsync_yf:123456

//关闭防火墙、selinux
[root@server ~]# systemctl status firewalld.service 
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:firewalld(1)

[root@server ~]# getenforce 
Disabled

//重启服务并设置服务开机自启
[root@server ~]# systemctl restart rsyncd
[root@server ~]# systemctl enable rsyncd
Created symlink /etc/systemd/system/multi-user.target.wants/rsyncd.service → /usr/lib/systemd/system/rsyncd.service.

//检查服务端口是否开启
[root@server ~]# ss -anlt
State         Recv-Q        Send-Q                Local Address:Port               Peer Address:Port       Process
LISTEN        0             5                    192.168.92.131:873                     0.0.0.0:*
LISTEN        0             128                         0.0.0.0:22                      0.0.0.0:*
LISTEN        0             128                            [::]:22                         [::]:*

5.2 客户端配置

  • 客户端也须安装rsync
  • 配置密码文件并设置权限
  • 测试收发数据
//客户端安装rsync
[root@client ~]# yum -y install rsync

//配置传输密码,该密码要和服务端的密码一致
[root@client ~]# echo '123456' > /etc/rsync.passwd
[root@client ~]# cat /etc/rsync.passwd
123456
[root@client ~]# chmod 600 /etc/rsync.passwd
[root@client ~]# ll /etc/rsync.passwd
-rw------- 1 root root 7 Aug 27 14:24 /etc/rsync.passwd

//测试收发数据
[root@client ~]# touch file1
[root@client ~]# rsync -avz /root/file1 rsync_yf@192.168.92.131::backup --password-file=/etc/rsync.passwd
sending incremental file list
file1

sent 87 bytes  received 43 bytes  260.00 bytes/sec
total size is 0  speedup is 0.00
//在服务端验证
[root@server ~]# ls /backup/
file1

//方法2:使用密码环境变量RSYNC_PASSWORD
[root@client ~]# export RSYNC_PASSWORD="123456"
//测试收发数据
[root@client ~]# touch file2
[root@client ~]# rsync -avz /root/file2 rsync_yf@192.168.92.131::backup
sending incremental file list
file2

sent 87 bytes  received 43 bytes  260.00 bytes/sec
total size is 0  speedup is 0.00

//服务端验证
[root@server ~]# ls /backup/
file1  file2

6、Rsync服务模式实战

  1. 客户端每天凌晨3点备份/etc目录至/backup下以"主机名_IP地址_当前时间命名"的目录中
  2. 客户端推送/backup目录下数据备份目录至Rsync备份服务器
  3. 客户端只保留最近七天的备份数据,避免浪费磁盘空间
  4. 服务端部署rsync服务,用于接收用户的备份数据
  5. 服务端每天校验客户端推送过来的数据是否完整,并将结果以邮件的方式发送给管理员
  6. 服务端仅保留6个月的备份数据

注意:所有服务器的备份目录均为/backup,所有脚本存放目录均为/scripts。

服务端部署rsync服务:

[root@server ~]# mkdir /scripts
[root@server ~]# vim /scripts/rsync_server.sh
#!/bin/bash
#安装rsync服务
yum -y install rsync
#配置rsync服务
cat > /etc/rsyncd.conf << EOF
uid = rsync
gid = rsync
port = 873
fake super = yes
use chroot = yes
max connections = 4
timeout = 900
ignore errors
read only = false
list = false
auth users = rsync_backup
secrets file = /etc/rsync.passwd
[backup]
path = /backup
comment = backup
EOF
#创建服务用户,创建数据备份目录并设置权限
useradd -r rsync
mkdir /backup
chown -R rsync.rsync /backup
#生成数据传输用户密码文件并设置权限
echo "rsync_backup:passwd" > /etc/rsync.passwd
chmod 600 /etc/rsync.passwd
#关闭防火墙和selinux
systemctl stop firewalld
setenforce 0
#设置服务开机自启,并重启服务
systemctl enable rsyncd
systemctl restart rsyncd

[root@server ~]# chmod +x /scripts/rsync_server.sh 

[root@server ~]# /scripts/rsync_server.sh 

客户端备份数据并推送至rsync服务器:

[root@client ~]# mkdir /scripts
[root@client ~]# vim /scripts/etc_backup.sh
#!/bin/bash
#客户端安装rsync
yum -y install rsync
#配置rsync客户端虚拟用户密码
export RSYNC_PASSWORD='passwd'
#创建备份目录
mkdir -p /backup/$(hostname)_$(ip addr show ens36 | awk -F '[ /]+' 'NR==3 {print $3}')_$(date "+%F")
#打包备份/etc目录下的数据至备份目录中
tar -zcf /backup/$(hostname)_$(ip addr show ens36 | awk -F '[ /]+' 'NR==3 {print $3}')_$(date "+%F")/etc_backup.tar.gz /etc/
#计算备份文件的校验值
md5sum /backup/$(hostname)_$(ip addr show ens36 | awk -F '[ /]+' 'NR==3 {print $3}')_$(date "+%F")/etc_backup.tar.gz > /backup/$(hostname)_$(ip addr show ens36 | awk -F '[ /]+' 'NR==3 {print $3}')_$(date "+%F")/checksum.txt
#将打包备份好的数据推送到rsync备份服务器
rsync -az /backup/$(hostname)_$(ip addr show ens36 | awk -F '[ /]+' 'NR==3 {print $3}')_$(date "+%F") rsync_backup@10.0.0.16::backup
#删除七天前的备份文件
find /backup/ -mtime +7 | xargs rm -rf

[root@client ~]# chmod +x /scripts/etc_backup.sh

[root@client ~]# crontab -e
0 3 * * * /scripts/etc_backup.sh

服务端校验数据并将结果以邮件发送给管理员:

如何具体配置使用mailx服务请移步👉Mailx服务

//配置邮件服务
[root@server ~]# yum -y install mailx
[root@server ~]# cat > /etc/mail.rc << EOF
set from=1023682065@qq.com
set smtp=smtps://smtp.qq.com:465
set smtp-auth-user=1023682065@qq.com
set smtp-auth-password=xxxxxxxxx
set smtp-auth=login
set ssl-verify=ignore
set nss-config-dir=/etc/pki/nssdb/
EOF

//配置脚本校验数据并将结果发送给管理员
[root@server ~]# vim /scripts/checksum.sh
#!/bin/bash
#校验备份数据并将结果发送给管理员
md5sum -c /backup/client_10.0.0.15_$(date "+%F")/checksum.txt | mail -s "/backup/client_10.0.0.15_$(date "+%F")" 2207118234@qq.com
#删除6个月以前的备份数据
find /backup/ -mtime +180 | xargs rm -rf

[root@server ~]# chmod +x /scripts/checksum.sh

[root@server ~]# crontab -e
0 6 * * * /scripts/checksum.sh

7、rsync+inotify数据实时同步

rsync与传统的cptar备份方式相比,rsync具有安全性高、备份迅速、支持增量备份等优点,通过rsync可以解决对实时性要求不高的数据备份需求,例如定期的备份文件服务器数据到远端服务器,对本地磁盘定期做数据镜像等。

随着应用系统规模的不断扩大,对数据的安全性和可靠性也提出的更好的要求,rsync在高端业务系统中也逐渐暴露出了很多不足,首先,rsync同步数据时,需要扫描所有文件后进行比对,进行差量传输。如果文件数量达到了百万甚至千万量级,扫描所有文件将是非常耗时的。而且正在发生变化的往往是其中很少的一部分,这是非常低效的方式。其次,rsync不能实时的去监测、同步数据,虽然它可以通过linux守护进程的方式进行触发同步,但是两次触发动作一定会有时间差,这样就导致了服务端和客户端数据可能出现不一致,无法在应用故障时完全的恢复数据。基于以上原因,rsync+inotify组合出现了!

Inotify是一种强大的、细粒度的、异步的文件系统事件监控机制,linux内核从2.6.13起,加入了Inotify支持,通过Inotify可以监控文件系统中添加、删除,修改、移动等各种细微事件,利用这个内核接口,第三方软件就可以监控文件系统下文件的各种变化情况,而 inotify-tools正是实施这样监控的软件。国人周洋在金山公司也开发了类似的实时同步软件sersync

inotify的实现有几款软件:inotify-toolssersynclrsyncd

rsync+inotify的使用方式:

  • inotify 对同步数据目录信息的监控

  • rsync 完成对数据信息的实时同步

  • 利用脚本进行结合

7.1 部署inotify软件的前提

需要2.6.13以后内核版本才能支持inotify软件。2.6.13内核之后版本,在没有安装inotify软件之前,应该有这三个文件。

[root@node2 ~]# ll /proc/sys/fs/inotify/
total 0
-rw-r--r-- 1 root root 0 Sep 23 11:55 max_queued_events
-rw-r--r-- 1 root root 0 Sep 23 11:55 max_user_instances
-rw-r--r-- 1 root root 0 Sep 23 11:55 max_user_watches
文件默认值作用说明
max_user_watches8192设置inotifywait或inotifywatch命令可以监视的文件数量(单进程)
max_user_instances128设置每个用户可以运行的inotifywait或inotifywatch命令的进程数
max_queued_events16384设置inotify实例事件(event)队列可容纳的事件数量

环境说明

服务器类型/主机名操作系统IP地址应用
源服务器/node1Centos8192.168.92.rsync
inotify-tools
脚本
目标服务器/node2Centos8192.168.92rsync

需求

  • 把源服务器上/etc目录实时同步到目标服务器的/tmp/

7.2 目标服务器配置

#关闭防火墙于SElinux
[root@node2 ~]# systemctl disable --now firewalld
[root@node2 ~]# setenforce 0
[root@node2 ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/sysconfig/selinux

#安装rsync服务端软件
[root@node2 ~]# dnf -y install rsync rsync-daemon

#配置rsyncd.conf配置文件
[root@node2 ~]# 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.passwd    # 用户认证配置文件,里面保存用户名称和密码,必须手动创建这个文件

[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 = 192.168.92.130   # 允许进行数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
hosts deny = 192.168.92.110     # 禁止数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
EOF

#创建用户认证文件
[root@node2 ~]# echo 'admin:123456' > /etc/rsync.passwd
[root@node2 ~]# cat /etc/rsync.passwd
admin:123456
#设置用户认证权限
[root@node2 ~]# chmod 600 /etc/rsync.passwd
[root@node2 ~]# ll /etc/rsync.passwd
-rw------- 1 root root 13 Sep 23 11:38 /etc/rsync.passwd

#启动rsync服务并设置开机自启动
[root@node2 ~]# systemctl enable --now rsyncd
Created symlink /etc/systemd/system/multi-user.target.wants/rsyncd.service → /usr/lib/systemd/system/rsyncd.service.
[root@node2 ~]# ss -anlt
State       Recv-Q      Send-Q             Local Address:Port             Peer Address:Port      Process
LISTEN      0           5                        0.0.0.0:873                   0.0.0.0:*
LISTEN      0           128                      0.0.0.0:22                    0.0.0.0:*
LISTEN      0           5                           [::]:873                      [::]:*
LISTEN      0           128                         [::]:22                       [::]:*

7.3 源服务器配置

#关闭防火墙于SElinux
[root@node1 ~]# systemctl disable --now firewalld
[root@node1 ~]# setenforce 0
[root@node1 ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/sysconfig/selinux

#安装rsync服务端软件,只需要安装,不要启动,不需要配置
[root@node1 ~]# dnf -y install rsync

#创建用户认证文件并设置权限
[root@node1 ~]# echo '123456' > /etc/rsync.passwd
[root@node1 ~]# chmod 600 /etc/rsync.passwd
[root@node1 ~]# cat /etc/rsync.passwd
123456
[root@node1 ~]# ll /etc/rsync.passwd
-rw------- 1 root root 7 Sep 23 11:47 /etc/rsync.passwd

#在源服务器上创建测试目录
[root@node1 ~]# mkdir -p /root/etc/test
#进行测试
[root@node1 ~]# rsync -avH --port 873 --progress --delete /root/etc/ admin@192.168.92.131::etc_from_client --password-file=/etc/rsync.passwd
sending incremental file list
deleting vmware-root_923-3988752765/
deleting ssh-S5oJT1RIhg/agent.5580
deleting ssh-S5oJT1RIhg/
./
test/

sent 77 bytes  received 106 bytes  366.00 bytes/sec
total size is 0  speedup is 0.00

#在目标服务器上查看,在/tmp目录下有test目录,说明数据同步成功
[root@node2 ~]# ls /tmp/
test

#安装inotify-tools工具
[root@node1 ~]# dnf -y install epel-release
[root@node1 ~]# dnf -y install inotify-tools

#写同步脚本
[root@node1 ~]# mkdir /scripts
[root@node1 ~]# touch /scripts/inotify.sh
[root@node1 ~]# chmod 755 /scripts/inotify.sh
[root@node1 ~]# ll /scripts/inotify.sh
-rwxr-xr-x 1 root root 0 Sep 23 12:05 /scripts/inotify.sh
[root@node1 ~]# vim /scripts/inotify.sh
host=192.168.92.131      # 目标服务器的ip(备份服务器)
src=/etc        # 在源服务器上所要监控的备份目录(此处可以自定义,但是要保证存在)
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@node1 ~]# nohup bash /scripts/inotify.sh &
[1] 14671
[root@node1 ~]# nohup: ignoring input and appending output to 'nohup.out'

[root@node1 ~]# ps -aux | grep inotify
root       14671  0.0  0.1  12724  2972 pts/0    S    12:14   0:00 bash /scripts/inotify.sh
root       14672  0.0  0.0   6636  1732 pts/0    S    12:14   0:00 /usr/bin/inotifywait -mrq --timefmt %Y%m%d %H:%M --format %T %w%f%e -e modify,delete,create,attrib /etc
root       14673  0.0  0.0  12724   260 pts/0    S    12:14   0:00 bash /scripts/inotify.sh
root       14675  0.0  0.0  12136  1124 pts/0    S+   12:14   0:00 grep --color=auto inotify

#在源服务器创建文件测试是
[root@node1 ~]# touch /etc/yefeng
[root@node1 ~]# echo 'hello,world!' > /etc/yefeng
#查看inotify生成的日志
[root@node1 ~]# tail /tmp/rsync.log
20220923 12:16 /etc/yefengCREATE was rsynced
20220923 12:16 /etc/yefengATTRIB was rsynced
20220923 12:16 /etc/yefengMODIFY was rsynced

#在目标服务器查看文件是否同步
[root@node2 ~]# cat /tmp/etc/yefeng
hello,world!

#配置脚本开机自启
[root@node2 ~]# chmod +x /etc/rc.d/rc.local
[root@node2 ~]# ll /etc/rc.d/rc.local
-rwxr-xr-x. 1 root root 474 Oct  5  2021 /etc/rc.d/rc.local
[root@node2 ~]# echo 'nohup /bin/bash /scripts/inotify.sh' >> /etc/rc.d/rc.local
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值