004、inotify+rsync实现实时同步
本文最后更新于 66 天前,其中的信息可能已经过时,如有错误请发送邮件到wuxianglongblog@163.com

inotify+rsync实现实时同步

第2章 inotify+rsync实时同步服务部署

2.1 第一个里程碑:部署rsync服务

2.1.1 rsync服务端部署
1)软件是否存在
[root@backup ~]# rpm -qa |grep rsyncrsync-3.0.6-12.el6.x86_64
#需求:查询到某个命令非常有用。但是不知道属于哪个软件包
yum provides rysnc 
provides   Find what package provides the given value
2)进行软件服务配置
[root@backup ~]# vim /etc/rsyncd.confuid = rsync
gid = rsync
use chroot = no
max connections = 200
timeout = 300
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
ignore errors
read only = false
list = false
hosts allow = 172.16.1.0/24
auth users = rsync_backup
secrets file = /etc/rsync.password
[backup]
comment = "backup dir by oldboy"
path = /backup
[nfsbackup]
comment = "nfsbackup dir by hzs"
path = /nfsbackup
3)创建rsync管理用户
[root@backup ~]# useradd -s /sbin/nologin -M rsync
4)创建数据备份储存目录,目录修改属主
[root@backup ~]# mkdir /nfsbackup/[root@backup ~]# chown -R rsync.rsync /nfsbackup/
5)创建认证用户密码文件并进行授权600
echo "rsync_backup:oldboy123">>/etc/rsync.password
chmod 600 /etc/rsync.password
6)启动rsync服务
rsync --daemon
至此服务端配置完成
[root@backup ~]# ps -ef |grep rsyncroot       2076      1  0 17:05 ?        00:00:00 rsync --daemon
root       2163   1817  0 17:38 pts/1    00:00:00 grep --color=auto rsync
2.1.2 rsync客户端配置
1)软件是否存在
[root@backup ~]# rpm -qa |grep rsyncrsync-3.0.6-12.el6.x86_64
2)创建安全认证文件,并进行修改权限600
echo "oldboy123">>/etc/rsync.password
chmod 600 /etc/rsync.password
3)测试数据传输
[root@nfs01 sersync]# rsync -avz /data  rsync_backup@172.16.1.41::nfsbackup  --password-file=/etc/rsync.passwordsending incremental file list
data/
data/.hzs
data/.tar.gz
data/.txt

2.2 第二个里程碑:部署inotify服务

首先先确认是否有epel源用来安装inotify-tools软件

[root@nfs01 ~]# yum repolistLoaded plugins: fastestmirror, security
Loading mirror speeds from cached hostfile

 * base: mirrors.aliyun.com
 * epel: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
   repo id  repo name                                       status
   base     CentOS-6 - Base - mirrors.aliyun.com             6,706
   epel     Extra Packages forEnterprise Linux 6 - x86_64  12,401
   extras   CentOS-6 - Extras - mirrors.aliyun.com              46
   updates  CentOS-6 - Updates - mirrors.aliyun.com            722
   repolist: 19,875
2.2.1 安装inotify软件

两种安装方式
1)yum安装

yum install -y inotify-tools

2) 手工编译安装

注:
手工编译安装方式需要到github上进行下载软件包
 inotify软件的参考资料链接:
https://github.com/rvoicilas/inotify-tools/wiki
2.2.2 查看inotify安装上的两个命令(inotifywait,inotifywatch)
[root@nfs01 ~]# rpm -ql inotify-tools/usr/bin/inotifywait      #主要/usr/bin/inotifywatch
2.2.2.1 inotifywait和inotifywatch的作用:

一共安装了2个工具(命令),即inotifywait和inotifywatch
inotifywait : 在被监控的文件或目录上等待特定文件系统事件(open close delete等)发生,
执行后处于阻塞状态,适合在shell脚本中使用
inotifywatch :收集被监控的文件系统使用的统计数据,指文件系统事件发生的次数统计。
说明:yum安装后可以直接使用,如果编译安装需要进入到相应软件目录的bin目录下使用

命令 man手册说明

man inotifywaitinotifywait - wait for changes to files using inotify

使用inotify进行监控,等待产生变化的文件信息

man inotifywatchinotifywatch - gather filesystem access statistics using inotify

使用inotify进行监控,收集文件系统访问统计佶息

2.3 第三个里程碑:编写脚本,实现rsync+inotify软件功能结合

2.3.1 rsync服务命令:
rsync -avz --delete /data/ rsync_backup@172.16.1.41::nfsbackup --password-file=/etc/rsync.password
2.3.2 inotify服务命令:
inotifywait -mrq /data -format "%w%f"-e create,delete,move_to,close_write
2.3.3 编写脚本:
[root@nfs01 sersync]# vim /server/scripts/inotify.sh
#!/bin/bashinotifywait -mrq /data --format "%w%f"-e create,delete,moved_to,close_write|\
while read line
do
        rsync -az --delete /data/ rsync_backup@172.16.1.41::nfsbackup --password-
file=/etc/rsync.password
done
脚本说明:
    for循环会定义一个条件,当条件不满足时停止循环
    while循环:只要条件满足就一直循环下去
2.3.4 对脚本进行优化
#!/bin/bashPath=/data
backup_Server=172.16.1.41
/usr/bin/inotifywait -mrq --format '%w%f'-e create,close_write,delete /data  | while read line  
do
    if[ -f $line ];then
        rsync -az $line --delete rsync_backup@$backup_Server::nfsbackup --password-file=/etc/rsync.password
    else
        cd $Path &&\
        rsync -az ./ --delete rsync_backup@$backup_Server::nfsbackup --password-file=/etc/rsync.password
    fi
done

2.4 第四个里程碑:测试编写的脚本

2.4.1 让脚本在后台运行

在/data 目录先创建6个文件

[root@nfs01 data]# sh  /server/scripts/inotify.sh &[root@nfs01 data]# touch {1..6}.txt
#在backup服务器上,已经时候同步过去了6个文件。
[root@backup ~]# ll /nfsbackup/total 8
-rw-r--r-- 1 rsync rsync 0 Oct 17 12:06 1.txt
-rw-r--r-- 1 rsync rsync 0 Oct 17 12:06 2.txt
-rw-r--r-- 1 rsync rsync 0 Oct 17 12:06 3.txt
-rw-r--r-- 1 rsync rsync 0 Oct 17 12:06 4.txt
-rw-r--r-- 1 rsync rsync 0 Oct 17 12:06 5.txt
-rw-r--r-- 1 rsync rsync 0 Oct 17 12:06 6.txt

2.5 利用while循环语句编写的脚本停止方法(kill)

ctrl+z暂停程序运行,kill -9杀死

不要暂停程序,直接利用杀手三剑客进行杀进程
说明:kill三个杀手不是万能的,在进程暂停时,无法杀死;kill -9 (危险)

2.5.1 查看后台都要哪些程序在运行
[root@nfs01 data]# jobs[1]+  Running                 sh /server/scripts/inotify.sh &
2.5.2 fg将后台的程序调到前台来
[root@nfs01 data]# fg 1sh /server/scripts/inotify.sh

2.6 进程的前台和后台运行方法:

fg    -- 前台
bg    -- 后台
2.6.1 脚本后台运行方法
1、sh inotify.sh &
2、nohup sh inotify.sh &
3、screen实现脚本程序后台运行
sh /server/scripts/inotify.sh &
nohup
nohup sh inotify.sh &

2.7 screen实现脚本程序后台运行

2.7.1 经过yum查找发现screen命令属于screen包
[root@test ~]# yum provides screenLoaded plugins: fastestmirror, security
Loading mirror speeds from cached hostfile
base: mirrors.aliyun.com
epel: mirrors.aliyun.com
extras: mirrors.aliyun.com
updates: mirrors.aliyun.com
base                                                      | 3.7 kB     00:00    
epel                                                      | 4.3 kB     00:00    
extras                                                    | 3.4 kB     00:00    
updates                                                   | 3.4 kB     00:00    
screen-4.0.3-19.el6.x86_64 : A screen manager that supports multiple logins on
                        : one terminal
Repo        : base
Matched from:
2.7.2 安装screen软件
[root@test ~]# yum install -y  screen
2.7.3 screen命令的参数
在shell中输入 screen即可进入screen 视图
[root@test ~]# screen
Screen实现后台运行程序的简单步骤:
  screen -ls :可看screen会话
  screen -r ID :指定进入哪个screen会话
Screen命令中用到的快捷键
  Ctrl+a c :创建窗口
  Ctrl+a w :窗口列表
  Ctrl+a n :下一个窗口
  Ctrl+a p :上一个窗口
  Ctrl+a 0-9 :在第0个窗口和第9个窗口之间切换
  Ctrl+a K(大写) :关闭当前窗口,并且切换到下一个窗口 ,
            (当退出最后一个窗口时,该终端自动终止,并且退回到原始shell状态)
  exit :关闭当前窗口,并且切换到下一个窗口
    (当退出最后一个窗口时,该终端自动终止,并且退回到原始shell状态)
  Ctrl+a d :退出当前终端,返回加载screen前的shell命令状态
  Ctrl+a " : 窗口列表不同于w
谨此笔记,记录过往。凭君阅览,如能收益,莫大奢望。
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇