搭建rsync+inotify实现实时备份
一、环境搭建说明
系统环境
CentOS7.5
备份节点
主机名:backup01
IP地址:172.16.2.41
数据节点
主机名:nfs-master
IP地址:172.16.2.31
二、在备份节点搭建rsync服务
Rsync服务端(即备份数据远程存储节点) 第一步:查看rsync安装包 rpm -qa rsync 第二步:添加rsync服务的用户,管理本地目录 useradd -s /sbin/nologin -M rsync id rsync 第三步:配置rsync的进程模式(vim /etc/rsyncd.conf) uid = rsync gid = rsync use chroot = no max connections = 200 timeout = 300 pid file = /var/run/rsyncd.pid lock file = /var/run/rsync.lock log file = /var/log/rsyncd.log [backup] path = /backup ignore errors read only = false list = false fake super = yes hosts allow = 172.16.2.0/24 hosts deny = 0.0.0.0/32 auth users = rsync_backup secrets file = /etc/rsync.password 第四步:根据rsync.conf的auth_users配置帐户,远程连接的,并根据secreets file参数生成密码文件 echo "rsync_backup:wt">>/etc/rsync.password cat /etc/rsync.password 第五步:更改密码配置文件的权限 chmod 600 /etc/rsync.password ls -l /etc/rsync.password 第六步:创建共享的目录授权rsync服务管理 mkdir -p /backup chown -R rsync.rsync /backup #提示:如果没有/backup目录,就会chdir failed 第七步:启动rsync服务并检查 rsync --daemon ps -ef|grep rsync|grep -v grep lsof -i :873 第八步:开机自启动 echo "/usr/bin/rsync --daemon">>/etc/rc.local tail -1 /etc/rc.local
三、在数据节点生成备份节点rsync服务的密码方便使用及安装inotify服务
1、生成密码
#数据端执行 第一步:生成连接服务器需要的密码文件 echo "wt">>/etc/rsync.password cat /etc/rsync.password 第二步:为密码文件配置权限 chmod 600 /etc/rsync.password ls -l /etc/rsync.password
2、安装inotify服务
yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm yum install inotify-tools -y rpm -qa inotify-tools
四、监控目录改变时执行rsync命令实时备份数据(此过程需写脚本,脚本如下)
脚本存放位置为:/server/scripts/inotify.sh,记得给脚本加上执行权限,chmod +x /server/scripts/inotify.sh
#!/bin/bash inotifywait -mrq --timefmt '%Y/%m/%d %H:%M' --format '%T %e %w%f' -e create,attrib,modify,moved_to,close_write,delete,move /backup/ |while read file do cd /backup rsync -az ./ --delete rsync_backup@172.16.2.41::backup --password-file=/etc/rsync.password done
五、把监控脚本变成自定义服务并加入开机自启动
1、定义监控目录的脚本服务(即服务的start、stop、restart、status实现)
脚本存放位置为:/server/scripts/sync.sh,记得给脚本加上执行权限,chmod +x /server/scripts/sync.sh
#!/bin/bash #chkconfig: 2345 38 46 . /etc/init.d/functions if [ $# -ne 1 ] then echo "usage: $0 {start|stop|status}" exit 1 fi case "$1" in start) if [ -e "/var/run/inotify.pid" ] then action "inotify service start fail" /bin/false echo "sync server is running......" sleep 1 exit 1 fi /bin/bash /server/scripts/inotify.sh & `ps -ef|grep "inotifywait"|grep -v "grep"|awk '{print $2}'` >/var/run/inotify.pid if [ `ps -ef|grep inotify|wc -l` -gt 2 ] then action "inotify service is started" /bin/true else action "inotify service is started" /bin/false fi ;; stop) if [ `ps -ef|grep inotify|grep -v grep|wc -l` -a -e "/var/run/inotify.pid" ] then rm -f /var/run/inotify.pid >/dev/null 2>&1 pkill inotifywait else action "inotify service stop fail" /bin/false echo "sync server is not running" sleep 1 exit 1 fi sleep 1 if [ `ps -ef|grep inotify|grep -v grep|wc -l` -eq 0 -a ! -e "/var/run/inotify.pid" ] then action "inotify service is stoped" /bin/true else action "inotify service is stoped" /bin/false fi ;; status) if [ `ps -ef|grep inotify|wc -l` -gt 2 ] then action "inotify service is running" else action "inotify service is stoped" fi ;; *) echo "usage: $0 {start|stop|status}" exit 1 esac
2、加入开机自启动
此脚本必须放在/usr/lib/systemd/system/下,例如/usr/lib/systemd/system/syncd.service,sysncd.service注册服务的配置如下
[Unit] Description="这是inotify实时同步服务" After=network.target remote-fs.target nss-lookup.target [Service] Type=forking ExecStart=/bin/sh /server/scripts/sync.sh start ExecReload=/bin/sh /server/scripts/sync.sh restart ExecStop=/bin/sh /server/scripts/sync.sh stop KillSignal=SIGQUIT TimeoutStopSec=5 KillMode=process PrivateTmp=true [Install] WantedBy=multi-user.target
3、启动服务及加入开机自启动
systemctl start syncd
systemctl enable syncd