日常開發使用的筆記本鍵盤又一次壞了,需要送修幾天,需要將這臺電腦上所有資料代碼備份到家裏臺式機,因爲這已經不是第一次這麼幹了,前幾次都是拿移動硬盤轉移數據,很是麻煩並且還不一定什麼數據都能簡單打包,而且特慢,終於在這次忍不住想到了有沒有什麼辦法做到自動備份同步,找了一下還真有,而且人家是用在數據運維過程中的數據備份,實時性很高,以下花了一晚上時間做了一些嘗試,雖然最後沒有用到實時備份同步的功能但是也進行了嘗試,留作他日使用。
本地文件簡單測試
luoyang@pavilion:~$ mkdir testdir
luoyang@pavilion:~$ rsync -av NewQotus testdir #會在testdir目錄下新建一個NewQotus目錄並備份
luoyang@pavilion:~$ rsync -av NewQotus/ testdir/ #不會建立NewQotus目錄
使用實時同步測試
編譯安裝完 inotify 後 /home/luoyang/App/shell/rsync.sh
文件如下:
#!/bin/bash
host=192.168.1.119
src=/home/luoyang/test
des=test
user=qotone
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src | while read files
do
/usr/bin/rsync -vzrtopg --progress --delete --password-file=/home/luoyang/rsync.pw $src $user@$host::$des
echo "${files} was rsynced" >>/home/luoyang/rsync.log 2>&1
done
服務器端配置文件(/etc/rsyncd.conf):
uid = root
gid = root
user chroot = no
max connections = 200
timeout = 600
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
log file = /var/run/rsyncd.log
[Workspace]
path = /home/luoyang/Workspace
ignore errors
read only = no
list = no
hosts allow = 192.168.1.5/255.255.255.0
auth users = qotone
secrets file = /etc/rsyncd.password
[DATA]
path = /home/luoyang/Documents/DATA
ignore errors
read only = no
list = no
hosts allow = 192.168.1.5/255.255.255.0
auth users = qotone
--exclude = Virtual_Machines BaiduNetdisk
secrets file = /etc/rsyncd.password
[nfsroot]
path = /home/luoyang/nfsroot
ignore errors
read only = no
list = no
hosts allow = 192.168.1.5/255.255.255.0
auth users = qotone
secrets file = /etc/rsyncd.password
[notes]
path = /home/luoyang/Documents/notes
ignore errors
read only = no
list = no
hosts allow = 192.168.1.5/255.255.255.0
auth users = qotone
secrets file = /etc/rsyncd.password
[Sources]
path = /home/luoyang/Source
ignore errors
read only = no
list = no
hosts allow = 192.168.1.5/255.255.255.0
auth users = qotone
secrets file = /etc/rsyncd.password
密碼文件:
touch /etc/rsyncd.password
echo "qotone:qotone" > /etc/rsyncd.password
往服務器上傳文檔:
rsync -avz --delete --progress ./test/ [email protected]::test
從服務器下載文檔:
rsync -avz --delete [email protected]::test ./test --password-file=/home/luoyang/rsync.pw
rsync 命令參數詳解
rsync 常用參數:
-a, --archive 歸檔模式,等於-rlptgoD; #最常用的 一般直接 -avzP
-r, --recursive 遞歸模式;
-l 保持符號連接;
-p 保持文件原有權限;
-t 保持文件原有修改時間;
-g 保持文件原有用戶組;
-o 保持文件原有屬主;
-D -devices -specials,保持塊設備文件和特殊文件;
-I 強制掃描,放棄“quick check”策略(quick check策略,通過查看文件的時間戳和文件大小,先排除一批認爲相同的文件[rsync不會對文件夾本身做“quick check”]);
-z, --compress 壓縮,默認的壓縮算法和gzip一樣;
-v, --verbose 顯示進度詳細信息;
-q, --quiet 精簡輸出模式;
-R, --relative 保持全路徑;
–progress 顯示傳輸進度(百分百); 等同於 -P
–delete 本地刪除某一文件,遠程也同步的刪除該文件,保持嚴格的一致(不加該參數,則表示只增不減);
–exclude="*.tmp" 排除某些文件;
–exclude-from=FILE 排除FILE中指定模式的文件;
–port=PORT 指定其他的rsync服務端口;
實際操作
rsync -avz --progress /home/luoyang/notes/ [email protected]::notes
rsync -avz --progress /home/luoyang/Source [email protected]::Sources
rsync -avz --progress /home/luoyang/nfsroot/ [email protected]::nfsroot
rsync -avz --progress /media/luoyang/DATA/ [email protected]::DATA
rsync -avz --progress /media/luoyang/Workspace/ [email protected]::Workspace
rsync -avz --delete --progress /media/luoyang/Workspace/stm32/ [email protected]::stm32 # --delete 會刪除掉服務器上不同的文件