【Linux】循序渐进学运维-服务篇-rysnc+inotify实战

微笑、不失礼 提交于 2020-07-28 06:36:43

大家好,我是高胜寒,本文是Linux运维-循序渐进学运维-服务篇的第12篇文章

前言

我们使用rsync可以实现触发式文件同步,但是通过crontab守护进程触发,同步数据时间上会有延迟,而inotify正好弥补了crontab的缺陷,可以实时监控文件系统的增删改查变化,当文件有任何变动时,都会触发rsync同步。很好的解决了rsync同步实时性的问题。

实验环境

服务器1 192.168.1.64 gaosh-64
服务器2 192.168.1.22 gaosh-1

实验步骤:

  1. 配置ssh免秘钥登陆,且配置时间同步
  2. 设置rsync的配置文件,确保两台服务器可以互相推送和监控
  3. 配置inotify
  4. 测试是否实现同步

1. 配置ssh免秘钥登陆,且配置时间同步

[root@gaosh-1 ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
04:12:28:0a:04:3e:33:09:b7:c9:36:73:9b:ae:e3:de root@gaosh-1
The key's randomart image is:
+--[ RSA 2048]----+
|=...o..          |
|=ooo . .         |
|oBB .   .        |
|..++ o .         |
|    o   S        |
|   .             |
|    .            |
|  .o             |
| o+.E            |
+-----------------+
[root@gaosh-1 ~]# 

[root@gaosh-1 ~]# ssh-copy-id 192.168.1.64
The authenticity of host '192.168.1.64 (192.168.1.64)' can't be established.
RSA key fingerprint is 3a:13:d9:39:09:d1:7a:5c:0f:a7:08:ad:f9:ee:85:b5.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.1.64' (RSA) to the list of known hosts.
root@192.168.1.64's password: 
Now try logging into the machine, with "ssh '192.168.1.64'", and check in:

  .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

配置时间同步:
服务器1:

[root@gaosh-1 ~]# yum install ntpdate
[root@gaosh-1 ~]# ntpdate -u 0.pool.ntp.org

服务器2:

[root@gaosh-64 ~]# ntpdate -u 0.pool.ntp.org
17 Jul 08:34:01 ntpdate[11325]: adjust time server 84.16.67.12 offset -0.032762 sec
[root@gaosh-64 ~]# date
2020年 07月 17日 星期五 08:34:04 CST

2 . 设置rsync的配置文件,确保两台服务器可以互相推送和监控

a. 服务器1的配置:
1) 安装xinetd,并启动
[root@gaosh-1 ~]# mkdir /gitbackup
[root@gaosh-1 ~]# yum install xinetd
[root@gaosh-1 ~]# service xinetd restart
停止 xinetd:                                              [失败]
正在启动 xinetd:                                          [确定]
[root@gaosh-1 ~]# 
2) 修改配置文件

创建备份目录

mkdir /gitbackup
[root@gaosh-1 ~]# chmod 600 /gitbackup
[root@gaosh-1 ~]# vim /etc/rsyncd.conf

uid = root
gid = root
usechroot = no
max connections = 20
timeout = 600
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[backup]
path = /gitbackup
ignore errors
read only = false
writeonly = false
list = false
hosts allow = 192.168.1.0/24
hosts deny = 0.0.0.0/32
auth users = backuser
secrets file = /etc/rsync.passwd

3) 修改密码文件 并修改权限
[root@gaosh-1 ~]# cat /etc/rsync.passwd 
backuser:123456

[root@gaosh-1 ~]# chmod 600 /etc/rsync.passwd 
4) 启动并查看
[root@gaosh-1 ~]# rsync --daemon
[root@gaosh-1 ~]# ps -ef  |grep rsync
root      45336      1  0 08:45 ?        00:00:00 rsync --daemon
root      45338  45151  0 08:45 pts/2    00:00:00 grep rsync
[root@gaosh-1 ~]# 
b. 服务器2的配置
1) 安装xinetd,并启动
[root@gaosh-1 ~]# mkdir /gitbackup
[root@gaosh-1 ~]# yum install xinetd
[root@gaosh-1 ~]# systemctl restart  xinetd 
停止 xinetd:                                              [失败]
正在启动 xinetd:                                          [确定]
[root@gaosh-1 ~]# 
2) 修改配置文件

创建备份目录

mkdir /gitbackup
[root@gaosh-1 ~]# chmod 600 /gitbackup
[root@gaosh-1 ~]# vim /etc/rsyncd.conf

uid = root
gid = root
usechroot = no
max connections = 20
timeout = 600
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[backup]
path = /gitbackup
ignore errors
read only = false
writeonly = false
list = false
hosts allow = 192.168.1.0/24
hosts deny = 0.0.0.0/32
auth users = backuser
secrets file = /etc/rsync.passwd

3) 修改密码文件 并修改权限
[root@gaosh-1 ~]# cat /etc/rsync.passwd 
backuser:123456

[root@gaosh-1 ~]# chmod 600 /etc/rsync.passwd 
4) 启动并查看

[root@gaosh-64 ~]# rsync --daemon
[root@gaosh-64 ~]# ps -ef |grep rsync
root      11524      1  0 08:49 ?        00:00:00 rsync --daemon
root      11527  11158  0 08:49 pts/0    00:00:00 grep --color=auto rsync
4. 配置inotify

服务器1:

1) 下载epel源
[root@gaosh-1 ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
2) 安装inotify
yum install inotify-tools -y
5 . 测试

服务器2:

[root@gaosh-1 gitbackup]# pwd
/gitbackup
[root@gaosh-1 gitbackup]# touch test1
[root@gaosh-64 gitbackup]# rsync -avz /gitbackup/ backuser@192.168.1.17::backup
Password: 

服务器2
查看目录是否同步成功

[root@gaosh-64 gitbackup]# ls
test1

总结

rysnc+inotify实现代码或者文件的实时同步,很好的解决了crontab延迟的问题。

因为此时还没有学习shell脚本,所以暂时把shell脚本的内容省掉了,后期学完shell脚本后,我们在回来本篇文章增加脚本内容。实现自动同步的触发机制。

我是高胜寒,一个在教培行业不忘初心的人,如果你有更好的想法,可以留言与我一起交流,我们下篇文章再见

rsync系列文章:

【Linux】循序渐进学运维-服务篇-rysnc原理

【Linux】循序渐进学运维-服务篇-rysnc安装及使用

【Linux】循序渐进学运维-服务篇-rsync配置文件

【Linux】循序渐进学运维-服务篇-rsync实战

【Linux】循序渐进学运维-服务篇-inotify部署及应用

【Linux】循序渐进学运维-服务篇-rysnc+inotify实战

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!