綜合架構-5 實時同步服務-rsync+crond+inotify

liu4356發表於2024-03-20

綜合架構-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 rsync_backup@10.0.1.111::backup --password-file=/etc/rsync.password    不需要密碼

或者
rsync -avz /etc/hosts rsync_backup@10.0.1.111::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

相關文章