8288分类目录 8288分类目录 8288分类目录
  当前位置:海洋目录网 » 站长资讯 » 站长资讯 » 文章详细 订阅RssFeed

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

来源:本站原创 浏览:99次 时间:2021-12-27

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

文章目录
    • 前言
    • 实验环境
    • a. 服务器1的配置:
    • b. 服务器2的配置
    • 4. 配置inotify
    • 5 . 测试
    • 1) 安装xinetd,并启动
    • 2) 修改配置文件
    • 3) 修改密码文件 并修改权限
    • 4) 启动并查看
    • 1) 安装xinetd,并启动
    • 2) 修改配置文件
    • 3) 修改密码文件 并修改权限
    • 4) 启动并查看
    • 1) 下载epel源
    • 2) 安装inotify
    • 1. 配置ssh免秘钥登陆,且配置时间同步
    • 2 . 设置rsync的配置文件,确保两台服务器可以互相推送和监控
    • 总结


前言

我们使用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-key�Ǽ�,�Һ�genGenerating 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-1The 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.64The 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)? yesWarning: 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_keysto 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.org17 Jul 08:34:01 ntpdate[11325]: adjust time server 84.16.67.12 offset -0.032762 sec[root@gaosh-64 ~]# date2020年 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.confuid = rootgid = rootusechroot = nomax connections = 20timeout = 600pid file = /var/run/rsyncd.pidlock file = /var/run/rsync.locklog file = /var/log/rsyncd.log[backup]path = /gitbackupignore errorsread only = falsewriteonly = falselist = falsehosts allow = 192.168.1.0/24hosts deny = 0.0.0.0/32auth users = backusersecrets 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 rsyncroot      45336      1  0 08:45 ?        00:00:00 rsync --daemonroot      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.confuid = rootgid = rootusechroot = nomax connections = 20timeout = 600pid file = /var/run/rsyncd.pidlock file = /var/run/rsync.locklog file = /var/log/rsyncd.log[backup]path = /gitbackupignore errorsread only = falsewriteonly = falselist = falsehosts allow = 192.168.1.0/24hosts deny = 0.0.0.0/32auth users = backusersecrets 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 rsyncroot      11524      1  0 08:49 ?        00:00:00 rsync --daemonroot      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::backupPassword:

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

[root@gaosh-64 gitbackup]# lstest1

总结

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

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

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

rsync系列文章:

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

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

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

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

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

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

  推荐站点

  • At-lib分类目录At-lib分类目录

    At-lib网站分类目录汇集全国所有高质量网站,是中国权威的中文网站分类目录,给站长提供免费网址目录提交收录和推荐最新最全的优秀网站大全是名站导航之家

    www.at-lib.cn
  • 中国链接目录中国链接目录

    中国链接目录简称链接目录,是收录优秀网站和淘宝网店的网站分类目录,为您提供优质的网址导航服务,也是网店进行收录推广,站长免费推广网站、加快百度收录、增加友情链接和网站外链的平台。

    www.cnlink.org
  • 35目录网35目录网

    35目录免费收录各类优秀网站,全力打造互动式网站目录,提供网站分类目录检索,关键字搜索功能。欢迎您向35目录推荐、提交优秀网站。

    www.35mulu.com
  • 就要爱网站目录就要爱网站目录

    就要爱网站目录,按主题和类别列出网站。所有提交的网站都经过人工审查,确保质量和无垃圾邮件的结果。

    www.912219.com
  • 伍佰目录伍佰目录

    伍佰网站目录免费收录各类优秀网站,全力打造互动式网站目录,提供网站分类目录检索,关键字搜索功能。欢迎您向伍佰目录推荐、提交优秀网站。

    www.wbwb.net