rsync+inotify实现自动备份
技术背景
rsync
与传统的cp
、tar
备份方式相比,rsync
具有安全性高、备份迅速、支持增量备份等优点,通过rsync
可以解决对实时性要求不高的数据备份需求,例如定期的备份文件服务器数据到远端服务器,对本地磁盘定期做数据镜像等。
随着应用系统规模的不断扩大,对数据的安全性和可靠性也提出的更好的要求,rsync
在高端业务系统中也逐渐暴露出了很多不足,首先,rsync
同步数据时,需要扫描所有文件后进行比对,进行差量传输。如果文件数量达到了百万甚至千万量级,扫描所有文件将是非常耗时的。而且正在发生变化的往往是其中很少的一部分,这是非常低效的方式。其次,rsync
不能实时的去监测、同步数据,虽然它可以通过linux
守护进程的方式进行触发同步,但是两次触发动作一定会有时间差,这样就导致了服务端和客户端数据可能出现不一致,无法在应用故障时完全的恢复数据。基于以上原因,rsync
+inotify
组合出现了!
在前面有讲到,rsync可以实现触发式的文件同步,但是通过crontab
守护进程方式进行触发,同步的数据和实际数据会有差异,而inotify
可以监控文件系统的各种变化,当文件有任何变动时,就触发rsync
同步,这样刚好解决了同步数据的实时性问题。
实验环境
备份服务器 | 本地主机 | |
---|---|---|
ip | 192.168.80.10 | 192.168.80.11 |
主机名 | backserver | client |
- 两边都需要关闭防火墙
[root@backserver ~]# setenforce 0
[root@backserver ~]# systemctl stop firewalld
[root@client ~]# setenforce 0
[root@client ~]# systemctl stop firewalld
- 备份服务器操作
[root@backserver ~]# yum install -y rsync #安装rsync
[root@backserver ~]# useradd rsync -s /sbin/nologin -M #添加一个用户
[root@backserver ~]# vim /etc/rsyncd.conf #编写配置文件
#rsync_config---start
uid = rsync #用户远端的命令使用rsync访问共享目录
gid = rsync #用户组
use chroot = no #安全相关
max connections = 200 #最大连接数
timeout = 300 #超时时间
pid file = /var/run/rsyncd.pid #进程对应的进程id
lock file = /var/run/rsync.lock #锁文件
log file = /var/log/rsyncd.log #日志文件
[backup] #模块名称
path = /backup #服务器提供访问的目录
ignore errors #忽略错误
read only = false #可写
list = false #不能列表
hosts allow = 192.168.80.0/24 #允许IP访问
hosts deny = 0.0.0.0/32 #禁止IP访问
auth users = rsync #虚拟用户
secrets file=/etc/rsync.password #虚拟账户用户和密码
[logbackup] #模块名称
path = /logbackup #服务器提供访问的目录
ignore errors #忽略错误
read only = false #可写
list = false #不能列表
hosts allow = 192.168.80.0/24 #允许IP访问
hosts deny = 0.0.0.0/32 #禁止IP访问
auth users = rsync #虚拟用户
secrets file=/etc/rsync.password #虚拟账户用户和密码
#rsync_config---end
#根据配置文件,创建密码文件
[root@backserver ~]# touch /etc/rsync.password
#写入账号密码
[root@backserver ~]# echo 'rsync:rsync123' >> /etc/rsync.password
#设置权限,一定是600
[root@backserver ~]# chmod 600 /etc/rsync.password
#创建文件夹
[root@backserver ~]# mkdir -p /backup
[root@backserver ~]# mkdir -p /logbackup
#更改属组
[root@backserver ~]# chown -R rsync.rsync /backup/
[root@backserver ~]# chown -R rsync.rsync /logbackup/
- 备份服务器编写.service文件
#先往一个文件里面创建一点空的
[root@backserver ~]# vim /etc/sysconfig/rsyncd
PTIONS=""
[root@backserver ~]# vim /usr/lib/systemd/system/rsyncd.service
[Unit]
Description=fast remote file copy program daemon
[Service]
User=root
Group=root
EnvironmentFile=/etc/sysconfig/rsyncd
ExecStart=/usr/bin/rsync --daemon --config=/etc/rsyncd.conf --no-detach
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=30s
[Install]
WantedBy=multi-user.target
#启动服务
[root@backserver ~]# systemctl start rsyncd #开启服务
[root@backserver ~]# systemctl status rsyncd #查看服务状态
● rsyncd.service - fast remote file copy program daemon
Loaded: loaded (/usr/lib/systemd/system/rsyncd.service; static; vendor preset: disabled)
Active: active (running) since Mon 2021-06-07 11:54:27 EDT; 5s ago
Main PID: 6321 (rsync)
Tasks: 1 (limit: 4743)
Memory: 280.0K
CGroup: /system.slice/rsyncd.service
└─6321 /usr/bin/rsync --daemon --config=/etc/rsyncd.conf --no-detach
[root@backserver ~]# systemctl enable rsyncd #设置为开机自启
Created symlink /etc/systemd/system/multi-user.target.wants/rsyncd.service → /usr/lib/systemd/system/rsyncd.service.
[root@backserver ~]# ss -antl
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 [::]:*
- 本地主机操作
#安装安装包
[root@client ~]# yum install -y rsync
#创建密码文件,写入密码(不需要写入密码)
[root@client ~]# touch /etc/rsync.password
[root@client ~]# echo rsync123 >>/etc/rsync.password
#为密码文件添加权限(必须是600)
[root@client ~]# chmod 600 /etc/rsync.password
#往文件里面写点东西(用于测试)
[root@client ~]# cd /backup/
[root@client backup]# ls
[root@client backup]# mkdir {1..10}
#传输文件:方法一:
语法: rsync 选项 备份服务器目录(唯一:按照配置文件里来)用户名@ip::本地主机目录(随意) 指定密码文件存放路径
[root@client backup]# rsync -avz /backup/ rsync@192.168.80.10::backup/ --password-file=/etc/rsync.password
#去备份服务器验证
[root@backserver ~]# ls /backup/
1 10 2 3 4 5 6 7 8 9
#传输文件:方法二:
[root@client backup]# rsync -avz /etc rsync://rsync@192.168.80.10/logbackup/hostname --password-file=/etc/rsync.password
#去备份服务器验证
[root@backserver ~]# ls /logbackup/hostname/etc/
adjtime dracut.conf.d ld.so.conf pki skel
aliases environment ld.so.conf.d plymouth ssh
alternatives ethertypes libaudit.conf pm ssl
anacrontab exports libnl polkit-1 sssd
audit filesystems libreport popt.d subgid
authselect firewalld libssh prelink.conf.d subuid
bash_completion.d fonts libuser.conf printcap sudo.conf
bashrc fstab locale.conf profile sudoers
bindresvport.blacklist fuse.conf localtime profile.d sudoers.d
binfmt.d gcrypt login.defs protocols sudo-ldap.conf
centos-release gnupg logrotate.conf rc0.d sysconfig
chkconfig.d GREP_COLORS logrotate.d rc1.d sysctl.conf
chrony.conf groff lvm rc2.d sysctl.d
chrony.keys group machine-id....
- 搭建inotify
#添加epel源,安装inotify-tools
[root@client ~]# vim /etc/yum.repos.d/epel.repo
[epel]
name=EPEL
baseurl=https://mirrors.aliyun.com/epel/$releasever/Everything/$basearch
enabled=1
gpgcheck=0
#安装inotify-tools工具,实时触发rsync进行同步
[root@client ~]# yum install -y inotify-tools
#在源服务器编写同步脚本(可实现自动去备份目录内容)
[root@client ~]# mkdir /scripts
[root@client ~]# cd /scripts/
[root@client scripts]# ls
[root@client scripts]# vim inotify.sh
host=192.168.80.10 # 目标服务器的ip(备份服务器)
src=/backup # 在源服务器上所要监控的备份目录(此处可以自定义,但是要保证存在)
des=backup # 自定义的模块名,需要与目标服务器上定义的同步名称一致
password=/etc/rsync.password # 执行数据同步的密码文件
user=rsync # 执行数据同步的用户名
inotifywait=/usr/bin/inotifywait #inotify-tools的位置
$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@client scrip]# chmod +x inotify.sh
#下载nohup命令
[root@client scripts]# yum install -y coreutils
#启动脚本,并放在后台
[root@client ~]# nohup bash /scripts/inotify.sh &
[1] 4131
[root@client ~]# nohup: ignoring input and appending output to 'nohup.out' #在这里按回车
[root@client ~]# ps -aux | grep inotify.sh
root 4131 0.0 0.3 12696 3032 pts/0 S 13:26 0:00 bash /scripts/inotify.sh
root 4133 0.0 0.0 12696 256 pts/0 S 13:26 0:00 bash /scripts/inotify.sh
root 4135 0.0 0.1 12108 1060 pts/0 R+ 13:27 0:00 grep --color=auto inotify.sh
#创建文件,手动触发脚本进行备份
[root@client ~]# cd /backup/
[root@client backup]# ls
1 10 2 3 4 5 6 7 8 9 httpd nohup.out pwd
[root@client backup]# touch 99999
#去备份服务器查看
[root@backserver ~]# ls /backup/backup/
1 10 2 3 4 5 6 7 8 9 99999 httpd nohup.out pwd #自动创建了999
- 设置脚本开机自动启动
[root@backserver ~]# chmod +x /etc/rc.d/rc.local #rc.local开机后执行的最后一个启动
[root@backserver ~]# echo 'nohup /bin/bash /scripts/inotify.sh' >> /etc/rc.d/rc.local
[root@backserver ~]# tail /etc/rc.d/rc.local
#
# 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