Ubuntu rsync同步文件實例

 服務器端:Ubuntu 9.10 - 192.168.1.3
客戶端:Ubuntu 10.04 - 192.168.1.73

我們先來設置一下服務器端的配置

1.ubuntu系統安裝完之後,rsync服務默認不是啓動的,我們要修改下面的文件。
$sudo vi /etc/default/rsync
RSYNC_ENABLE=true


2.修改配置文件
$sudo cp /usr/share/doc/rsync/examples/rsyncd.conf /etc
我們先來查看一下這個文件
$sudo cat /etc/rsyncd.conf

# sample rsyncd.conf configuration file

# GLOBAL OPTIONS

#motd file=/etc/motd #登錄歡迎信息
#log file=/var/log/rsyncd #日誌文件
# for pid file, do not use /var/run/rsync.pid if
# you are going to run rsync out of the init.d script.
pid file=/var/run/rsyncd.pid

#指定rsync發送日誌消息給syslog時的消息級別,常見的消息級別是:uth, authpriv, cron, daemon, ftp, kern, lpr, mail, news, security, sys-log, user, uucp, local0, local1, local2, local3,local4, local5, local6和local7。默認值是daemon。 
#syslog facility=daemon

#自定義tcp選項,默認是關閉的
#socket options=

#以下是模塊信息,我們可以創建多個模塊
# MODULE OPTIONS

[ftp]

        comment = public archive #模塊描述
        path = /var/www/pub #需要同步的路徑
        use chroot = yes #默認是yes|true,如果爲true,那麼在rsync在傳輸文件以前首先chroot到path參數指定的目錄下。這樣做的原因是實現額外的安全防護,但是缺點是需要root權限,並且不能備份指向外部的符號連接指向的目錄文件。
#       max connections=10 #最大連接數
        lock file = /var/lock/rsyncd #指定支持max connections參數的鎖文件。
# the default for read only is yes...
        read only = yes #只讀選項
        list = yes #客戶請求時可用模塊時是否列出該模塊
        uid = nobody #設定該模塊傳輸文件時守護進程應該具有的uid
        gid = nogroup #設定該模塊傳輸文件時守護進程應具有的gid,此項與uid配合可以確定文件的訪問權限
#       exclude = #用來指定多個由空格隔開的多個模式列表,並將其添加到exclude列表中。這等同於在客戶端命令中使用--exclude來指定模式,不過配置文件中指定的exlude模式不會傳遞給客戶端,而僅僅應用於服務器。一個模塊只能指定一個exlude選項,但是可以在模式前面使用"-"和"+"來指定是exclude還是include
#       exclude from = #可以指定一個包含exclude模式定義的文件名
#       include = #與exclude相似
#       include from = #可以指定一個包含include模式定義的文件名
#       auth users = #該選項指定由空格或逗號分隔的用戶名列表,只有這些用戶才允許連接該模塊。這裏的用戶和系統用戶沒有任何關係。如果"auth users"被設置,那麼客戶端發出對該模塊的連接請求以後會被rsync請求challenged進行驗證身份這裏使用的 challenge/response認證協議。用戶的名和密碼以明文方式存放在"secrets file"選項指定的文件中。默認情況下無需密碼就可以連接模塊(也就是匿名方式)
#       secrets file = /etc/rsyncd.secrets #該文件每行包含一個username:password對,以明文方式存儲,只有在auth users被定義時,此選項才生效。同時我們需要將此文件權限設置爲0600
        strict modes = yes #該選項指定是否監測密碼文件的權限,如果該選項值爲true那麼密碼文件只能被rsync服務器運行身份的用戶訪問,其他任何用戶不可以訪問該文件。默認值爲true
#       hosts allow = #允許的主機
#       hosts deny = #拒絕訪問的主機
        ignore errors = no #設定rsync服務器在運行delete操作時是否忽略I/O錯誤
        ignore nonreadable = yes #設定rysnc服務器忽略那些沒有訪問文件權限的用戶
        transfer logging = no #使rsync服務器使用ftp格式的文件來記錄下載和上載操作在自己單獨的日誌中
#       log format = %t: host %h (%a) %o %f (%l bytes). Total %b bytes. #設定日誌格式
        timeout = 600 #超時設置(秒)
        refuse options = checksum dry-run #定義一些不允許客戶對該模塊使用的命令選項列表
        dont compress = *.gz *.tgz *.zip *.z *.rpm *.deb *.iso *.bz2 *.tbz #告訴rysnc那些文件在傳輸前不用壓縮,默認已設定壓縮包不再進行壓縮

        

日誌格式選項列表:
%h:遠程主機名
%a:遠程IP地址
%l:文件長度字符數
%p:該次rsync會話的進程id
%o:操作類型:"send"或"recv"、”del.”
%f:文件名
%P:模塊路徑
%m:模塊名
%t:當前時間
%u:認證的用戶名(匿名時是null)
%b:實際傳輸的字節數
%c:當發送文件時,該字段記錄該文件的校驗碼


下面我們來定義自己的conf文件

# [GLOBAL OPTIONS]

#motd file=/etc/motd
log file=/var/log/rsyncd
# for pid file, do not use /var/run/rsync.pid if
# you are going to run rsync out of the init.d script.
pid file=/var/run/rsyncd.pid
syslog facility=daemon

#socket options=

# [MODULE OPTIONS]

[serverdb]

        comment = Server Databases
        path = /home/flow/Documents/db_backup
        use chroot = no
#       max connections=10
        lock file= /var/lock/rsyncd
# the default for read only is yes...
        read only = true
        list = true
        uid = nobody
        gid = nogroup
        exclude = db_backup.sh
#       exclude from = 
#       include = 
#       include from = 
        auth users = liubing
        secrets file = /etc/rsyncd.secrets 
        strict modes = true
        hosts allow = 192.168.1.73
#       hosts deny = 
        ignore errors = true
        ignore nonreadable = true
        transfer logging = true
        log format = %t[%p]: host %h (%a) %o %f (%l bytes). Total %b bytes.
        timeout = 600
        refuse options = checksum dry-run
        dont compress = *.gz *.tgz *.zip *.z *.rpm *.deb *.iso *.bz2 *.tbz


創建一個密碼文件
$sudo vi /etc/rsyncd.secrets
liubing:123456
$sudo chmod 0600 /etc/rsyncd.secrets


啓動rsync
sudo /etc/init.d/rsync start


我們再來看一下客戶端的操作,一般客戶端不需要進行特殊的配置,直接同步即可
$rsync -vzrtopg --progress [email protected]::serverdb .
password

成功!

我們把這個同步工作交給crontab去執行。首先我們要創建一個密碼文件
$sudo vi /etc/rsync.pwd輸入123456,保存

!注意:下面這兩步操作是必須的
sudo chmod 0600 /etc/rsync.pwd
sudo chown liubing:liubing /etc/rsync.pwd


然後我們打開crontab,加入以下任務
$crontab -e
* */1 * * * rsync -a --password-file=/etc/rsync.pwd [email protected]::serverdb /home/liubing/Documents/db_backup
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章