fllf

rysnc备份服务结合inotify

rysnc备份服务结合inotify

搭建

服务器端

1.首先关闭selinux,iptables

 
 
 
xxxxxxxxxx
2
 
 
 
1
setenfore 0 
2
systemctl stop firewalld
 
 

上面是临时关闭,永久关闭如下:

 
 
 
xxxxxxxxxx
3
 
 
 
1
sed -ir 's/SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config
2
source /etc/selinux/config
3
systemctl disable firewalld
 
 

2.编辑 /etc/rsyncd.conf文件

 
 
 
xxxxxxxxxx
17
 
 
 
1
uid = rsync
2
gid = rsync
3
use chroot = no
4
max connections = 100
5
pid file = /var/run/rsyncd.pid
6
lock file = /var/run/rsyncd.lock
7
log file = /var/log/rsyncd.log
8
9
[backup]
10
path = /var/backup
11
comment = rsync backup directory
12
ignore errors
13
read only = no
14
hosts allow = *
15
list = false
16
auth users = rsync_backup
17
secrets file = /etc/rsync.passwd
 
 

3.创建密码文件并修改权限

 
 
 
xxxxxxxxxx
3
 
 
 
1
[root@controller ~]# vim /etc/rsync.passwd
2
rysnc_backup:123456
3
[root@controller ~]# chmod 600 /etc/rsync.passwd
 
 

4.创建备份目录,并修改权限

 
 
 
xxxxxxxxxx
2
 
 
 
1
[root@controller ~]# mkdir /var/backup
2
[root@controller ~]# chown rsync.rsync /var/backup
 
 

5.重启服务即可

 
 
 
xxxxxxxxxx
1
 
 
 
1
systemctl restart rsyncd
 
 

客户端

1.配置密码文件/etc/rsync.passwd

 
 
 
xxxxxxxxxx
2
 
 
 
1
[root@node1 ~]# vim /etc/rsync.passwd
2
123456
 
 

2.推送文件到服务器

 
 
 
xxxxxxxxxx
4
 
 
 
1
[root@node1 ~]# rsync -avz test.txt    rsync://rsync_backup@192.168.139.105/backup --password-file=/etc/rsync.passwd
2
#或者
3
[root@node1 ~]# rsync -avz test.txt   rsync_backup@192.168.139.105::backup --password-file=/etc/rsync.passwd
4
#将以上命令写进cron定时任务实现定时备份
 
 

结合inotify实现实时同步

1.安装inotify工具

 
 
 
xxxxxxxxxx
1
 
 
 
1
yum -y install inotify-tools
 
 

2.选项说明

  • -m 持续监控
  • -r 递归
  • -q 静默,仅打印时间信息
  • --timefmt 指定输出时间格式
  • --format 指定事件输出格式
    %Xe 事件
    %w 目录
    %f 文件
  • -e 指定监控的事件
    access 访问
    modify 内容修改
    attrib 属性修改
    close_write 修改真实文件内容
    open 打开
    create 创建
    delete 删除
    umount 卸载 
  • inotify监控效果如下
 
 
 
xxxxxxxxxx
5
 
 
 
1
[root@node1 ~]# /usr/bin/inotifywait  -mrq  --format '%Xe  %w  %f' -e create,modify,delete,attrib,close_write  /tmp
2
MODIFY  /tmp/  test
3
CLOSE_WRITEXCLOSE  /tmp/  test
4
ATTRIB  /tmp/  test
5
 
 

rsync+inotify 脚本

简洁版

 
 
 
xxxxxxxxxx
8
 
 
 
1
[root@node1 ~]# vim rsyn-inotify.sh
2
[root@node1 ~]# cat rsyn-inotify.sh
3
#!/bin/bash
4
dir=/tmp/backup
5
/usr/bin/inotifywait  -mrq  --format '%w %f' -e create,delete,attrib,close_write  $dir | while read line;do
6
        cd  $dir  && rsync -az -R  --delete  .  rsync_backup@192.168.139.105::backup --password-file=/etc/rsync.passwd >/dev/null 2>&1
7
done  &
8
 
 

优化版:

 
 
 
x
 
 
 
1
#!/bin/bash
2
src=/tmp/backup                          # 需要同步的源路径
3
des=backup                            # 目标服务器上 rsync --daemon 发布的名称,rsync --daemon这里就不做介绍了,网上搜一下,比较简单。
4
rsync_passwd_file=/etc/rsyncd.passwd            # rsync验证的密码文件
5
ip1=192.168.139.105                # 目标服务器1
6
#ip2=192.168.0.19                # 目标服务器2
7
user=rsync_backup                          # rsync --daemon定义的验证用户名
8
cd ${src}                              # 此方法中,由于rsync同步的特性,这里必须要先cd到源目录,inotify再监听 ./ 才能rsync同步后目录结构一致,有兴趣的同学可以进行各种尝试观看其效果
9
/usr/bin/inotifywait -mrq --format  '%Xe %w%f' -e modify,create,delete,attrib,close_write,move ./ | while read file        # 把监控到有发生更改的"文件路径列表"循环
10
do
11
        INO_EVENT=$(echo $file | awk '{print $1}')      # 把inotify输出切割 把事件类型部分赋值给INO_EVENT
12
        INO_FILE=$(echo $file | awk '{print $2}')      # 把inotify输出切割 把文件路径部分赋值给INO_FILE
13
        # echo "-------------------------------$(date)------------------------------------"
14
        # echo $file
15
        #增加、修改、写入完成、移动进事件
16
        #增、改放在同一个判断,因为他们都肯定是针对文件的操作,即使是新建目录,要同步的也只是一个空目录,不会影响速度。
17
        if [[ $INO_EVENT =~ 'CREATE' ]] || [[ $INO_EVENT =~ 'MODIFY' ]] || [[ $INO_EVENT =~ 'CLOSE_WRITE' ]] || [[ $INO_EVENT =~ 'MOVED_TO' ]]        # 判断事件类型
18
        then
19
        #        echo 'CREATE or MODIFY or CLOSE_WRITE or MOVED_TO'
20
                rsync -azcR --password-file=${rsync_passwd_file} $(dirname ${INO_FILE}) ${user}@${ip1}::${des} >/dev/null 2>&1 #&&        # INO_FILE变量代表路径哦  -c校验文件内容
21
                #rsync -avzcR --password-file=${rsync_passwd_file} $(dirname ${INO_FILE}) ${user}@${ip2}::${des}
22
                #仔细看 上面的rsync同步命令 源是用了$(dirname ${INO_FILE})变量 即每次只针对性的同步发生改变的文件的目录(只同步目标文件的方法在生产环境的某些极端环境下会漏文件 现在可以在不漏>文件下也有不错的速度 做到平衡) 然后用-R参数把源的目录结构递归到目标后面 保证目录结构一致性
23
        fi
24
        #删除、移动出事件
25
        if [[ $INO_EVENT =~ 'DELETE' ]] || [[ $INO_EVENT =~ 'MOVED_FROM' ]]
26
        then
27
      #        echo 'DELETE or MOVED_FROM'
28
                rsync -azR --delete --password-file=${rsync_passwd_file} $(dirname ${INO_FILE}) ${user}@${ip1}::${des} >/dev/null 2>&1 # &&
29
#                rsync -avzR --delete --password-file=${rsync_passwd_file} $(dirname ${INO_FILE}) ${user}@${ip2}::${des}
30
                #看rsync命令 如果直接同步已删除的路径${INO_FILE}会报no such or directory错误 所以这里同步的源是被删文件或目录的上一级路径,并加上--delete来删除目标上有而源中没有的文件,这里不
31
能做到指定文件删除,如果删除的路径越靠近根,则同步的目录月多,同步删除的操作就越花时间。
32
        fi
33
        #修改属性事件 指 touch chgrp chmod chown等操作
34
        if [[ $INO_EVENT =~ 'ATTRIB' ]]
35
        then
36
      #        echo 'ATTRIB'
37
                if [ ! -d "$INO_FILE" ]                # 如果修改属性的是目录 则不同步,因为同步目录会发生递归扫描,等此目录下的文件发生同步时,rsync会顺带更新此目录。
38
                then
39
                        rsync -azcR --password-file=${rsync_passwd_file} $(dirname ${INO_FILE}) ${user}@${ip1}::${des} >/dev/null 2>&1 #&&            
40
#                      rsync -avzcR --password-file=${rsync_passwd_file} $(dirname ${INO_FILE}) ${user}@${ip2}::${des}
41
                fi
42
        fi
43
done &
44
 
 

 

相关文章:

  • 2021-11-19
  • 2021-11-12
  • 2021-11-19
  • 2021-11-02
  • 2021-11-02
  • 2021-07-23
  • 2021-11-01
  • 2021-11-19
猜你喜欢
  • 2017-11-23
  • 2021-11-02
  • 2021-11-12
相关资源
相似解决方案