zoukankan      html  css  js  c++  java
  • rsync+inotify同步备份文件

    前言

    rsync作用:man rsync可以看到解释为a fast, versatile, remote (and local) file-copying tool,主要进行文件的同步。

    inotify作用:man inotify可以看到解释为 monitoring file system events,主要是监控文件状态。

    配置环境

    本文中备份主机ip为192.168.1.159,hostname为inotify-slave

    宿主机ip为192.168.1.185,hostname为inotify-master

    在备份主机中运行rsync程序,然后在宿主机中使用inotify对files的状态进行监控,从而再使用rsync命令对文件进行同步

    rsync的配置

    1.slave端rsync的安装

    centos6一般默认有rsync,没有的话可以

    yum -y install rsync

    2.配置slave端rsync基础环境
    [root@inotify-slave ~]# useradd rsync -s /sbin/nologin  ##新建rsync用户,不需要登陆
    [root@inotify-slave ~]# grep rsync /etc/passwd
    rsync:x:500:500::/home/rsync:/sbin/nologin
    [root@inotify-slave ~]# mkdir /rsyncbackup   #创建rsync daemon工作模式的模块目录
    [root@inotify-slave ~]# chown -R rsync.rsync /rsyncbackup   
    #更改模块目录的用户组,如果rsync用户没有权限对模块目录进行写的话,后续会影响同步
    
    3.slave端rsync的配置文件
    [root@inotify-slave ~]# cat /etc/rsync.conf  ##没有此文件的自己新建即可
    uid = rsync                 
    gid = rsync
    use chroot = no
    max connections = 200
    timeout = 300
    pid file = /var/run/rsync.pid
    lock file = /var/run/rsync.lock
    log file = /var/log/rsync.log
    
    [backup]                          ##模块名称,可随意取
    path = /data/rsyncbackup          ##同步保存文件的路径
    ignore errors
    read only = no                    ##网络可写
    list = no                         ##设置的ip是否同步
    hosts allow = 192.168.1.0/24      ##同步的ip网段
    auth users = rsync_backup         ##rsync的虚拟用户,可随意
    secrets file = /etc/rsync.passwd  ##rsync虚拟用户的密码
    
    4.slave端rsync虚拟用户配置
    [root@inotify-slave ~]# echo "rsync_backup:chunlanyy" > /etc/rsync.passwd     ##格式为虚拟用户:虚拟用户密码
    [root@inotify-slave ~]# chmod 600 /etc/rsync.passwd  
    ##更改密码文件的权限,必须更改,否则在同步的时候会出现auth fail的错误
    
    5.启动slave端rsync服务
    [root@inotify-slave ~]# rsync --daemon --config=/etc/rsync.config   ##以daemon模式启动,同时加载对应的配置文件
    [root@inotify-slave ~]# ps aux  | grep rsync
    root      5642  0.0  0.0 103308   860 pts/3    S+   18:17   0:00 grep rsync
    root     26647  0.0  0.0 107628   552 ?        Ss   00:25   0:05 rsync --daemon --config=/etc/rsync.conf
    root     29663  0.0  0.0 182668  1196 ?        Ss   02:30   0:00 svnserve -d -r /data/rsyncbackup/
    [root@inotify-slave ~]# ss -tnlp | grep rsync
    LISTEN     0      5                        :::873                     :::*      users:(("rsync",26647,5))
    LISTEN     0      5                         *:873                      *:*      users:(("rsync",26647,4))
    rsync启动端口为873端口
    将rsync服务加载至开机启动中
    echo "/usr/bin/rsync --daemon --config=/etc/rsync.config" >>/etc/rc.local
    
    6.master端rsync配置
    [root@inotify-master ~]# cat /etc/rsync.passwd
    chunlanyy         ##此处密码为slave端rsync虚拟用户的密码,与slave端不是,这里只有密码。
    
    7.slave-master同步测试
    [root@inotify-master ~]# echo "marility to chunlanyy" > test.txt
    [root@inotify-master ~]# rsync -avz test.txt rsync_backup@192.168.1.159::backup --password-file=/etc/rsync.passwd
    sending incremental file list
    test.txt
    
    sent 91 bytes  received 33 bytes  82.67 bytes/sec
    total size is 22  speedup is 0.18
    
    其中rsync_backup为slave端rsync的虚拟用户
    192.168.1.159为slave端ip
    ::backup,双冒号,其中backup为slave端设定的rsync的工作模块,并非文件存放路径,这与scp的格式略有不同,
    
    切换至slave端的rsync工作目录,可以查看到生成的文件已经出现
    [root@inotify-slave rsyncbackup]# pwd
    /data/rsyncbackup
    [root@inotify-slave rsyncbackup]# ll
    total 4
    -rw-r--r--. 1 rsync rsync   22 Feb 15  2017 test.txt
    [root@inotify-slave rsyncbackup]# cat test.txt
    marility to chunlanyy
    

    inotify配置

    1.inotify的安装配置

    [root@inotify-master ~]# yum install -y inotify-tools

    2.inotify自动检测脚本
    [root@inotify-master ~]# cat inotify.sh
    #!/bin/bash
    
    ## moniter the file
    
    host01=192.168.1.159  ##slave主机ip
    src=/data/svn/repos   ##master需要同步文件的路径
    dst=backup            ##slave中rsync的模块
    user=rsync_backup     ##slave中rsync的虚拟用户
    rsync_passfile=/etc/rsync.passwd
    /usr/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e close_write,delete,create,attrib $src | while read file
    do
    	cd $src && rsync -aruz -R --delete ./  --timeout=100 $user@$host01::$dst --password-file=${rsync_passfile} >/dev/null 2>&1
    
    done
    

    将该脚本启动放置于后台

    [root@inotify-master ~]# nohup /bin/bash inotify.sh 2>1&
    [1] 11315
    [root@inotify-master ~]# ps aux | grep inotify
    root     11315  0.0  0.0 106096  1168 pts/1    S    19:02   0:00 /bin/bash inotify.sh
    root     11316  0.0  0.0   6320   752 pts/1    S    19:02   0:00 /usr/bin/inotifywait -mrq --timefmt %d/%m/%y %H:%M
    --format %T %w%f -e close_write,delete,create,attrib /data/svn/repos
    root     11317  0.0  0.0 106096   668 pts/1    S    19:02   0:01 /bin/bash inotify.sh
    root     14666  0.0  0.0 103308   864 pts/1    S+   19:37   0:00 grep inotify
    
    3.自动检测测试
    [root@inotify-master repos]# mkdir -pv /data/svn/repos/test
    mkdir: created directory `/data/svn/repos/test'
    [root@inotify-master repos]# cd /data/svn/repos/test
    [root@inotify-master test]# ls
    [root@inotify-master test]# for a in `seq 200`;do touch $a.txt;done   ##新建200个文件
    [root@inotify-master test]# ll
    total 0
    -rw-r--r--. 1 root root 0 Feb 15 19:12 100.txt
    -rw-r--r--. 1 root root 0 Feb 15 19:12 101.txt
    .
    .
    .
    -rw-r--r--. 1 root root 0 Feb 15 19:12 9.txt
    
    查看slave端目录
    [root@inotify-slave test]# pwd
    /data/rsyncbackup/test
    [root@inotify-slave test]# ll
    total 0
    -rw-r--r--. 1 rsync rsync 0 Feb 15  2017 100.txt
    -rw-r--r--. 1 rsync rsync 0 Feb 15  2017 101.txt
    .
    .
    .
    -rw-r--r--. 1 rsync rsync 0 Feb 15  2017 9.txt
    
    可以看到slave端已经将文件同步过来
    
    [root@inotify-master repos]# ls
    conf  db  format  hooks  locks  README.txt  test
    [root@inotify-master repos]# du -sh
    125M	.
    
    [root@inotify-slave rsyncbackup]# ls
    conf  db  format  hooks  locks  README.txt  test
    [root@inotify-slave rsyncbackup]# du -sh
    125M	.
    
    可以看到两者的文件及大小一致
    
    删除master端的目录,检测是否能同步
    [root@inotify-master repos]# rm test/ -rf
    [root@inotify-master repos]# ls
    conf  db  format  hooks  locks  README.txt
    
    [root@inotify-slave rsyncbackup]# ls
    conf  db  format  hooks  locks  README.txt
    
    可以看到当master端删除文件后,slave端也同步删除
    
    4.将该脚本置于开机启动中

    echo "/usr/bin/nohup /root/inotify.sh 2>&1" >>/etc/rc.local
    chmod +x /etc/rc.local

    同步之后的备份

    虽然inotify+rsync可以将文件进行备份,但是由于设定在master端进行文件删除时,也会跟着删除(主要是为了保证slave端与master端文件的一致性),故为避免目录文件被误删引起的悲剧,故每周或者每日,还需要对文件进行全量备份,即tar+scp命令的使用

  • 相关阅读:
    VScode 关闭回车后自动格式化代码
    『转载』专利申请
    『转载』 免费公用DNS服务及三大运营商DNS大全 含IPV4和IPV6
    正则表达式和元字符
    随机背景图
    秒表
    数组相关的函数
    对象的结构语法
    数组的结构语法
    展开合并运算符
  • 原文地址:https://www.cnblogs.com/marility/p/7278512.html
Copyright © 2011-2022 走看看