服务器文件之间的同步常用三种方式:
- rsync
- inotify+rsync
- lsyncd
软件下载:https://git.oschina.net/sgfoot/linux-tools
第一种:rsync
核心代码:rsync -az --progress --exclude-from=$exclude_file --delete $local_dir $remote_dir
优点:rsync算法进行增量传输
缺点:每秒都去执行,耗资源
实例代码:可实现分时段,每秒进行同步
#!/bin/bash###############################功能:同步日志#作者:300js#日期:2017/01/05#调用:sync_log.sh [间隔时间 <可选> ] [循环次数 <可选> ]##############################set -xt=$1 #外部间隔时间c=$2 #外部循环次数local_dir="/data/data/" #本地需要复制的目录remote_dir="root@ip:/data/data/" #复制到远程的目录exclude_file="/data/sh/exclude_log.txt" #排除的文件hour_start=9 #开始时间hour_end=23 #结束时间double=100 #多少倍的时间timeout=${t:=3} # 间隔时间count=${c:=18} #循环次数i=1 #自增i########################################################################################hour=`date +%H`hour_int=`expr $hour + 0`if (( $hour_int < $hour_start || $hour_int > $hour_end )) #判断时间段,除9~23工作时间段,为非工作时间,调整间隔时间then timeout=`expr $timeout \* $double`fiwhile (( $i <= $count ))do date=`date +%x%X` rsync -az --progress --exclude-from=$exclude_file --delete $local_dir $remote_dir if [ $? -eq 0 ] then echo "第$i次于$date执行成功,当前进程$$" else echo "第$i次于$date执行失败,当前进程$$" fi let "i++" sleep $timeoutdone 可选> 可选>
第二种:inotify+rsync
核心代码:/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y/%H:%M' --format '%Xe %w%f' -e modify,delete,create,attrib,move /data/
优点:相对rsync,无需每秒每行执行,以监控文件状态进行同步
缺点:若文件有变化,每次都执行一次rsync,浪费资源
需要先安装inotify
#!/bin/bashset -x#########################功能:监控文件的增,删,修#作者:300js#时间:2017/01/09#调用:watch_dir [监控目录] [远程目录root@path] [排除文件] [时间间隔 <可选> ]########################function watch_dir(){ path=$1 #监控目录 remote_dir=$2 #远程目录 exclude_file=$3 #排除文件 timeout=$4 #时间间隔 timeout=${timeout:=2} #默认间隔时间2秒 if [ -z $path ] || [ ! -e $path ] then echo "请输入目录或不存在" exit fi if [ -z $remote_dir ] then echo "远程目录为空,格式root@path" exit fi if [ -z $exclude_file ] || [ ! -e $exclude_file ] then echo "排除文件为空或不存在" exit fi cd $path /usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y/%H:%M' --format '%Xe %w%f' -e modify,delete,create,attrib,move ./ | while read file do #对文件进行判断 if [ -z $file] || [ ! -e $file ] then echo "文件为空或不存在,详细file:$file" continue fi ino_mode=$(echo $file |awk '{print $1}') ino_file=$(echo $file |awk '{print $2}') dir=$(dirname $ino_file) echo $dir >> watch.log echo "当前模式:${ino_mode}" #删除、移动出事件 if [[ $ino_mode =~ 'DELETE' ]] || [[ $ino_mode =~ 'MOVED_FROM' ]] then echo "删除目录:$dir,文件:$ino_file" rsync -avz --exclude-from=$exclude_file --delete $dir $remote_dir else echo "变动目录:$dir,文件:$ino_file" rsync -avz --exclude-from=$exclude_file $dir $remote_dir fi if [ $? -eq 0 ] then echo "远程同步成功" else echo "远程同步失败,错误号:$?" fi sleep $timeout done}path="/data/"remote_dir="root@ip:/data/log/test/" #复制到远程的目录exclude_file="/data/sh/exclude_log.txt" #排除的文件watch_dir $path $remote_dir $exclude_file 可选>
第三种:lsyncd
核心代码:lsyncd -rsyncssh /data/ root@ip /data/
优点:Lysncd 实际上是lua语言封装了 inotify 和 rsync 工具,采用了 Linux 内核(2.6.13 及以后)里的 inotify 触发机制,然后通过rsync去差异同步,达到实时的效果。我认为它最令人称道的特性是,完美解决了 inotify + rsync
海量文件同步带来的文件频繁发送文件列表的问题 —— 通过时间延迟或累计触发事件次数实现。另外,它的配置方式很简单,lua本身就是一种配置语言,可读性非常强。lsyncd也有多种工作模式可以选择,本地目录cp,本地目录rsync,远程目录rsyncssh。
参考:http://seanlook.com/2015/05/06/lsyncd-synchronize-realtime/
安装:环境centos6.5
#yum install lua lua-devel#cd lsyncd#cmake -DCMAKE_INSTALL_PREFIX=/usr/local/lsyncd#make && make install #ln -s /usr/local/lsyncd/bin/lsyncd /usr/sbin/ 或直接:yum -y install lsyncd 安装 启动 调试模式: #lsyncd -nodaemon -rsyncssh /data/ root@ip /data/ 正式模式 #lsyncd -rsyncssh /data/ root@ip /data/
#参考的文档 https://github.com/axkibe/lsyncd/wiki http://www.tuicool.com/articles/VBzmm2 https://code.google.com/archive/p/lsyncd/downloads 注:两台机器或多台机器,保证rsync版本为3.1.x以上,否则会报错:rsync : on remote machine: --delete-missing-args: unknown option