搭建rsync+inotify实现实时备份
2024-09-01 19:52:06
一、环境搭建说明
系统环境
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 =
timeout =
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/
hosts deny = 0.0.0.0/
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 /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 : 第八步:开机自启动
echo "/usr/bin/rsync --daemon">>/etc/rc.local
tail - /etc/rc.local
三、在数据节点生成备份节点rsync服务的密码方便使用及安装inotify服务
1、生成密码
#数据端执行 第一步:生成连接服务器需要的密码文件
echo "wt">>/etc/rsync.password
cat /etc/rsync.password 第二步:为密码文件配置权限
chmod /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: . /etc/init.d/functions
if [ $# -ne ]
then
echo "usage: $0 {start|stop|status}"
exit
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
exit
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 ]
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 >&
pkill inotifywait
else
action "inotify service stop fail" /bin/false
echo "sync server is not running"
sleep
exit
fi
sleep
if [ `ps -ef|grep inotify|grep -v grep|wc -l` -eq -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 ]
then
action "inotify service is running"
else
action "inotify service is stoped"
fi
;;
*)
echo "usage: $0 {start|stop|status}"
exit
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=
KillMode=process
PrivateTmp=true [Install]
WantedBy=multi-user.target
3、启动服务及加入开机自启动
systemctl start syncd
systemctl enable syncd
最新文章
- sqlserver 连接mysql
- SQL Server基础之《视图的概述和基本操作》
- java 命令notes
- 入门React和Webpack
- 多层iframe的页面取子标签
- 【微信Java开发 --番外篇】错误解析
- html5 实现video标签的自定义播放进度条
- 一步步学习ASP.NET MVC3 (7)——Controller,Action,ActionResult
- Docker控制组
- 让网站永久拥有HTTPS - 申请免费SSL证书并自动续期 Let’s Encrypt
- ionic3 应用内打开第三方地图导航 百度 高德
- 图解python中赋值、浅拷贝、深拷贝的区别
- react-conponent-secondesElapsed
- echarts仪表盘
- C# Clone控件
- textarea点击蓝色背景,黄色条,input点击黄色条,如何去掉?
- python 关闭垃圾回收
- js 筛选数据
- WIFI_认证加密学习_STA_AP_WDS
- 关于Flume以及Kafka理解