IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 系统运维 -> CentoS7 安装篇十:rsync+ inotify 实现附件备份至其他服务器 -> 正文阅读

[系统运维]CentoS7 安装篇十:rsync+ inotify 实现附件备份至其他服务器

一、什么是rsync

rsync (remote sync)是一款非常好的数据同步工具,能够通过对比同步双方的数据变动,实现增量同步,还可以通过LAN/WAN实现远程多台主机间文件的同步,还能结合crond任务计划来执行自动备份,又可以结合ssh实现远程数据备份的安全,种种特性使他看起来相当优秀。但如果需备份数据十分庞大时,它的不足之处就显现出来了,比如每次执行同步操作时,rsync都会扫描全部数据进而计算出增量部分,而后再同步增量数据,这将会十分耗时,使其变得低效;并且受限于crond计划任务最小时间间隔为一分钟,会导致同步源端和目的段数据不一致,这在高可用环境中是不被允许的。这个时候我们就可以构建rsync+Inotify架构来解决此应用瓶颈。

二、inotify简介

inotify是内核的一个功能,众所周知内核的功能我们必须要配合工具才能使用,通常情况下用户要使用内核的功能,都需要用户空间的软件去调用才可以达到使用内核的功能的目的,用户是无法直接操内核的。实现inotify软件有inotify-tools、sersync、lrsyncd。我们这里以inotify-tools这个软件包为例进行实验;inotify-tools包主要有两个文件,一个是inotifywait: 在被监控的文件或目录上等待特定文件系统事件(open close delete等)发生,常用于实时同步的目录监控;一个是inotifywatch:收集被监控的文件系统使用的统计数据,指文件系统事件发生的次数统计。通常情况下我们使用iontifywait就可以了。接下来我们来安装inotify-tools

三、环境两台服务器

源服务器:192.168.15.2

目标服务器:192.168.15.4

四、需求

把源服务器上附件实时同步备份至目标服务器

五、目标服务器环境配置

1.安装rsync软件

第一种安装方式:

yum -y install rsync

?第二种安装方式:

cd /home/software
wget https://download.samba.org/pub/rsync/src/rsync-3.1.2.tar.gz
tar -zxvf rsync-3.1.2.tar.gz
cd rsync-3.1.2
./configure --prefix=/usr/local/rsync
make
make install

2.rsync配置rsyncd.conf文件

vim /etc/rsyncd.conf  //增加以下内容
log file = /var/log/rsyncd.log //日志文件位置,启动rsync后自动产生,无需提前创建
pidfile = /var/run/rsyncd.pid //pid文件存放位置
lock file = /var/run/rsync.lock //支持max connections参数的锁文件
secrets file = /etc/rsync.password//用户认证配置文件,里面存放用户名称和密码,必须手动创建这个文件
 
 [backup]  //自定义同步名称
   path = /data/backup/ //rsync服务端存放路径,客户端的数据将同步到此目录
  comment = sync etc from client
  uid = root  //设置rsync运行权限为root
  gid = root   //设置rsync运行权限为root
  port = 873    //默认端口为873
  ignore errors  //表示出现错误忽视错误
  use chroot = no  //默认为true ,修改为no,增加对目录软链接的备份
  read only = no //设置rsync服务端为读写权限
  list = no  //不显示rsync服务端资源列表
  max connections = 200 //最大连接数
  timeout = 600  //设置超时时间
  auth users = admin  //执行数据同步的用户名,可以设置多个,用英文逗号隔开
  hosts allow = 192.168.15.2 //允许进行数据同步的IP地址,可以设置多个,用英文逗号隔开
  hosts deny = 192.168.24.188  禁止进行数据同步的IP地址,可以设置多个,用英文逗号隔开

3.创建备份目录

mkdir /data/backup

4.创建同步使用账号密码

 echo 'root:518' > etc/rsync.password
 cat etc/rsync.password
 root:518

5.设置文件权限

chmod 600 /etc/rsync*
ll /etc/rsync*
-rw-------. 1 root root 880 Aug 13 14:54 /etc/rsyncd.conf
-rw-------. 1 root root  10 Aug 13 14:55 /etc/rsync.password

6.启动rsync服务并设置开机自启动

systemctl start rsyncd
systemctl enable rsyncd
Created symlink from /etc/systemd/system/multi-user.target.wants/rsyncd.service to /usr/lib/systemd/system/rsyncd.service.

以上操作完毕之后,目标服务器已经配置完毕

六、源服务器环境配置

1.关闭防火墙与SELINUX

systemctl stop firewalld
systemctl disable firewalld
sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/sysconfig/selinux
setenforce 0

2.安装rsync服务端软件,只需要安装,不要启动,不需要配置(同目标服务器安装方式)

3.创建账户

echo '518' > /etc/rsync.pass
cat /etc/rsync.pass
518

4.设置文件权限

chmod 600 /etc/rsync.pass
ll /etc/rsync.pass
-rw-------. 1 root root 0 Aug 13 15:46 /etc/rsync.pass

5.测试

mkdir  data/attachment
rsync -avH --port 873 --progress --delete /root/etc/ root@192.168.15.4::backup --password-file=/etc/rsync.pass
sending incremental file list
./
test/
 
sent 69 bytes  received 22 bytes  182.00 bytes/sec
total size is 0  speedup is 0.00

出现以上情况表示两台服务器人rsync环境已经搭建成功

七、源服务器安装inotify-tools工具,实时触发rsync同步

? 1.检查服务器内核是否支持inotify

//检查服务器内核是否支持inotify
ll /proc/sys/fs/inotify/
total 0
-rw-r--r--. 1 root root 0 Aug 13 16:13 max_queued_events
-rw-r--r--. 1 root root 0 Aug 13 16:13 max_user_instances
-rw-r--r--. 1 root root 0 Aug 13 16:13 max_user_watches
//如果有这三个max开头的文件则表示服务器内核支持inotify
 
//安装inotify-tools
yum -y install make gcc gcc-c++ inotify-tools 

2.写实时同步脚本

#!/bin/bash
host=192.168.15.4  //目标服务器的ip(备份服务器)
src=/data/attachment //在源服务器上所要监控的备份目标
des=backup   //自定义的模块名,需要与目标服务器上的定义名称同步
password=/etc/rsync.password  //执行数据同步的密码文件
user=root   //执行数据同步的名
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" >>/data/log/rsync/rsync.log 2>&1
 done

3.检查脚本是否存在问题

bash -x /sh/rsync.sh

4.启动脚本

nohup bash /jysoft/sh/rsync.sh &
ps -ef|grep inotify

5.测试,再源服务器上新建目录之后,查看目标服务器是否存在,如果存在表示成功

6.设置开机自启

chmod +x /etc/rc.d/rc.local
ll /etc/rc.d/rc.local
-rwxr-xr-x. 1 root root 473 Apr 11 15:36 /etc/rc.d/rc.local
echo 'nohup /bin/bash /jysoft/sh/rsync.sh' >> /etc/rc.d/rc.local
 tail  /etc/rc.d/rc.local
# to run scripts during boot instead of using this file.
#
# 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 /jysoft/sh/rsync.sh

  系统运维 最新文章
配置小型公司网络WLAN基本业务(AC通过三层
如何在交付运维过程中建立风险底线意识,提
快速传输大文件,怎么通过网络传大文件给对
从游戏服务端角度分析移动同步(状态同步)
MySQL使用MyCat实现分库分表
如何用DWDM射频光纤技术实现200公里外的站点
国内顺畅下载k8s.gcr.io的镜像
自动化测试appium
ctfshow ssrf
Linux操作系统学习之实用指令(Centos7/8均
上一篇文章      下一篇文章      查看所有文章
加:2022-06-01 15:29:48  更:2022-06-01 15:30:42 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/18 19:47:08-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码