rsync



Document Actions 最新變更在 2 年 以前 作者 patt

作者:Patt Hsieh (patt@ossacc.org)
Song Huang (song@ossacc.org)

聲明:本文可自由取用,但不可轉載用於商業用途

非營利轉載請注明來源

簡介:rsync 本來是發展來取代 rcp 的一個工具, 目前是由 rsync.samba.org 維護。
rsync 使用 rsync 演算法 有興趣可以研究一下。
環境說明:rsync 具有很高的使用彈性,可以把client端的內容備份到rsync server上;也可以從client端抓取rsync server上需要備份的內容。亦即:需要被備份的主機可以是rsync server,也可以是rsync client。兩者做法略有差異。本篇因被備份的主機上有mail server,可以在備份完之後mail給相關人員,所以選擇將rsync server架設?在遠端機器上,在需備份的主機上run rsync client 指令,並將過程轉輸出到一個檔案。
rsync client:
OS: Debian Linux
rsync version: 2.5.6
這是需要備份的主機。
rsync server:
OS: OpenBSD 3.4 Release
rsync version: 2.5.6
把東西備份到此一機器。ip: xxx.xxx.xxx.xxx
安裝:
從主站抓source自行compile 或 binary檔安裝。
Debian: 用apt-get 安裝 # apt-get install rsync
OpenBSD: 用ports安裝 # cd /usr/ports/net/rsync; make install
Server端設定:規劃建立備份目錄區: 建議準備一個容量較大且獨立的分割區....當然,只是建議!
rsyncd.conf:
# GLOBAL OPTIONS
log file=/var/log/rsyncd.log

# MODULE OPTIONS
[web] # module name
comment = WEB # 說明
path = /home/rmtbak/web_var # 備份的內容存放之路徑
auth users = root # 允許root登入(若沒有auth users此option,則
# anonymous可login)
uid = nobody # 以uid nobody啟動rsync server
gid = nogroup # 以gid nogroup啟動rsync server
use chroot = no # the rsync server will chroot to the "path"
# before starting the file transfer with the
# client.
secrets file = /etc/rsyncd.pw # 代表儲存帳號密碼的密碼檔, 其放置的路徑檔名.
read
dont compress = *.gz *.tgz *.zip *.z *.rpm *.deb *.iso *.bz2 *.tbz
#不要對有以上列出附檔名之檔案做壓縮。
.
.
.
下略,如有其他來源需做備份,再另行增加module即可。
設定開機啟動: 編輯/etc/inetd.conf加上這一行:
rsync stream tcp nowait root /usr/local/bin/rsync rsync --daemon
立即生效: # kill -1 `cat /var/run/inetd.pid`
secrets file密碼檔的設定:
內容: id:password:
YourID:YourPassword <==請別套用,自定一組!謝謝!
ownership: root
permission: 600
大致上就這樣。
Client端設定之其一(rsync):rsync usage:
rsync [OPTION]... SRC [SRC]... [USER@]HOST::DEST

其他用法,請man rsync
先將要做的工作寫成script檔,內容如下:
#!/bin/sh
/bin/cat /dev/null > /tmp/rsync.log #清空log

/usr/bin/rsync -arvlHpogDtS --delete -e ssh/n
--password-file=/etc/rsyncd.secrets/n
/home/web/zope/var/ root@xxx.xxx.xxx.xxx::web >/tmp/rsync.log

/usr/bin/rsync -arvlHpogDtS --delete -e ssh/n
--password-file=/etc/rsyncd.secrets/n
/home/edu/zope/var/ root@xxx.xxx.xxx.xxx::edu >>/tmp/rsync.log

/usr/bin/rsync -arvlHpogDtS --delete -e ssh/n
--password-file=/etc/rsyncd.secrets/n
/etc/ root@xxx.xxx.xxx.xxx::etc >> /tmp/rsync.log

/usr/bin/rsync -arvlHpogDtS --delete -e ssh/n
--password-file=/etc/rsyncd.secrets/n
/var/lib/mysql/ root@xxx.xxx.xxx.xxx::mysql >>/tmp/rsync.log

(
echo "To: root"
echo "From: rsync<rsync>"
echo "Subject: rsync backup log"
echo
date
cat /tmp/rsync.log
)2>&1 |/usr/lib/sendmail -t
exit 0
參數說明:
-arvlHpogDtS:
-a archive mode
-r recurse into directories
-v verbose
-l copy symlinks as symlinks
-H preserve hard links 保存hardlink
-p preserve permissions 保留原來的權限
-o preserve owner (root only)保留原來的ownership
-g preserve group
-D preserve devices (root only)
-t preserve times
-S handle sparse files efficiently 提高處理稀疏檔案的效率
--delete #如果source端刪除了一文件,那destination端也相應把這一文件刪除,保持真正的一致。
-e ssh 使用ssh做加密傳輸
--password-file=/etc/rsyncd.secrets #提供連結rsync server所用帳號的密碼。該注意的有兩點:
此檔案權?限不應為world readable。請設為600,並且把owner設為root。
內容只需包含密碼,不需含id;每行一個密碼。
密碼需跟rsync server的secrets file裡的密碼一致!
/home/web/zope/var/ #需要做備份的目錄。
root@xxx.xxx.xxx.xxx::web #以root身分登入到rsync server上的"web" module。
小括弧之後為將執行期間之verbose輸出以mail通知root!
記得將屬性改為可執行!
Client端設定之其二(ssh):說明: 因為使用ssh連結至ssh server時需要輸入ID & Password做認證,對於自動化的工作會有妨害,因此使用 Trusted SSH Authorized。
實作:
在rsync client端(在這裡指的是需要備份的主機 Debian Linux)以root身分執行:
# ssh-keygen -d <---產生DSA key pair
Generating public/private dsa key pair.
Enter file in which to save the key (/root/.ssh/id_dsa):
Enter passphrase (empty for no passphrase): <-- 不打passphrase,就不會詢問
password
Enter same passphrase again: <---同樣直接按下enter
Your identification has been saved in /root/.ssh/id_dsa.
Your public key has been saved in /root/.ssh/id_dsa.pub.
The key fingerprint is:
下略..
把/root/.ssh/id_dsa.pub 放到rsync server上/root/.ssh/並改名為authorized_keys2:
scp ~/.ssh/id_dsa.pub xxx.xxx.xxx.xxx:~/.ssh/authorized_keys2
注意: 指令中root@xxx.xxx.xxx::web 的root@ 不但是rsync的登入身分,也是ssh的認證ID。在server端rsyncd.conf裡指定的身分必須跟ssh登入的身分一致。但secrets file跟client端的password-file裡的密碼不需跟系統密碼一樣,而且最好是不一樣的!
Client端設定之其三(cron): 用指令"# crontab -e"編輯crontab,加入:
33 04 * * * /The/Path/of/Your/Script
排定每天清晨4點33分做備份!
後記:參考文件:
使用 rsync (全自動網路備份法) by OLS3
rsync tutorial
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值