两台linux 服务器同步

准备:

主服务器 192.168.0.1

备份服务器 192.168.0.2

备份服务器 注意需要开放873端口 而且小心selinux

开始:

sudo vim /etc/rsyncd.passwd

rsync_user:123456  注意格式,每行一个用户名:密码

sudo chmod 600 /etc/rsyncd.passwd

sudo vim /etc/rsyncd.conf

# /etc/rsyncd: configuration file for rsync daemon mode

uid = root
gid = root
use chroot = no
munge symlinks = no
max connections = 10
strict modes = yes
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log

ignore errors
read only = no
write only = no
hosts allow = 192.168.0.1
hosts deny = *
list = false
auth users = rsync_user
secrets file = /etc/rsyncd.passwd

[web]
path = /var/www/
comment = web

[cron]
path = /var/spool/cron/
comment = cron

注意 [web] [cron] 是不同的目录,如果有其他的可以继续添加,如果对某个目录下有特殊要求可以在它下面重复配置上面的内容

加入自启动

sudo vim /etc/rc.local

/usr/bin/rsync --daemon

并且手动执行下

sudo /usr/bin/rsync --daemon

 

主服务器

sudo yum install inotify-tools

sudo vim /etc/rsync.passwd

123456  注意和备份服务器格式不同,这里仅仅需要密码即可,我在这浪费了几个小时折腾

sudo chmod 600 /etc/rsync.passwd

sudo vim /usr/bin/rsync.sh

#!/bin/bash

host=192.168.0.2
user=rsync_user
rsync_passwd_file=/etc/rsync.passwd

src=/data/vhosts/
des=web

rsync -avz --password-file=${rsync_passwd_file} ${src} ${user}@${host}::${des} >> /var/log/rsync.log 2>&1

cd $src

/usr/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%Xe %w%f' -e delete,attrib,close_write ./ | while read file
do
  INO_EVENT=$(echo $file | awk '{print $1}')
  INO_FILE=$(echo $file | awk '{print $2}')
  if [[ $INO_EVENT =~ 'CLOSE_WRITE' ]]
  then
    rsync -avzR --password-file=${rsync_passwd_file} $(dirname ${INO_FILE}) ${user}@${host}::${des} >> /var/log/rsync.log 2>&1
  fi

  if [[ $INO_EVENT =~ 'DELETE' ]]
  then
    rsync -avzR --delete --password-file=${rsync_passwd_file} $(dirname ${INO_FILE}) ${user}@${host}::${des} >> /var/log/rsync.log 2>&1
  fi

  if [[ $INO_EVENT =~ 'ATTRIB' ]]
  then
  if [ ! -d "$INO_FILE" ]
  then
    rsync -avzR --password-file=${rsync_passwd_file} $(dirname ${INO_FILE}) ${user}@${host}::${des} >> /var/log/rsync.log 2>&1
  fi
fi
done

sudo chmod +x /usr/bin/rsync.sh

添加自启动

sudo vim /etc/rc.local

/usr/bin/rsync.sh &

并且手动启动下

sudo /usr/bin/rsync.sh &

结束

 

如果出现
Failed to watch /var/www/; upper limit on inotify watches reached!
Please increase the amount of inotify watches allowed per user via `/proc/sys/fs/inotify/max_user_watches'.

sudo sysctl -w fs.inotify.max_user_watches="99999999"
sudo vim /etc/sysctl.conf
添加
fs.inotify.max_user_watches=99999999

posted @ 2017-10-12 23:43  Blon  阅读(505)  评论(0编辑  收藏  举报