一、环境搭建说明

系统环境

  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

最新文章

  1. sqlserver 连接mysql
  2. SQL Server基础之《视图的概述和基本操作》
  3. java 命令notes
  4. 入门React和Webpack
  5. 多层iframe的页面取子标签
  6. 【微信Java开发 --番外篇】错误解析
  7. html5 实现video标签的自定义播放进度条
  8. 一步步学习ASP.NET MVC3 (7)——Controller,Action,ActionResult
  9. Docker控制组
  10. 让网站永久拥有HTTPS - 申请免费SSL证书并自动续期 Let’s Encrypt
  11. ionic3 应用内打开第三方地图导航 百度 高德
  12. 图解python中赋值、浅拷贝、深拷贝的区别
  13. react-conponent-secondesElapsed
  14. echarts仪表盘
  15. C# Clone控件
  16. textarea点击蓝色背景,黄色条,input点击黄色条,如何去掉?
  17. python 关闭垃圾回收
  18. js 筛选数据
  19. WIFI_认证加密学习_STA_AP_WDS
  20. 关于Flume以及Kafka理解

热门文章

  1. Pashmak and Parmida's problem(树状数组)
  2. 破解网站二维码验证,Java实现,不调用任何平台api接口
  3. 十、CSR8670的DFU功能[补充]
  4. 在linux系统下进行pip升级注意事项
  5. centos7 设置连接无线wifi
  6. 结合RBAC模型讲解权限管理系统需求及表结构创建
  7. 程序员需要掌握的七种 Python 代码更易维护的武器
  8. 设置属性节点(setAttribute())
  9. flanneld 安装
  10. Docker虚拟化之<基础命令>