美文网首页
第五章 rsync + inotify实现文件目录同步备份

第五章 rsync + inotify实现文件目录同步备份

作者: simok | 来源:发表于2019-02-21 15:34 被阅读0次

    一、linux 6.5

    • 同步目录备份文件的客户端

    • 安装rsync xinetd

    yum -y install xinetd rsync

    • 创建并配置rsyncd.conf

    vi /etc/rsyncd.conf

    • 创建帐号密码文件

    echo test:test123 > /etc/rsyncd.passwd

    uid = root
    gid = root
    use chroot = no
    max connections = 100 
    log file = /var/run/rsyncd.log
    pid file = /var/run/rsyncd.pid
    lock file = /var/run/rsyncd.lock
    [rsync]
    path = /home/share_backup/   
    comment = rsync
    list = yes
    read only = no
    port = 873
    timeout = 200 
    write only = no
    auth users = test
    secrets file = /etc/rsyncd.passwd
    ignore errors = yes
    hosts allow = 192.168.1.2  ##服务端的IP
    
    • 启动

    /etc/init.d/xinetd start
    /usr/bin/rsync --daemon --config=/etc/rsyncd.conf
    

    建议设置为开机自启动在/etc/rc.local下


    • 存储文件的服务端

    • 安装inotify

    yum -y install epel-release
    yum -y install inotify-tools

    • 创建密码文件

    echo test123 > /etc/rsyncd.passwd

    • 自定义目录下创建脚本文件

    vi inotify.sh

    #!/bin/bash
    host=192.168.1.1    ###客户端的IP
    data_dir=/home/files/    ##存源文件的目录
    dst=rsync
    username=test
    /usr/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $data_dir | while read files
    do
                    /usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/rsyncd.passwd $data_dir $username@$host::$dst
                    echo "${files} was rsynced" >> /tmp/rsync.log 2>&1 #写入日志
    done
    
    
    • 启动脚本

    bash inotify.sh &
    

    建议设置为开机自启动在/etc/rc.local下

    • 测试以及注意事项

    1 在服务端的/home/files/创建文件,查看客户端是否同步。
    2 注意selinux iptables等限制。


    二、linux 7

    • 同步目录备份文件的客户端

    • 安装与linux6.5类似

    • rsyncd.conf 配置

    vim /etc/rsyncd.conf 
    uid = root
    gid = root
    use chroot = no
    max connections = 2000
    pid file = /var/run/rsyncd.pid
    exclude = lost+found/
    transfer logging = yes
    timeout = 200
    log file = /var/run/rsyncd.log
    pid file = /var/run/rsyncd.pid
    lock file = /var/run/rsyncd.lock
    log format = %t %a %m %f %b
    [rsync]
    path = /var/www/html   //必须存在
    comment = rsync
    list = yes
    read only = no
    write only = no
    auth users = test
    secrets file = /etc/rsyncd.secret
    ignore errors = yes
    hosts allow = *
    
    • 创建帐号密码文件 并启动

    echo test:test123 > /etc/rsyncd.secret
    /usr/bin/rsync --daemon --config=/etc/rsyncd.conf


    • 存储文件的服务端

    • 安装

    1.yum -y install epel-release
    yum -y install inotify-tools

    • 创建密码文件

    echo test123 > /etc/rsyncd.secret
    文件夹 /var/www/html 存在

    • 后台执行脚本

    host=192.168.122.8   //服务端ip
    src=/var/www/html
    dst=rsync
    user=test
    rsync_passfile=/etc/rsyncd.secret
    inotify_home=/usr/
    if [ ! -e "$src" ] \
    || [ ! -e "${rsync_passfile}" ] \
    || [ ! -e "${inotify_home}/bin/inotifywait" ] \
    || [ ! -e "/usr/bin/rsync" ];
    then
    echo "Check File and Directory"
    exit 9
    fi
    ${inotify_home}/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e close_write,delete,create,attrib $src \
    | while read file
    do
    rsync -avzP --delete --timeout=100 --password-file=${rsync_passfile} $src $user@$host::$dst >/dev/null 2>&1
    cd $src && rsync -aruz -R --delete ./ --timeout=100 $user@$host::$dst --password-file=${rsync_passfile} >/dev/null 2>&1
    done
    exit 0
    
    • 测试:在客户端/var/www/html下创建文件,查看服务端是否同步

    相关文章

      网友评论

          本文标题:第五章 rsync + inotify实现文件目录同步备份

          本文链接:https://www.haomeiwen.com/subject/ezbryqtx.html