阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

inotify配合rsync将文件实时同步到备份服务器

521次阅读
没有评论

共计 8747 个字符,预计需要花费 22 分钟才能阅读完成。

一、环境准备

1、服务器详细信息如下,有两台主机,A 和 B

主机 主机名 ip
A linux-node1 192.168.17.23
B linux-node2 192.168.17.24

2、为详细的拓扑图架构
逻辑架构图如下:inotify 配合 rsync 将文件实时同步到备份服务器

物理架构图
3、将两台主机上的 hosts 解析设置为一样
主机 A

[root@linux-node1 ~]# tail -2 /etc/hosts
192.168.17.23  linux-node1.com
192.168.17.24  linux-node2.com

主机 B

[root@linux-node2 ~]# tail -2 /etc/hosts
192.168.17.23  linux-node1.com
192.168.17.24  linux-node2.com

二、部署 rsync 服务器端

1、配置 rsyncd.conf

# 情动发现报错,原来是需要配置 rsyncd.conf
[root@linux-node1 ~]# rsync --daemon
Failed to parse config file: /etc/rsyncd.conf
# 配置 rsyncd.conf
[root@linux-node1 ~]# cat /etc/rsyncd.conf    
###this is rsyncd.conf 
###by amsilence
uid = rsync
gid = rsync
use chroot = no
max connections =2000
timeout = 600
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
ignore errors
read only = false
list = false
hosts allow = 192.168.17.0/24
hosts deny = 0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync.password
#################################
[backup]
path = /backup

2、启动 rsync 服务
错误 1:

[root@linux-node1 ~]# rsync --deamon
rsync: --deamon: unknown option
rsync error: syntax or usage error (code 1) at main.c(1422) [client=3.0.6]

解决办法:忘记创建 /etc/rsync.password 文件

[root@linux-node1 ~]# echo“rsync_backup:123456”>/etc/rsync.password 

启动 rsync 成功

[root@linux-node1 ~]# cat /etc/rsync.password 
rsync_backup:123456
[root@linux-node1 ~]# chmod 600 /etc/rsync.password 
[root@linux-node1 ~]# rsync --daemon
[root@linux-node1 ~]# pgrep rsync
1267
[root@linux-node1 ~]# netstat -lntup|grep rsync
tcp        0      0 0.0.0.0:873                 0.0.0.0:*                   LISTEN      1267/rsync
tcp        0      0 :::873                      :::*                        LISTEN      1267/rsync    

3、创建 rsync 需要使用对的真实用户

[root@linux-node1 ~]# useradd -s /sbin/nologin -M rsync
[root@linux-node1 ~]# id rsync
uid=501(rsync) gid=501(rsync) groups=501(rsync)

4、创建 rsync 推送需要的目录 (需要按照)

[root@linux-node1 ~]# mkdir /backup
[root@linux-node1 ~]# chown -R rsync.rsync /backup/

5、使用真实用户来测试 rsync 服务
注意:如果 ssh 端口修改了,就需要用这种加上端口,rsync - 参数 文件 -e‘ssh -p 端口号’用户名 @ip: 要推送到那里

[root@linux-node1 ~]# rsync -avz install.log '-e ssh -p 4086' silence@linux-node1.com:/tmp
The authenticity of host '[linux-node1.com]:4086 ([192.168.17.23]:4086)' can't be established.
RSA key fingerprint is a3:55:28:26:ec:e6:9c:95:bc:06:3c:fd:fc:1b:b3:23.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[linux-node1.com]:4086,[192.168.17.23]:4086' (RSA) to the list of known hosts.
silence@linux-node1.com's password: 
sending incremental file list
install.log

sent 5398 bytes  received 31 bytes  638.71 bytes/sec
total size is 21764  speedup is 4.01
[root@linux-node1 ~]# ll /tmp/
total 24
-rw-r--r-- 1 silence silence 21764 Jan  4 08:41 install.log

测试 rsync 使用成功

三、部署 rsync 客户端

1、测试 rsync 虚拟用户
注意:在使用虚拟用户的时候不需用 ssh 的端口

[root@linux-node2 ~]# rsync -avz install.log rsync_backup@linux-node1.com::backup                 
Password: 
sending incremental file list
install.log

sent 5394 bytes  received 27 bytes  2168.40 bytes/sec
total size is 21764  speedup is 4.01

2、创建自动应答密码文件

[root@linux-node2 ~]# cat /etc/rsync.password 
123456
[root@linux-node2 ~]# chmod 600 /etc/rsync.password
# 下面内容为测试 
[root@linux-node2 ~]# rsync -avz install.log rsync_backup@linux-node1.com::backup --password-file=/etc/rsync.password 
sending incremental file list

sent 32 bytes  received 8 bytes  26.67 bytes/sec
total size is 21764  speedup is 544.10

更多详情见请继续阅读下一页的精彩内容 :http://www.linuxidc.com/Linux/2017-10/147668p2.htm

实验环境:还是上一篇文章中的实验环境
PS:上一篇文章我写了如何使用 rsync 同步文件到备份服务器,但是有一个缺点就是计算机不能够检测到电脑上面哪些数据发生了变化,因此需要使用 inotify 来监测哪些文件发生了变化。让后将变化了的文件通过 rsync 时时同步到备份服务器。

一、创建下载软件的文件夹

[root@linux-node2 ~]# [-d /home/silence/tools] ||mkdir -p /home/silence/tools && cd /home/silence/tools  
[root@linux-node2 tools]# ll /home/silence/tools/
total 0
[root@linux-node2 tools]# pwd                    
/home/silence/tools

二、下载编译安装 inotify

[root@linux-node2 tools]# wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
[root@linux-node2 tools]# ls -lh inotify-tools-3.14.tar.gz 
-rw-r--r-- 1 root root 351K Mar 14  2010 inotify-tools-3.14.tar.gz
[root@linux-node2 tools]# tar xf inotify-tools-3.14.tar.gz 
[root@linux-node2 tools]# cd inotify-tools-3.14
[root@linux-node2 inotify-tools-3.14]# mkdir /application
[root@linux-node2 inotify-tools-3.14]# ./configure --prefix=/application/inotify-tools-3.14 && make && make install
[root@linux-node2 inotify-tools-3.14]# ln -s /application/inotify-tools-3.14/ /application/inotify  

三、测试 inotify

开启两个终端,终端一用于使用 inotify,终端二用于创建和删除文件
终端一上

[root@linux-node2 tools]# /application/inotify/bin/inotifywait -mrq --timefmt '%y%m%d%H:%M' --format '%T %w%f' -e create,close_write,delete /tmp/

终端二上

[silence@linux-node2 ~]$ for name in `seq 5`;do touch /tmp/test${name}.txt;sleep 1;done;

终端一上面的检测

[root@linux-node2 tools]# /application/inotify/bin/inotifywait -mrq --timefmt '%y%m%d%H:%M' --format '%T %w%f' -e create,close_write,delete /tmp/
17010620:24 /tmp/test1.txt
17010620:24 /tmp/test1.txt
17010620:24 /tmp/test2.txt
17010620:24 /tmp/test2.txt
17010620:24 /tmp/test3.txt
17010620:24 /tmp/test3.txt
17010620:24 /tmp/test4.txt
17010620:24 /tmp/test4.txt
17010620:24 /tmp/test5.txt
17010620:24 /tmp/test5.txt

四、利用脚本配合 rsync 同步变化了的文件

脚本内容如下

[root@linux-node2 scripts]# cat rsync-backup.sh 
#!/bin/sh
# rsync-backup.sh
inotify=/application/inotify/bin/inotifywait
$inotify -mrq --format '%w%f' -e create,close_write,delete /data/ |while read file
do
   rsync -az /data/ --delete rsync_backup@linux-node1.com::backup    --password-file=/etc/rsync.password
done

测试脚本

 终端二
[silence@linux-node2 ~]$ sudo touch /data/{a..d}.txt 
终端一
[root@linux-node2 scripts]# sh -x /server/scripts/rsync-backup.sh 
+ inotify=/application/inotify/bin/inotifywait
+ /application/inotify/bin/inotifywait -mrq --format %w%f -e create,close_write,delete /data/
+ read file
+ rsync -az /data/ --delete rsync_backup@linux-node1.com::backup --password-file=/etc/rsync.password
……………………………………中间内容省略………………………………………………
+ rsync -az /data/ --delete rsync_backup@linux-node1.com::backup --password-file=/etc/rsync.password
+ read file

查看是否同步成功

[root@linux-node1 ~]# ll /backup/
total 0
-rw-r--r-- 1 rsync rsync 0 Jan  6 20:35 a.txt
-rw-r--r-- 1 rsync rsync 0 Jan  6 20:35 b.txt
-rw-r--r-- 1 rsync rsync 0 Jan  6 20:35 c.txt
-rw-r--r-- 1 rsync rsync 0 Jan  6 20:35 d.txt

在 linux-node2.com 主机

[silence@linux-node2 ~]$ sudo rm -f /data/* 

在 backup 上面查看

[root@linux-node1 ~]# ll /backup/     
total 0

挂载在后台运行

[root@linux-node2 scripts]# /bin/bash /server/scripts/rsync-backup.sh &
[1] 27048

本文永久更新链接地址 :http://www.linuxidc.com/Linux/2017-10/147668.htm

一、环境准备

1、服务器详细信息如下,有两台主机,A 和 B

主机 主机名 ip
A linux-node1 192.168.17.23
B linux-node2 192.168.17.24

2、为详细的拓扑图架构
逻辑架构图如下:inotify 配合 rsync 将文件实时同步到备份服务器

物理架构图
3、将两台主机上的 hosts 解析设置为一样
主机 A

[root@linux-node1 ~]# tail -2 /etc/hosts
192.168.17.23  linux-node1.com
192.168.17.24  linux-node2.com

主机 B

[root@linux-node2 ~]# tail -2 /etc/hosts
192.168.17.23  linux-node1.com
192.168.17.24  linux-node2.com

二、部署 rsync 服务器端

1、配置 rsyncd.conf

# 情动发现报错,原来是需要配置 rsyncd.conf
[root@linux-node1 ~]# rsync --daemon
Failed to parse config file: /etc/rsyncd.conf
# 配置 rsyncd.conf
[root@linux-node1 ~]# cat /etc/rsyncd.conf    
###this is rsyncd.conf 
###by amsilence
uid = rsync
gid = rsync
use chroot = no
max connections =2000
timeout = 600
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
ignore errors
read only = false
list = false
hosts allow = 192.168.17.0/24
hosts deny = 0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync.password
#################################
[backup]
path = /backup

2、启动 rsync 服务
错误 1:

[root@linux-node1 ~]# rsync --deamon
rsync: --deamon: unknown option
rsync error: syntax or usage error (code 1) at main.c(1422) [client=3.0.6]

解决办法:忘记创建 /etc/rsync.password 文件

[root@linux-node1 ~]# echo“rsync_backup:123456”>/etc/rsync.password 

启动 rsync 成功

[root@linux-node1 ~]# cat /etc/rsync.password 
rsync_backup:123456
[root@linux-node1 ~]# chmod 600 /etc/rsync.password 
[root@linux-node1 ~]# rsync --daemon
[root@linux-node1 ~]# pgrep rsync
1267
[root@linux-node1 ~]# netstat -lntup|grep rsync
tcp        0      0 0.0.0.0:873                 0.0.0.0:*                   LISTEN      1267/rsync
tcp        0      0 :::873                      :::*                        LISTEN      1267/rsync    

3、创建 rsync 需要使用对的真实用户

[root@linux-node1 ~]# useradd -s /sbin/nologin -M rsync
[root@linux-node1 ~]# id rsync
uid=501(rsync) gid=501(rsync) groups=501(rsync)

4、创建 rsync 推送需要的目录 (需要按照)

[root@linux-node1 ~]# mkdir /backup
[root@linux-node1 ~]# chown -R rsync.rsync /backup/

5、使用真实用户来测试 rsync 服务
注意:如果 ssh 端口修改了,就需要用这种加上端口,rsync - 参数 文件 -e‘ssh -p 端口号’用户名 @ip: 要推送到那里

[root@linux-node1 ~]# rsync -avz install.log '-e ssh -p 4086' silence@linux-node1.com:/tmp
The authenticity of host '[linux-node1.com]:4086 ([192.168.17.23]:4086)' can't be established.
RSA key fingerprint is a3:55:28:26:ec:e6:9c:95:bc:06:3c:fd:fc:1b:b3:23.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[linux-node1.com]:4086,[192.168.17.23]:4086' (RSA) to the list of known hosts.
silence@linux-node1.com's password: 
sending incremental file list
install.log

sent 5398 bytes  received 31 bytes  638.71 bytes/sec
total size is 21764  speedup is 4.01
[root@linux-node1 ~]# ll /tmp/
total 24
-rw-r--r-- 1 silence silence 21764 Jan  4 08:41 install.log

测试 rsync 使用成功

三、部署 rsync 客户端

1、测试 rsync 虚拟用户
注意:在使用虚拟用户的时候不需用 ssh 的端口

[root@linux-node2 ~]# rsync -avz install.log rsync_backup@linux-node1.com::backup                 
Password: 
sending incremental file list
install.log

sent 5394 bytes  received 27 bytes  2168.40 bytes/sec
total size is 21764  speedup is 4.01

2、创建自动应答密码文件

[root@linux-node2 ~]# cat /etc/rsync.password 
123456
[root@linux-node2 ~]# chmod 600 /etc/rsync.password
# 下面内容为测试 
[root@linux-node2 ~]# rsync -avz install.log rsync_backup@linux-node1.com::backup --password-file=/etc/rsync.password 
sending incremental file list

sent 32 bytes  received 8 bytes  26.67 bytes/sec
total size is 21764  speedup is 544.10

更多详情见请继续阅读下一页的精彩内容 :http://www.linuxidc.com/Linux/2017-10/147668p2.htm

正文完
星哥玩云-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计8747字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中

星哥玩云

星哥玩云
星哥玩云
分享互联网知识
用户数
4
文章数
19352
评论数
4
阅读量
8087709
文章搜索
热门文章
星哥带你玩飞牛NAS-6:抖音视频同步工具,视频下载自动下载保存

星哥带你玩飞牛NAS-6:抖音视频同步工具,视频下载自动下载保存

星哥带你玩飞牛 NAS-6:抖音视频同步工具,视频下载自动下载保存 前言 各位玩 NAS 的朋友好,我是星哥!...
星哥带你玩飞牛NAS-3:安装飞牛NAS后的很有必要的操作

星哥带你玩飞牛NAS-3:安装飞牛NAS后的很有必要的操作

星哥带你玩飞牛 NAS-3:安装飞牛 NAS 后的很有必要的操作 前言 如果你已经有了飞牛 NAS 系统,之前...
再见zabbix!轻量级自建服务器监控神器在Linux 的完整部署指南

再见zabbix!轻量级自建服务器监控神器在Linux 的完整部署指南

再见 zabbix!轻量级自建服务器监控神器在 Linux 的完整部署指南 在日常运维中,服务器监控是绕不开的...
飞牛NAS中安装Navidrome音乐文件中文标签乱码问题解决、安装FntermX终端

飞牛NAS中安装Navidrome音乐文件中文标签乱码问题解决、安装FntermX终端

飞牛 NAS 中安装 Navidrome 音乐文件中文标签乱码问题解决、安装 FntermX 终端 问题背景 ...
免费领取huggingface的2核16G云服务器,超简单教程

免费领取huggingface的2核16G云服务器,超简单教程

免费领取 huggingface 的 2 核 16G 云服务器,超简单教程 前言 HuggingFace.co...
阿里云CDN
阿里云CDN-提高用户访问的响应速度和成功率
随机文章
【开源神器】微信公众号内容单篇、批量下载软件

【开源神器】微信公众号内容单篇、批量下载软件

【开源神器】微信公众号内容单篇、批量下载软件 大家好,我是星哥,很多人都希望能高效地保存微信公众号的文章,用于...
星哥带你玩飞牛NAS-4:飞牛NAS安装istore旁路由,家庭网络升级的最佳实践

星哥带你玩飞牛NAS-4:飞牛NAS安装istore旁路由,家庭网络升级的最佳实践

星哥带你玩飞牛 NAS-4:飞牛 NAS 安装 istore 旁路由,家庭网络升级的最佳实践 开始 大家好我是...
小白也能看懂:什么是云服务器?腾讯云 vs 阿里云对比

小白也能看懂:什么是云服务器?腾讯云 vs 阿里云对比

小白也能看懂:什么是云服务器?腾讯云 vs 阿里云对比 星哥玩云,带你从小白到上云高手。今天咱们就来聊聊——什...
自己手撸一个AI智能体—跟创业大佬对话

自己手撸一个AI智能体—跟创业大佬对话

自己手撸一个 AI 智能体 — 跟创业大佬对话 前言 智能体(Agent)已经成为创业者和技术人绕...
还在找免费服务器?无广告免费主机,新手也能轻松上手!

还在找免费服务器?无广告免费主机,新手也能轻松上手!

还在找免费服务器?无广告免费主机,新手也能轻松上手! 前言 对于个人开发者、建站新手或是想搭建测试站点的从业者...

免费图片视频管理工具让灵感库告别混乱

一言一句话
-「
手气不错
星哥带你玩飞牛NAS-12:开源笔记的进化之路,效率玩家的新选择

星哥带你玩飞牛NAS-12:开源笔记的进化之路,效率玩家的新选择

星哥带你玩飞牛 NAS-12:开源笔记的进化之路,效率玩家的新选择 前言 如何高效管理知识与笔记,已经成为技术...
安装并使用谷歌AI编程工具Antigravity(亲测有效)

安装并使用谷歌AI编程工具Antigravity(亲测有效)

  安装并使用谷歌 AI 编程工具 Antigravity(亲测有效) 引言 Antigravity...
星哥带你玩飞牛NAS-16:不再错过公众号更新,飞牛NAS搭建RSS

星哥带你玩飞牛NAS-16:不再错过公众号更新,飞牛NAS搭建RSS

  星哥带你玩飞牛 NAS-16:不再错过公众号更新,飞牛 NAS 搭建 RSS 对于经常关注多个微...
自己手撸一个AI智能体—跟创业大佬对话

自己手撸一个AI智能体—跟创业大佬对话

自己手撸一个 AI 智能体 — 跟创业大佬对话 前言 智能体(Agent)已经成为创业者和技术人绕...
星哥带你玩飞牛NAS-11:咪咕视频订阅部署全攻略

星哥带你玩飞牛NAS-11:咪咕视频订阅部署全攻略

星哥带你玩飞牛 NAS-11:咪咕视频订阅部署全攻略 前言 在家庭影音系统里,NAS 不仅是存储中心,更是内容...