服務器文件之間的同步常用三種方式:
- 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 -x t=$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` fi while (( $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 $timeout done
第二種: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/bash set -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