搭建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 =
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
搭建rsync+inotify实现实时备份的更多相关文章
- rsync + inotify 数据实时同步
一.rsync介绍 rsync英文全称为Remote synchronization,从软件的名称就可以看出来,Rsync具有可是本地和远程两台主机之间的数据快速复制同步镜像.远程备份的功能,这个功能 ...
- linux rsync +inotify 实现 实时同步
前言: rsync可以实现触发式的文件同步,但是通过crontab守护进程方式进行触发,同步的数据和实际数据会有差异,而inotify可以监控文件系统的各种变化,当文件有任何变动时,就触发rs ...
- CentOS7 Rsync服务搭建-Rsync+Inotify架构实现实时同步
一.rsync 概念 1.rsyncrsync是类unix/linux系统下的数据镜像备份工具.使用快速增量备份工具Remote Sync可以远程同步,支持本地复制,或者与其他SSH.rsync主机同 ...
- centos 配置rsync+inotify数据实时同步2
一.Rsync服务简介 1. 什么是Rsync 它是一个远程数据同步工具,它在同步文件的同时,可通过LAN/WAN快速同步多台主机间的文件.Rsync使用所谓的“rsync算法”来使本地和远程两个主机 ...
- rsync简介与rsync+inotify配置实时同步数据
rsync简介 rsync是linux系统下的数据镜像备份工具.使用快速增量备份工具Remote Sync可以远程同步,支持本地复制,或者与其他SSH.rsync主机同步. rsync特性 rsync ...
- centos7配置rsync+inotify数据实时共享
关于centos7版本上面搭建rsync服务并且实现实时同步之前一直是在6版本上面搭建rsync服务,在7版本上面折腾了半天.此处总结下inotify下载地址:http://github.com/do ...
- centos 配置rsync+inotify数据实时同步
何为rsync? 定义: rsync是一个开源的快速备份工具,可以在不同主机之间镜像同步整个目录树,支持增量备份,保持链接和权限,非常适用于异地备份 何为源端和发起端? 在远程同步过程中,负责发起rs ...
- rsync+inotify实现实时同步案例--转
转自:http://chocolee.blog.51cto.com/8158455/1400596 随着应用系统规模的不断扩大,对数据的安全性和可靠性也提出的更好的要求,rsync在高端业务系统中也逐 ...
- Rsync+inotify实现实时同步
1.1 inotify介绍 inotify是一种强大的.细粒度的.异步的文件系统事件控制机制.linux内核从2.6.13起,加入了inotify支持,通过inotify可以监控文件系统中添加.删除. ...
随机推荐
- linux内核的preempt抢占调度,preempt_count抢占保护“锁”
抢断调度,是调度机制对实时系统需要的支持,是一种快速响应的重调度机制.既然与重调度有关,那么就先回顾一下调度和重调度. 调度分两种情况,1. 一种是自愿调度,由代码主动调用schedule来让度cpu ...
- react-router-dom路由
- 结合开源软件kaptcha讲解登录验证码功能的实现
一.验证码生成之配置使用kaptcha 使用google开源的验证码实现类库kaptcha,通过maven坐标引入 <dependency> <groupId>com.gith ...
- Dart Learn Notes 04
流程控制语句 流程控制语句的作用就是控制代码的执行流程. if and else var a = 10; if(a > 10){ print('ok'); }else if( 5 < a ...
- Python使用场景和应用领域
Python特点 1.Python使用C语言开发,但是Python不再有C语言中的指针等复杂的数据类型. 2.Python具有很强的面向对象特性,而且简化了面向对象的实现.它消除了保护类型.抽象类.接 ...
- day 26 约束、自定义异常、加密hashlib、logging
一.约束 建议使用: class BaseMessage(object): def send(self): """ 必须继承BaseMessage,然后其中必须编写sen ...
- ctf线下赛中关闭非法用户shell脚本
linux中三类用户:根用户,虚拟用户,普通用户. 其中普通用户的UID一般介于500-6000之间. #!/bin/bash for uid in $( cat /etc/passwd | cut ...
- Linux服务和systemctl详解
定义 A Linux service is an application (or set of applications) that runs in the background waiting to ...
- JavaScript如何创建一个对象
我们可以利用JavaScript的语法特征,以类的思想来创建对象. 方法一:原始方法代码如下: <script> var obj = new Object(); obj.name = &q ...
- MySQL分层和查询数据的流程
MySQL分层 MySQL分层 主要分为:连接层,服务层,引擎层,存储层 客户端执行一条select命令的流程如下 连接器 功能: 负责跟客户端建立连接.获取权限.维持和管理连接 细节: 1.当用户登 ...