码迷,mamicode.com
首页 > 其他好文 > 详细

实时同步rsync+inotify

时间:2017-10-23 18:33:20      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:dir   config   chm   git   exp   tar   开机自启动   同步   默认参数   

 

实时同步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 0 # 使配置立即生效
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  测试无效
echo $(date) >>11.txt

#关闭进程
pkill unison.sh
pkill inotifywait

 

实时同步rsync+inotify

标签:dir   config   chm   git   exp   tar   开机自启动   同步   默认参数   

原文地址:http://www.cnblogs.com/elvi/p/7658071.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!