实时同步rsync+inotify

原创博文http://www.cnblogs.com/elvi/p/7658071.html

#linux同步
#实时同步rsync+inotify,双向同步inotify+unison
#centos6 #关闭selinux、防火墙
chkconfig iptables off
chkconfig ip6tables off
/etc/init.d/iptables stop
/etc/init.d/ip6tables stop
sed -i '/^SELINUX=.*/c SELINUX=disabled' /etc/selinux/config
sed -i '/^SELINUX=/ s/^SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config
grep --color=auto '^SELINUX' /etc/selinux/config #查看
setenforce # 使配置立即生效
vim /etc/hosts #添加hostname名称
reboot #最好重启
#更换阿里源
yum -y install wget vim
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
#安装epel包
yum -y install http://mirrors.ustc.edu.cn/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm
yum makecache #生成缓存 ##安装
yum install -y rsync #server1 IP 172.16.11.31
mkdir -p /bak/sys1
cd /bak/sys1
echo test >$(date +%Y-%m-%d-%H%M%S).txt
#server2 IP 172.16.11.32
mkdir -p /bak/sys2
cd /bak/sys2 #ssh同步源
rsync -avz root@172.16.11.31:/bak/sys1/* /bak/sys2
#rsync -avz /bak/sys2 root@172.16.11.31:/bak/sys1/* #rsync同步源配置
vim /etc/rsyncd.conf
#
uid = nobody
gid = nobody
user chroot = no
max connections = 200
timeout = 600
port = 873
pid file = /var/run/rsyncd.pid
#lock file = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log
[sys1]
comment= sys1 test
path=/bak/sys1
ignore errors
read only = no
list = no
#hosts allow = 192.168.0.0/24
auth users = test
secrets file = /etc/rsyncd.db
echo test:test>/etc/rsyncd.db
chown root:root /etc/rsyncd.db
chmod 600 /etc/rsyncd.db yum -y install xinetd
vim /etc/xinetd.d/rsync
# disable = yes 改成 disable = no
service xinetd restart #启动独立进程运行
pkill rsync
/usr/bin/rsync --daemon
#client
/usr/bin/rsync -avz test@172.16.11.31::sys1 /bak/sys2
#若需要写入权限,需要设置目录
setfacl -m u:nobody:rwx /bak/sys1/ ###免密码验证
#ssh秘钥
ssh-keygen -t rsa -P ''
ssh-copy-id root@172.16.11.31
ssh 172.16.11.31
rsync -avz root@172.16.11.31:/bak/sys1/* /bak/sys2
#rsync的同步源,设置变量export RSUNC_PASSWORD=test
#设置密码文件
echo test> /etc/rsyncd.password
chmod 600 /etc/rsyncd.password
/usr/bin/rsync -avz test@172.16.11.31::sys1 /bak/sys2 --password-file=/etc/rsyncd.password ###实时同步
#安装inotify-tools
yum install make gcc gcc-c++ #安装编译工具
cd /usr/local/src
wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
tar zxvf inotify-tools-3.14.tar.gz #解压
cd inotify-tools-3.14 #进入解压目录
./configure && make && make install #编译 #安装
#目录测试
inotifywait -mrq -e create,delete /bak/sys1/ #修改inotify默认参数
sysctl -w fs.inotify.max_queued_events="99999999"
sysctl -w fs.inotify.max_user_watches="99999999"
sysctl -w fs.inotify.max_user_instances="65535"
vim /etc/sysctl.conf #添加以下代码
fs.inotify.max_queued_events=99999999
fs.inotify.max_user_watches=99999999
fs.inotify.max_user_instances=65535 #创建实时监控脚本
vim /bak/inotify_rsync.sh
#!/bin/sh
SRC=/bak/sys1/
DST=root@172.16.11.32:/bak/sys2/
#/bin/su - rsync
/usr/local/bin/inotifywait -mrq -e modify,delete,create,attrib ${SRC} | while read D E F
do
/usr/bin/rsync -ahqzt --delete $SRC $DST
done
#
chmod +x /bak/inotify_rsync.sh
/bak/inotify_rsync.sh &
#echo "/bak/inotify_rsync.sh &" >> /etc/rc.local #开机自启动 #双向同步inotify+unison
yum install ocaml ocaml-camlp4-devel ctags ctags-etags -y
wget http://www.seas.upenn.edu/~bcpierce/unison//download/releases/stable/unison-2.48.4.tar.gz
tar -zxvf unison*
cd src
make UISTYLE=text THREADS=true STATIC=true
make install
cp unison /usr/local/bin
#cp unison /usr/bin
#创建实时监控脚本
vim /bak/unison.sh
#!/bin/sh
SRC=/bak/sys1/
DST=ssh://172.16.11.32//bak/sys2/
/usr/local/bin/inotifywait -mrq -e modify,delete,create,attrib ${SRC} | while read D E F
do
/usr/local/bin/unison -batch $SRC $DST
done
#
chmod +x /bak/unison.sh
/bak/unison.sh &
#nohup /bak/unison.sh &
#server2也配置运行
vim /bak/unison.sh
#!/bin/sh
SRC=/bak/sys2/
DST=ssh://172.16.11.31//bak/sys1/
/usr/local/bin/inotifywait -mrq -e modify,delete,create,attrib ${SRC} | while read D E F
do
/usr/local/bin/unison -batch $SRC $DST
done
#
mkdir /bak/log
nohup /bak/unison.sh &>>/bak/log/unison.log &
#开机自启动(vim /etc/rc.local无效)
crontab -e
* * * * * /bak/unison.sh >>/bak/log/unison.log 2>&1 &
#这样会造成很多进程
##
echo "nohup /bak/unison.sh >>/bak/log/unison.log 2>&1 &" >> /etc/rc.local
#vim /etc/rc.local 测试无效
chmod +x /etc/rc.d/rc.local
echo $(date) >>11.txt #关闭进程
pkill unison.sh
pkill inotifywait

实时同步rsync+inotify的更多相关文章

  1. 项目cobbler+lamp+vsftp+nfs+数据实时同步(inotify+rsync)

    先配置好epel源 [root@node3 ~]#yum install epel-release -y 关闭防火墙和selinux [root@node3 ~]#iptables -F [root@ ...

  2. 数据文件实时同步(rsync + sersync2)

    因近期项目需求,需要同步云端服务器的数据给**方做大数据分析. 思路: 起初只要数据同步,准备开放数据采集接口.但实时性较差,会有延迟. 故而寻觅各种解决方案,最终确定使用 rsync 进行文件同步, ...

  3. 文件触发式实时同步 Rsync+Sersync Rsync+Inotify-tools

    一.概述 1.Rsync+Sersync 是什么? 1)Sersync使用c++编写基于inotify开发的触发机制: 2)Sersync可以监控所监听的目录发生的变化(包括新建.修改.删除),具体到 ...

  4. rsync+inotify实时同步环境部署记录

    随着应用系统规模的不断扩大,对数据的安全性和可靠性也提出的更好的要求,rsync在高端业务系统中也逐渐暴露出了很多不足.首先,rsync在同步数据时,需要扫描所有文件后进行比对,进行差量传输.如果文件 ...

  5. rsync+inotify实时同步方案

    rsync+inotify实时同步,inotify可以实时监控本地文件或目录变化,当检测到本地文件变化,执行rsync同步命令,将变化的文件同步到其他服务器节点. 1.配置环境 3.在服务节点1.服务 ...

  6. rsync简介与rsync+inotify配置实时同步数据

    rsync简介 rsync是linux系统下的数据镜像备份工具.使用快速增量备份工具Remote Sync可以远程同步,支持本地复制,或者与其他SSH.rsync主机同步. rsync特性 rsync ...

  7. (转)Linux下通过rsync与inotify(异步文件系统事件监控机制)实现文件实时同步

    Linux下通过rsync与inotify(异步文件系统事件监控机制)实现文件实时同步原文:http://www.summerspacestation.com/linux%E4%B8%8B%E9%80 ...

  8. inotify与rsync实现实时同步记录文档

    目录 安装 配置 参考链接 安装 安装rsync yum -y install rsync 安装inotify-tools 这是一个实时监听文件变换的工具 wget -O /etc/yum.repos ...

  9. Rsync+inotify自动同步数据

    一.简介 随着应用系统规模的不断扩大,对数据的安全性和可靠性也提出的更好的要求,rsync在高端业务系统中也逐渐暴露出了很多不足. 首先,rsync在同步数据时,需要扫描所有文件后进行比对,进行差量传 ...

随机推荐

  1. spring mvc+mybatis+maven集成tkmapper+pagehelper

    <!-- maven tkmapper引入--> <dependency> <groupId>tk.mybatis</groupId> <arti ...

  2. DataTable数据修改,换列

    增加列             DataTable table= new DataTable();             table.Columns.Add("ID", type ...

  3. C++11 static_assert

    C++11 static_assert C++0x中引入了static_assert这个关键字,用来做编译期间的断言,因此叫做静态断言. 其语法:static_assert(常量表达式,提示字符串). ...

  4. Java:求字符串中邻接的数字为一个整体

    public static void main(String[] args) { String strNumbers = "0123456789";//用来进行判断数字的 Syst ...

  5. Ajax 跨域 异步 CORS

    HTTP access control (CORS) 核心在于使用定制(添加新的header)HTTP header让浏览器和服务器有更多的相互了解,从而决定一个请求或者响应成功还是失败   对于一个 ...

  6. Python爬虫入门:Cookie的使用

    大家好哈,上一节我们研究了一下爬虫的异常处理问题,那么接下来我们一起来看一下Cookie的使用. 为什么要使用Cookie呢? Cookie,指某些网站为了辨别用户身份.进行session跟踪而储存在 ...

  7. 使用 Go-Ethereum 1.7.2搭建以太坊私有链

    目录 [toc] 1.什么是Ethereum(以太坊) 以太坊(Ethereum)并不是一个机构,而是一款能够在区块链上实现智能合约.开源的底层系统,以太坊从诞生到2017年5月,短短3年半时间,全球 ...

  8. mysql查询锁表及解锁

    SHOW PROCESSLIST; KILL ; 锁表网上解释: 这牵涉到mysql的事务,简单通俗的话,就这样给你解释有一个任务序列控制sql语句的执行,第一次有select的语句查询表a,mysq ...

  9. 如何实现border-width:0.5px;

    工作中遇到了一个产品需求,要求把列表分割线改成0.5px,直接写成border:0.5px solid #cccccc;是不符合规范的写法,会存在Android和IOS手机上的兼容问题,故,我们可以利 ...

  10. C#中的静态成员和非静态成员

    C#的类中可以包含两种方法:C#静态方法与非静态方法.那么他们的定义有什么不同呢?他们在使用上会有什么不同呢? 让我们来看看最直观的差别:使用了static 修饰符的方法为静态方法,反之则是非静态方法 ...