rsync在推送或抓取資料時,在rsync用戶端會出現性能問題, 負載比較大且資料量比較大的生産環境要注意這一點。 本文隻是個人平時配置示例,可以簡單修改後用于實際環境使用,rsync不僅可以實作伺服器間的複制,對于本地目錄的同步也相當友善, 其它功能如實時同步等功能待補充。
#!/bin/bash
# author: koumm
# desc: backup for /data/ to 10
# date: 20xx-xx-xx
# version: v1.0
# modify:
#rsync bak
end_date=`date +%y/%m/%d_%h%m%s`
/usr/bin/rsync -vzrtopg --delete /data/ 192.168.200.10::ip6/ --exclude "tmp/" --port=1865
echo ""
echo "############################################"
echo "rsync completed time is :$end_date"
echo "############################################"
# crontab -e
40 12,23 * * * /root/system/rsync_bak_to_10.sh
# vi /etc/rsyncd.conf
uid=root
gid=root
max connections=36000
use chroot=no
log file=/var/log/rsyncd.log
pid file=/var/run/rsyncd.pid
lock file=/var/run/rsyncd.lock
[ip6]
path=/data/project_bak/ip6
comment = ip6
ignore errors
read only = no
write only = no
list = no
hosts allow = 192.168.200.0/24
#auth users = www
#secrets file = /etc/rsyncd.secrets
服務端配置開機自啟動,以及手動執行啟動指令。
# vi /etc/rc.local
/usr/bin/rsync --daemon --port=1865
重新開機rsync服務采用的殺程序方式。先ps –ef |grep rsync 找到程序号,再kill –9 xxxx殺之,再啟動。
用戶端: cwrsync_4.1.0_installer.zip
服務端: cwrsyncserver_4.1.0_installer.zip
預設安裝即可,配置檔案如下:
use chroot = false
strict modes = false
hosts allow = *
log file = rsyncd.log
gid = 0
uid = 0
[gzbak]
path = /cygdrive/d/data
read only = false
transfer logging = yes
修改windows服務,将rsyncserver配置為啟動,自動啟動。
rsync.bat
@echo off
cls
"c:\program files\cwrsync\bin\rsync.exe" -avr --delete /cygdrive/c/opt 192.168.233.14::gzbak/ --port=1865
# rsync -avz -e ssh 192.168.0.25:/data/ /data01/
[email protected]'s password: 輸入密碼
receiving incremental file list
./
a.txt
abc.txt
注:如果兩伺服器之前配置密鑰直接登入的話,可以直接傳檔案與scp方式類似。
4. inotify實時同步配置
(1) inotify-tools安裝,隻在用戶端安裝即可。
下載下傳位址: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.13
# ./configure --prefix=/usr/local/inotify
# make && make install
調整inotify隊列參數及參數說明:
max_queued_events: inotify隊列最大長度,如果值太小,會出現"** event queue overflow **"錯誤,導緻監控檔案不準确
max_user_watches: 要同步的檔案包含多少目錄
max_user_instances:每個使用者建立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"
(2) 通過配置檔案準備
# vi inosync.sh
#!/bin/sh
src=/data01/
dst=192.168.1.125::data
inwt=/usr/local/inotify/bin/inotifywait
rsync=/usr/bin/rsync
$inwt -mrq --timefmt '%d/%m/%y %h:%m' --format '%t %w%f' -e create,move,delete,modify,attrib $src \
| while read line
do
$rsync -az --delete /data01/ 192.168.1.125::data
done
# chmod +x inosync.sh
(3) 手動運作測試
運作的時候日志會自動輸出到終端,可以觀察運作的情況。
# /usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %h:%m' --format '%t %w%f' -e modify,delete,create,attrib /data01 &
說明:
--timefmt: 指定輸出時的輸出格式
--format : '%t %w%f'指定輸出的格式
(4) 啟動,或将如下啟動腳本加入/etc/rc.local中。
# ./inosync.sh &
[1] 23254
說明:inotify有一個問題就是如果檔案資料量比較大,或同步的檔案比較大,或變更比較頻繁,效率不高。
rsync參數:
-v, --verbose 詳細模式輸出
-q, --quiet 精簡輸出模式
-c, --checksum 打開校驗開關,強制對檔案傳輸進行校驗
-a, --archive 歸檔模式,表示以遞歸方式傳輸檔案,并保持所有檔案屬性,等于-rlptgod
-r, --recursive 對子目錄以遞歸模式處理
-r, --relative 使用相對路徑資訊
-b, --backup 建立備份,也就是對于目的已經存在有同樣的檔案名時,将老的檔案重新命名為~filename。可以使用--suffix選項來指定不同的備份檔案字首。
--backup-dir 将備份檔案(如~filename)存放在在目錄下。
-suffix=suffix 定義備份檔案字首
-u, --update 僅僅進行更新,也就是跳過所有已經存在于dst,并且檔案時間晚于要備份的檔案。(不覆寫更新的檔案)
-l, --links 保留軟鍊結
-l, --copy-links 想對待正常檔案一樣處理軟鍊結
--copy-unsafe-links 僅僅拷貝指向src路徑目錄樹以外的鍊結
--safe-links 忽略指向src路徑目錄樹以外的鍊結
-h, --hard-links 保留硬鍊結
-p, --perms 保持檔案權限
-o, --owner 保持檔案屬主資訊
-g, --group 保持檔案屬組資訊
-d, --devices 保持裝置檔案資訊
-t, --times 保持檔案時間資訊
-s, --sparse 對稀疏檔案進行特殊處理以節省dst的空間
-n, --dry-run 現實哪些檔案将被傳輸
-w, --whole-file 拷貝檔案,不進行增量檢測
-x, --one-file-system 不要跨越檔案系統邊界
-b, --block-size=size 檢驗算法使用的塊尺寸,預設是700位元組
-e, --rsh=command 指定使用rsh、ssh方式進行資料同步
--rsync-path=path 指定遠端伺服器上的rsync指令所在路徑資訊
-c, --cvs-exclude 使用和cvs一樣的方法自動忽略檔案,用來排除那些不希望傳輸的檔案
--existing 僅僅更新那些已經存在于dst的檔案,而不備份那些新建立的檔案
--delete 删除那些dst中src沒有的檔案
--delete-excluded 同樣删除接收端那些被該選項指定排除的檔案
--delete-after 傳輸結束以後再删除
--ignore-errors 及時出現io錯誤也進行删除
--max-delete=num 最多删除num個檔案
--partial 保留那些因故沒有完全傳輸的檔案,以是加快随後的再次傳輸
--force 強制删除目錄,即使不為空
--numeric-ids 不将數字的使用者群組id比對為使用者名群組名
--timeout=time ip逾時時間,機關為秒
-i, --ignore-times 不跳過那些有同樣的時間和長度的檔案
--size-only 當決定是否要備份檔案時,僅僅察看檔案大小而不考慮檔案時間
--modify-window=num 決定檔案是否時間相同時使用的時間戳視窗,預設為0
-t --temp-dir=dir 在dir中建立臨時檔案
--compare-dest=dir 同樣比較dir中的檔案來決定是否需要備份
-p 等同于 --partial
--progress 顯示備份過程
-z, --compress 對備份的檔案在傳輸時進行壓縮處理
--exclude=pattern 指定排除不需要傳輸的檔案模式
--include=pattern 指定不排除而需要傳輸的檔案模式
--exclude-from=file 排除file中指定模式的檔案
--include-from=file 不排除file指定模式比對的檔案
--version 列印版本資訊
--address 綁定到特定的位址
--config=file 指定其他的配置檔案,不使用預設的rsyncd.conf檔案
--port=port 指定其他的rsync服務端口
--blocking-io 對遠端shell使用阻塞io
-stats 給出某些檔案的傳輸狀态
--progress 在傳輸時現實傳輸過程
--log-format=format 指定日志檔案格式
--password-file=file 從file中得到密碼
--bwlimit=kbps 限制i/o帶寬,kbytes per second
-h, --help 顯示幫助資訊