首页 > 其他分享 >综合架构-5 实时同步服务-rsync+crond+inotify

综合架构-5 实时同步服务-rsync+crond+inotify

时间:2024-03-20 23:23:25浏览次数:28  
标签:web rsync inotify bytes sh file txt crond sent

综合架构-5 实时同步服务-rsync+crond+inotify

增量实时备份-监控状态-利用rsync+inotify+crond

实现服务端和客户端b互相免密连接



ssh-keygen

cd /root/.ssh 

mv id_rsa.pub authorized_keys

scp  -r  /root/.ssh   10.0.1.113:/root 







企业文件目录增量实时同步 删除不同步
wget -O/etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
yum -y install inotify-tools  rsync


#!/bin/bash
path1=/data/web
ip=10.0.1.113         
/usr/bin/inotifywait -mrq --timefmt %y/%m'%d %H:/%M' --format '%T %w%f' -emodify,create,attrib $path1 | while read file;
do
rsync -avz $path1  root@$ip:/backup
echo "${file} was rsynced" >> /var/log/rsync.log 2>&1
done







具体实操演示

客户端
ip:10.0.1.113

mkdir -p /data/web

cd /data
mkdir web

以上是我自己没有这个目录,你如果有的话,就不需要了,生产环境中应根据自己的需求更换目录的



rsync -avz /data/web [email protected]::backup --password-file=/etc/rsync.password    不需要密码

或者
rsync -avz /etc/hosts [email protected]::backup    需要密码   
这里的密码是你配置的虚拟用户的密码



下载inotify-tools
wget -O/etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
yum -y install inotify-tools  rsync

创建一个目录或者你可以找个地方存放脚本

cd /data/sh

mkdir -p /data/sh

这里我有就不创建了

vi in.sh

#!/bin/bash
path1=/data/web
ip=10.0.1.113         
/usr/bin/inotifywait -mrq --timefmt %y/%m'%d %H:/%M' --format '%T %w%f' -emodify,create,attrib $path1 | while read file;
do
rsync -avz $path1  root@$ip:/backup
echo "${file} was rsynced" >> /var/log/rsync.log 2>&1
done


给予执行权限
chmod +x in.sh
bash in.sh









 代码注解


#!/bin/bash
path1=/data/web
ip=10.0.1.111         填的是rsync灾备服务器ip
/usr/bin/inotifywait -mrq --timefmt %y/%m'%d %H:/%M' --format '%T %w%f' -emodify,create,attrib $path1 | while read file;
do
rsync -avz $path1  root@$ip:/backup
echo "${file} was rsynced" >> /var/log/rsync.log 2>&1
done

 
 -emodify,  修改文件
 create, 创建
 attrib   修改属性
 
 自己也可以定义一下删除,这里我们是备份并监控
 delete
 






服务端
[root@web1 backup]# ls
data  hosts  passwd  web  www_2024-03-20.tar.gz

此时服务端应是已经配置过的服务端 因为它是灾备服务器 rsync都应该已经安装过来了

主要我们是在客户端写个脚本,监控他实时备份的过程
监控其他客户端的过程,实时的过程









效果测试:


客户端

此时你再打开一个窗口,开第二个窗口

用touch新建文件就可以了
再去看脚本的运行监控情况


[root@web02 ~]# cd /data/sh/
[root@web02 sh]# ls
backup.sh  in.sh
[root@web02 sh]# bash in.sh 
sending incremental file list
web/
web/1
web/10
web/100
web/11
web/12
web/13
web/14
web/15
web/16
web/17
web/18
web/19
web/2
web/20
web/21
web/22
web/23
web/24
web/25
web/26
web/27
web/28
web/29
web/3
web/30
web/31
web/32
web/33
web/34
web/35
web/36
web/37
web/38
web/39
web/4
web/40
web/41
web/42
web/43
web/44
web/45
web/46
web/47
web/48
web/49
web/5
web/50
web/51
web/52
web/53
web/54
web/55
web/56
web/57
web/58
web/59
web/6
web/60
web/61
web/62
web/63
web/64
web/65
web/66
web/67
web/68
web/69
web/7
web/70
web/71
web/72
web/73
web/74
web/75
web/76
web/77
web/78
web/79
web/8
web/80
web/81
web/82
web/83
web/84
web/85
web/86
web/87
web/88
web/89
web/9
web/90
web/91
web/92
web/93
web/94
web/95
web/96
web/97
web/98
web/99
web/{1.10}.txt

sent 5,039 bytes  received 1,939 bytes  13,956.00 bytes/sec
total size is 0  speedup is 0.00
sending incremental file list

sent 1,396 bytes  received 17 bytes  2,826.00 bytes/sec
total size is 0  speedup is 0.00
sending incremental file list
web/
web/1.txt
web/10.txt
web/2.txt
web/3.txt
web/4.txt
web/5.txt
web/6.txt
web/7.txt
web/8.txt
web/9.txt

sent 1,938 bytes  received 210 bytes  4,296.00 bytes/sec
total size is 0  speedup is 0.00
sending incremental file list

sent 1,571 bytes  received 17 bytes  3,176.00 bytes/sec
total size is 0  speedup is 0.00
sending incremental file list

sent 1,575 bytes  received 17 bytes  3,184.00 bytes/sec
total size is 0  speedup is 0.00
sending incremental file list

sent 1,571 bytes  received 17 bytes  3,176.00 bytes/sec
total size is 0  speedup is 0.00
sending incremental file list

sent 1,571 bytes  received 17 bytes  3,176.00 bytes/sec
total size is 0  speedup is 0.00
sending incremental file list

sent 1,571 bytes  received 17 bytes  3,176.00 bytes/sec
total size is 0  speedup is 0.00
sending incremental file list

sent 1,571 bytes  received 17 bytes  1,058.67 bytes/sec
total size is 0  speedup is 0.00
sending incremental file list

sent 1,575 bytes  received 17 bytes  3,184.00 bytes/sec
total size is 0  speedup is 0.00
sending incremental file list

sent 1,571 bytes  received 17 bytes  3,176.00 bytes/sec
total size is 0  speedup is 0.00
sending incremental file list

sent 1,571 bytes  received 17 bytes  3,176.00 bytes/sec
total size is 0  speedup is 0.00
sending incremental file list

sent 1,571 bytes  received 17 bytes  3,176.00 bytes/sec
total size is 0  speedup is 0.00
sending incremental file list

sent 1,575 bytes  received 17 bytes  3,184.00 bytes/sec
total size is 0  speedup is 0.00
sending incremental file list

sent 1,571 bytes  received 17 bytes  3,176.00 bytes/sec
total size is 0  speedup is 0.00
sending incremental file list

sent 1,571 bytes  received 17 bytes  3,176.00 bytes/sec
total size is 0  speedup is 0.00
sending incremental file list

sent 1,571 bytes  received 17 bytes  3,176.00 bytes/sec
total size is 0  speedup is 0.00
sending incremental file list

sent 1,571 bytes  received 17 bytes  3,176.00 bytes/sec
total size is 0  speedup is 0.00
sending incremental file list

sent 1,571 bytes  received 17 bytes  3,176.00 bytes/sec
total size is 0  speedup is 0.00
sending incremental file list

sent 1,571 bytes  received 17 bytes  3,176.00 bytes/sec
total size is 0  speedup is 0.00
sending incremental file list

sent 1,571 bytes  received 17 bytes  3,176.00 bytes/sec
total size is 0  speedup is 0.00
sending incremental file list

sent 1,575 bytes  received 17 bytes  3,184.00 bytes/sec
total size is 0  speedup is 0.00






服务端

[root@web1 ~]# cd /backup/web/
[root@web1 web]# ls
[root@web1 web]# ls
1           14     20  28     34     40  48     54     60  68     74     80  88     94
10          15     21  29     35     41  49     55     61  69     75     81  89     95
100         16     22  2.txt  36     42  4.txt  56     62  6.txt  76     82  8.txt  96
10.txt      17     23  3      37     43  5      57     63  7      77     83  9      97
11          18     24  30     38     44  50     58     64  70     78     84  90     98
{1.10}.txt  19     25  31     39     45  51     59     65  71     79     85  91     99
12          1.txt  26  32     3.txt  46  52     5.txt  66  72     7.txt  86  92     9.txt
13          2      27  33     4      47  53     6      67  73     8      87  93






注意:这个脚本运行时执行备份和实时同步传输 ,但是如果退出了,就不可以了,没办法实现监控和备份了,

bash in.sh &

[root@web02 sh]# bash in.sh &
[1] 3177


这个命令代表可以可以后台运行,他会给你个进程号
只要执行备份,你这边可以监控到的



演示效果

这两个客户端是一个哦,我分两个窗口打开的
客户端1
[root@web02 web]# touch nnwwqq{1..2}.txt



客户端2


[root@web02 sh]# bash in.sh &
[1] 3177
[root@web02 sh]# sending incremental file list
web/
web/nnwwqq1.txt
web/nnwwqq2.txt

sent 1,869 bytes  received 58 bytes  3,854.00 bytes/sec
total size is 0  speedup is 0.00
sending incremental file list

sent 1,790 bytes  received 17 bytes  3,614.00 bytes/sec
total size is 0  speedup is 0.00





服务器端

[root@web1 web]# ls
1           17     26     35     44     53     62     71     80     9   9.txt        nwq8.txt
10          18     27     36     45     54     63     72     81     90  nnwwqq1.txt  nwq9.txt
100         19     28     37     46     55     64     73     82     91  nnwwqq2.txt
10.txt      1.txt  29     38     47     56     65     74     83     92  nwq10.txt
11          2      2.txt  39     48     57     66     75     84     93  nwq1.txt
{1.10}.txt  20     3      3.txt  49     58     67     76     85     94  nwq2.txt
12          21     30     4      4.txt  59     68     77     86     95  nwq3.txt
13          22     31     40     5      5.txt  69     78     87     96  nwq4.txt
14          23     32     41     50     6      6.txt  79     88     97  nwq5.txt
15          24     33     42     51     60     7      7.txt  89     98  nwq6.txt
16          25     34     43     52     61     70     8      8.txt  99  nwq7.txt







需求:
比方说如果你想要他在晚上两点执行

此时就要配置定时任务了,用到crond这个工具




1.
编辑crontab文件
crontab -e



2.
0 2 * * * /bin/bash /data/sh/in.sh
在每天的凌晨2点0分(0 2)执行/data/sh/in.sh这个脚本。


3.
确保脚本有执行权限
chmod +x /data/sh/in.sh

4.
检查cron服务状态
crontab -l









具体演示过程

[root@web02 sh]# crontab -e
crontab: installing new crontab
添加下面这行

0 2 * * * /bin/bash /data/sh/in.sh >/dev/null 2>&1

检查一下
[root@web02 sh]# crontab -l
*/5 * * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1
#time sync by lidao at 2017-03-08
*/5 * * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1
0 2 * * * /bin/bash /data/sh/in.sh >/dev/null 2>&1

标签:web,rsync,inotify,bytes,sh,file,txt,crond,sent
From: https://www.cnblogs.com/nwq1101/p/18086362

相关文章

  • 常用命令--复制-备份--cp--mv--scp--rsync
    常用命令--复制-备份--cp--mv--scp--rsynccpcp命令用来将一个或多个源文件或者目录复制到指定的目的文件或目录。它可以将单个源文件复制成一个指定文件名的具体的文件或一个已经存在的目录下。cp命令还支持同时复制多个文件,当一次复制多个文件时,目标文件参数必须是一个已经存在......
  • 综合架构学习笔记-3---rsync-扩展脚本
    综合架构学习笔记-3---rsync-扩展脚本虚拟机测试环境ip10.0.1.0网关10.0.1.2子网掩码255.255.255.0知识回顾rsync如何部署?1.安装软件rsync2.编写配置文件虚拟用户备份目录密码文件具体流程演示linux系统安装部署服务流程:a下载安装软件yumb编写配置......
  • 综合架构学习笔记-2---rsync-实战
    综合架构学习笔记-2---rsync-实战实战1rsync守护进程部署测试环境--虚拟机环境ip10.0.1.0网关10.0.1.2子网掩码255.255.255.0步骤服务端第一步:下载安装软件 [root@localhost~]#yuminstall-yrsync 第二步:编写配置文件把原来的配置文件删除 [roo......
  • 常用命令rsyncscp-1
    常用命令:rsync/scpscpscp命令文件传输scp命令用于在Linux下进行远程拷贝文件的命令,和它类似的命令有cp,不过cp只是在本机进行拷贝不能跨服务器,而且scp传输是加密的。可能会稍微影响一下速度。当你服务器硬盘变为只读read only system时,用scp可以帮你把文件移出来。另外,scp还......
  • [转帖]linux-windows文件实时同步:Rsync使用教程
    http://luomuren.top/articles/2021/04/06/1617641017252.html#:~:text=linux-windows%E6%96%87%E4%BB%B6%E5%AE%9E%E6%97%B6%E5%90%8C%E6%AD%A5%EF%BC%9ARsync%E4%BD%BF%E7%94%A8%E6%95%99%E7%A8%8B%201%20%E4%B8%80%20%E3%80%81%E4%BB%80%E4%B9%88%E6%98%AFrync%20%EF%BC%9F......
  • rsync的部署
    Rsync服务模式-服务端配置为什么需要服务模式Rsync借助SSH协议同步数据存在的缺陷:1.使用系统用户(不安全)/etc/passwd2.使用普通用户(会导致权限不足情况)3.守护进程传输方式:rsync自身非常重要的功能(不使用系统用户,更加安全)1.安装rsyncyuminstallrsync-y2.修改......
  • Linux---rsync服务
    1.rsync简介rsync英文称为remotesynchronization,从软件的名称就可以看出来,rsync具有可使本地和远程两台主机之间的数据快速复制同步镜像、远程备份的功能,这个功能类似于ssh带的scp命令,但是又优于scp命令的功能,scp每次都是全量拷贝,而rsync可以增量拷贝。当然,rsync还可以在本地主......
  • 使用rsync快速清空文件和目录
    快速删除目录1)先建立一个空目录mkdir/data/blank2)用rsync删除目标目录rsync--delete-before-d/data/blank//var/spool/clientmqueue/这样目标目录很快就被清空了注:其中--delete-before接收者在传输之前进行删除操作快速删除大文件假如你有一些特别大的文件要删除,比如nohup.ou......
  • rsync配置推送文件
    A往B推送文件,B开启rsync服务即可B端操作vi/etc/rsyncd.confsecretsfile=/etc/rsync.passhostsallow=192.168.72.32authusers=rsyncuid=rootgid=rootreadonly=falseusechroot=yesauthusers=rsyncmaxconnections=4pidfile=/var/run/rsy......
  • wpf 数据绑定 INotifyPropertyChanged封装
    BindableBase.cspublicabstractclassBindableBase:INotifyPropertyChanged{publiceventPropertyChangedEventHandlerPropertyChanged;//调用方法:publicstringName{get=>name;set{SetProperty<string>(refname,value);}}......