综合架构-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