rsync+inotify文件同步

rsync+inotify文件同步

   在服务器中,通常结合计划任务、shell脚本来执行本地备份。为了进一步提高备份的可靠性,使用异地备份也是非常重要的,利用rsync工具,可以实现快速、高效的异地备份。本篇博客将配置rsync+crond实现定时备份、配置ssh+rsync+inotify实现触发式备份

rsync概述

   rsync(Remote Sync,远程同步)是一个开源的快速备份工具,适用于异地备份、镜像服务器等。作为一种最常用的文件备份工具,往往是Linux和UNIX系统默认安装的基本组件之一

具有以下特性:

(1)可以在不同主机间镜像同步整个目录树和文件系统

(2)可以很容易做到保持原来文件的权限、时间、软硬链接等

(3)支持增量备份

(3)无须特殊权限即可安装

(4)优化的同步算法,传输前执行压缩,文件传输效率高

(5)可以使用 rsh、ssh 方式来传输文件

(6)支持匿名传输,以方便进行网站镜象

192.168.175.129同步源(文件往这里传)

192.168.175.128客户端(文件提交)

例子1: rsync手动同步

 设置同步源:vi /etc/rsyncd.conf

#目录是:/var/www/html,那么这个目录权限用户是nginx
 uid = nginx 
 gid = nginx
 port = 873                            //监听端口
 host all = 192.168.175.129     //监听地址,本机地址
 use chroot = yes                   //禁锢在源目录
 max connections = 4            
 timeout = yes                   
 hosts allow = 192.168.175.128    //允许访问的客户机地址
# pid file = /var/run/rsyncd.pid
# exclude = lost+found/
# transfer logging = yes
# timeout = 900
# ignore nonreadable = yes
# dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2

[webroot]                          //共享模块名称
path=/var/www/html        //源目录的路径
comment = rsync files     //描述信息
ignore errors
read only = no                   //是否为只读
list = yes                        
auth users = muyang        //授权账户,如果采用匿名账户,可以去掉
secrets file = /etc/rsync_server.passwd   
//存放授权账户信息的数据文件

  

//为备份账户建立数据文件: //用:分隔账户和密码,无需建立同名系统用户

/etc/rsync_server.passwd
muyang:rsync

  

//修改权限,避免信息泄露

 chmod 600 /etc/rsync_server.passwd

  

 vim /etc/sysconfig/selinux   //关闭selinux

SELINUX=disabled

  

setenforce 0                 //立即生效

iptables -I INPUT -p tcp --dport 873 -j ACCEPT   //允许tcp873端口通过防火墙

启动rsync

rsync –daemon –config=/etc/rsyncd.conf 
开机启动: 
vim /etc/rc.local 
/usr/bin/rsync –daemon

执行同步:将128目录下文件,拉取129目录

rsync -avz muyang@192.168.175.128::webroot /root

  

例2:将128目录下的文件同步到129目录下

  • 安装inotify-tools工具

  使用inotify机制还需要安装inotify-tools,以便提供inotifywait、inotifywatch辅助工具程序,用来监控、汇总改动情况。

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

  

1. 调整inotify内核参数

    Linux内核中,默认的inotify机制提供了三个调控参数,分别表示监控事件队列、最多监控实例数、每个实例最多监控文件数。当需要监控的目录、文件数量过多时,建议加大这三个参数

[root@localhost ~]# cat /proc/sys/fs/inotify/max_queued_events 
16384                                               //监控事件队列默认16384
[root@localhost ~]# cat /proc/sys/fs/inotify/max_user_instances 
128                                                 //最多监控实例数默认128
[root@localhost ~]# cat /proc/sys/fs/inotify/max_user_watches 
8192                                                //每个实例最多监控文件数默认8192
[root@localhost ~]# vim /etc/sysctl.conf         //如果监控文件过多,建议加大数量 
fs.inotify.max_queued_events=16384               //添加三行
fs.inotify.max_user_instances=1024
fs.inotify.max_user_watches=1048576
[root@localhost ~]# sysctl -p                    //立即生效

  

inotifywait:可监控modify(修改)、create(创建)、move(移动)、delete(删除)、attrib(属性更改)等各种事件,一有变动立即输出结果

inotifywatch:可用来收集文件系统变动情况,并在运行结束后输出汇总的变化情况

-m  表示持续监控

-r  表示递归整个目录

-q  表示简化输出信息

-e  用来指定监控哪些事件

在rsync同步源启用密钥对验证

[root@localhost html]# vim /etc/ssh/sshd_config   //修改sshd服务配置文件
PubkeyAuthentication yes                          //找到这两项,去掉注释
AuthorizedKeysFile      .ssh/authorized_keys
[root@localhost html]# service sshd reload        //重载服务

  

 在rsync发起端创建ssh密钥对,并将公钥导入到服务器公钥文本     

[root@localhost ~]# ssh-keygen -t rsa         //使用RSA算法生成密钥对
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):   
Enter passphrase (empty for no passphrase): 
Enter same passphrase again:                 //按三下Enter键,实现无口令登录
[root@localhost ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@192.168.175.129    //将公钥文件上传到rsync同步源
The authenticity of host '192.168.175.129 (192.168.175.129)' can't be established.
RSA key fingerprint is e5:c6:c7:96:30:eb:04:00:da:e9:43:72:67:d7:ff:e3.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.175.129' (RSA) to the list of known hosts.
root@192.168.175.129's password:                                             //输入同步源用户密码
Now try logging into the machine, with "ssh 'root@192.168.175.129'", and check in:
  .ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting
[root@localhost ~]# ssh root@192.168.175.129            //成功登录sync同步源
[root@localhost ~]# ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 00:0C:29:1C:B4:FB  
          inet addr:192.168.175.129  Bcast:192.168.175.255  Mask:255.255.255.0

  

退回到128服务器,编写触发式同步脚本

这个脚本是有问题的(file列表是循环形式触发rsync ,等于有10个文件发生更改,就触发10次rsync全量同步(简直就是噩梦),那还不如直接写个死循环的rsync全量同步得了)

[root@localhost ~]# vim rsync.sh               //编写脚本,若进程已经存在,则忽略本次备份,避免并发执行rsync备份
#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e modify,create,attrib,move,delete /var/www/html/"
RSYNC_CMD="rsync -azH --delete /var/www/html/ root@192.168.175.129:/var/www/html"
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
do
    if [ $(pgrep rsync | wc -l) -le 0 ] ; then
        $RSYNC_CMD
    fi
done
[root@localhost ~]# chmod +x rsync.sh           //添加执行权限,并且服务器开机自动运行此脚本    
[root@localhost ~]# echo '/root/rsync.sh' >> /etc/rc.local

6. 验证脚本是否正确

(1)128f服务器执行脚本,

(2)并新开一个连接到128服务器上,切换到本机/var/www/html目录,执行增、删、改等操作,

(3)查看129服务器下/var/www/html 文件及文件是否有了变化

原文地址:https://www.cnblogs.com/achengmu/p/9459620.html