rsync文件同步配置,实现实时同步将A服务器的/home/test/目录下面的所有文件同步到B服务器的/tmp/data/目录下面,并且修改A的文件之后实时同步到B下面,删除B下面的文件A并不受到影响。
要求:
下面对两个脚本的编写
复制代码代码如下:
#!/bin/sh
SRC=/home/test/ #记得在最后面加/不然RYNC会自动增加一层目录 DES=bbsatt IP=192.168.55.34 USER=root #DST=/tmp/data/ #远程rsync模块下的目录 INWT=/usr/bin/inotifywait RSYNC=/usr/bin/rsync $RSYNC -zahqt --password-file=/root/rsync.pwd $SRC $USER@$IP::$DES
之后chmod +x inotify_init.sh
复制代码代码如下:
#!/bin/bash
########################### sync[0]='/home/test/,192.168.55.34,bbsatt,root' INWT=/usr/bin/inotifywait RSYNC=/usr/bin/rsync PASS=/root/rsync.pwd ########################### for item in ${sync[@]}; do dir=`echo $item | awk -F"," '{print $1}'` host=`echo $item | awk -F"," '{print $2}'` module=`echo $item | awk -F"," '{print $3}'` user=`echo $item | awk -F"," '{print $4}'` $INWT -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f %e' --event CLOSE_WRITE,create,move $dir | while read date time file event do #echo $event'-'$file case $event in MODIFY|CREATE|MOVE|MODIFY,ISDIR|CREATE,ISDIR|MODIFY,ISDIR) if [ "${file: -4}" != '4913' ] && [ "${file: -1}" != '~' ]; then cmd="$RSYNC -zahqzt --password-file=$PASS --include=$file $dir $user@$host::$module" echo $cmd >> a.log $cmd fi ;; MOVED_FROM|MOVED_FROM,ISDIR|DELETE,ISDIR) if [ "${file: -4}" != '4913' ] && [ "${file: -1}" != '~' ]; then cmd="$RSYNC -zahqzt --password-file=$PASS --exclude=$file $dir $user@$host::$module" echo $cmd >>a.log $cmd fi ;; esac done & done
设置rsync自动登陆密码验证
之后在B服务器端
修改配置文件
复制代码代码如下:
[root@htdb data]#more /etc/rsyncd.conf
uid = root gid = root use chroot = no max connections = 400 pid file = /var/run/rsyncd.pid lock file = /var/run/rsync.lock log file = /var/log/rsyncd.log [bbsatt] path = /tmp/data/ ignore errors read only = no list = false hosts allow = 192.168.55.15 auth users = root secrets file = /etc/rsync.pas
vi /etc/rsync.pas
复制代码代码如下:
root:xxxx
chmod 600 /etc/rsync.pas
启动RSYNCD
回到主服务器A
复制代码代码如下:
/root/inotify_init.sh
(责任编辑:IT)/root/inotify_monitor.sh |