两台linux服务器使用rsync+inotify实现文件实时双向同步

1.安装rsync (两台服务器都要安装)

        yum在线:

        yum install -y rsync

        离线:

        cd /usr/local

        wget https://download.samba.org/pub/rsync/src/rsync-3.1.2.tar.gz

        tar -zxvf rsync-3.1.2.tar.gz

        cd rsync-3.1.2

        ./configure --prefix=/usr/local/rsync

        make

        make install

2.创建rsync配置文件

        yum在线:

        vi /etc/rsyncd.conf

        离线:

        vi /usr/local/rsync/rsyncd.conf

 

        配置文件内容:

        #rsync通用配置文件,配置的注释不要写在配置后面,否则会有问题

        uid = root

        gid = root

        use chroot = 0

        port = 873

        #允许ip访问设置,请根据实际需要进行配置,这里为了方便设为全网段 *

        # hosts allow = 192.168.0.1/255.255.255.0 198.162.145.1 10.0.1.0/255.255.255.0

        hosts allow = *

        max connections = 0

        timeout = 300

        pid file = /var/run/rsyncd.pid

        ock file = /var/run/rsyncd.lock

        log file = /var/log/rsyncd.log

        log format = %t %a %m %f %b

        transfer logging = yes

        syslog facility = local3

        #方括号中为模块声明,对应命名,这里master_web对应了主web机配置

        [master_web]

        #指定当前模块在rsync服务器上的同步路径,该参数是必须指定的

        path = /app/web/data-share/static/

        #注释,可以同模块名一样,从服务器可都为slave_web

        comment = master_web

        ignore errors

        #是否允许客户端上传文件

        read only = no

        list = no

        #指定由空格或逗号分隔的用户名列表,只有这些用户才允许连接该模块

        auth users = rsync

        #保存密码和用户名文件,需要自己生成

        secrets file = /etc/rsyncd.passwd

3.创建用户名密码文件

        yum在线:

        vi /etc/rsyncd.passwd

        vi /root/rsyncd.passwd

        离线:

        vi /usr/local/rsync/rsyncd.passwd

        vi /root/rsyncd.passwd

 

        文件内容:

        #/etc/rsyncd.passwd中内容格式 用户名:密码 ,两台服务器设置相同的用户名密码 rsync:123456

        #/root/rsyncd.passwd中内容只需要填写从服务器的密码,两台服务器设置相同的密码 123456

4.给文件赋权限

    Yum在线:

    chmod 600 /etc/rsyncd.passwd

    chmod 600 /root/rsyncd.passwd

    离线:

    chmod 600 /usr/local/rsync/rsyncd.passwd

    chmod 600 /root/rsyncd.passwd

5.以守护进程方式启动rsync服务

    Yum在线:

    /usr/bin/rsync --daemon --config=/etc/rsyncd.conf

    离线:

    /usr/local/rsync/bin/rsync --daemon --config=/usr/local/rsync/rsyncd.conf

6.添加开机自启动

    yum在线:

    echo "/usr/bin/rsync --daemon --config=/etc/rsyncd.conf">>/etc/rc.local

    离线:

    echo "/usr/local/rsync/bin/rsync --daemon --config=/usr/local/rsync/rsyncd.conf">>/etc/rc.local

7.验证是否可以手动同步

    # master_web 为从web服务器的 rsyncd.conf 中配置的模块名,

    # rsync 为从web服务器的中 rsyncd.passwd 中配置的用户名

    78服务器给79服务器同步:

    rsync -vzrtopg --delete --progress /usr/local/data-share/static/ rsync@xx.xx.xx.79::master_web --password-file=/root/rsyncd.passwd

    定时任务:

    echo '*/1 * * * * rsync -vzrtopg --delete --progress /usr/local/data-share/static/ rsync@xx.xx.xx.79::master_web --    password-file=/root/rsyncd.passwd > /dev/null 2>&1 &' >> /var/spool/cron/root

    79服务器给78服务器同步:

    rsync -vzrtopg --delete --progress /usr/local/data-share/static/ rsync@xx.xx.xx.78::master_web --password-file=/root/rsyncd.passwd

    定时任务:

    echo '*/1 * * * * rsync -vzrtopg --delete --progress /usr/local/data-share/static/ rsync@xx.xx.xx.78::master_web --password-file=/root/rsyncd.passwd > /dev/null 2>&1 &' >> /var/spool/cron/root

8.安装inotify(两台服务器都要安装)

    wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz --no-check-certificate

    tar -zxvf inotify-tools-3.14.tar.gz

    makedir /usr/local/inotify

    cd inotify-tools-3.14.tar.gz

    ./configure --prefix=/usr/local/inotify

    make && make install

    # 查看是否安装成功

    ls -alh /usr/local/inotify/bin/inotify*

    # 建立软连接

    ln -s /usr/local/inotify/bin/inotifywait /usr/bin/inotifywait

    ln -s /usr/local/inotify/bin/inotifywatch /usr/bin/inotifywatch

9.创建并配置inotify_back.sh同步监控脚本

    vi /usr/local/inotify/inotify_back.sh

    配置文件内容:

    78服务器:

    #!/bin/bash

    src=/app/web/data-share/static/

    /usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e close_write,delete,create,attrib $src | while read file

    do

    rsync -vzrtopg --delete --progress /usr/local/data-share/static/ [email protected]::master_web --password-file=/root/rsyncd.passwd

    echo "${file} was rsynced" >> /tmp/rsync.log 2>&1

    done

    79服务器:

    #!/bin/bash

    src=/app/web/data-share/static/

    /usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e close_write,delete,create,attrib $src | while read file

    do

    rsync -vzrtopg --delete --progress /usr/local/data-share/static/ [email protected]::master_web --password-file=/root/rsyncd.passwd

    echo "${file} was rsynced" >> /tmp/rsync.log 2>&1

    done

10.给inotify_back.sh脚本赋可执行权限

    chmod +x inotify_back.sh

11.运行inotify_back.sh同步监控脚本和配置守护进程

    nohup ./inotify_back.sh &

    #建立守护进程运行inotify_back.sh脚本

    echo "nohup ./inotify_back.sh &" >> /etc/rc.local

转载于:https://my.oschina.net/u/4148675/blog/3083081

你可能感兴趣的:(两台linux服务器使用rsync+inotify实现文件实时双向同步)