2012年11月28日星期三

Rsync Notes


Rsync主要是傳送資料差異的部份,因此大多都被使用在差異備份上。除了資料第一次傳輸是整份檔案外,之後都只會傳送資料間異動的部份。
在常駐模式(daemon mode)下,rsync預設監聽TCP埠873,以原生rsync傳輸協定或者透過遠端shell如RSH或者SSH伺服檔案。SSH情況下,rsync用戶端執行程式必須同時在本機和遠端機器上安裝。

Rsync在備份的部份主要是傳送資料差異的部份,因此大多都被使用在差異備份上。除了資料第一次傳輸是整份檔案外,之後都只會傳送資料間異動的部份。
備份相關名詞 :
鏡像備份(mirror backup) : 如同鏡子一般,把資料完整的做一份備份。
增量備份(incremental backup) : 只備份從上次備份後,資料有異動的部份。

Rsync設定
在開始設定之前,有兩個檔案要說明一下:
/etc/rsyncd.conf 為rsync設定檔
/etc/rsyncd.secrets 為rsync 密碼檔

這次在rsync設定部份,直接使用範例的方式會比較快了解。範例 :把A主機的網頁資料( /var/www/html ),定期備份到B主機的( /home/backup/)
A主機( Server端 ) IP :192.168.1.222
B主機( Clinet端 ) IP :192.168.1.122
1. 先設定B主機
1.1 修改 /etc/xinetd.d/rsync 設定
在這邊所設定的rsync是用xinetd來做啟動的。所以如果在rsync並無特別設定的話,
則會依照xinetd.conf所設定的執行。

vim /etc/xinetd.d/rsync  
  1. service rsync 
  2.         disable = yes  改為 no  
  3.         socket_type     = stream 
  4.         wait            = no 
  5.         user            = root 
  6.         server          = /usr/bin/rsync 
  7.         server_args     = --daemon 
  8.         log_on_failure  += USERID 
1.2修改rsync.conf 設定檔 (如果沒有此檔的話請自行建立)
以下所使用到的設定參數請依照需求選取設定。

vim /etc/rsyncd.conf 
  1. ###backup config###                      //註解
  2. [web]                                   //module name
  3.            comment = backup dmz web      //說明
  4.            hosts allow = 192.168.1.222   //允許使用rsync連入的ip
  5.            hosts deny = *                //不允許連入的ip,*表示全檔
  6.            path = /home/backup/          //存放備份資料的目錄
  7.            auth users = root    //認證帳號 (要設定於rsyncd.secrets 內的帳號)
  8.            uid = root                    //用來啟動rsync server 的uid
  9.            gid = root                    //用來啟動rsync server 的gid
  10.            secrets file = /etc/rsyncd.secrets      //密碼檔存放路徑
  11.            read only = no                          //是否設定為唯讀
  12.      dont compress = *.gz *.tgz *.zip *.z *.rpm *.deb *.iso *.bz2 *.tbz 
  13. //不要對這些附檔名的檔案做壓縮
1.3設定連線的帳號密碼
vim /etc/rsyncd.secrets 
  1. root:12345  (設定格式:  帳號:密碼) 
  2. 修改rsyncd.secrets使用權限 (很重要) 
  3. [root@localhost ~]# chmod 600 /etc/rsyncd.secrets 
  4. [root@localhost ~]# chown root.root  /etc/rsyncd.secrets 
1.4重新啟動 xinetd,不是啟動 rsync 。
這邊應該會有人有所疑問,為什麼是重啟xinetd而不是rsync。
在2.1步驟時,是使用xinetd來啟動rsync。因此在這邊是重啟xinetd而不是rsync。
[root@localhost ~]# /etc/init.d/xinetd restart
1.5 設定開機自動啟動xinetd
基本上xinetd,已經有預設開機會啟動,如果沒有話請自行手動設定

chkconfig xinetd on              #開機自動啟動 
  1. [root@localhost ~]# /etc/init.d/xinetd  restart      #手動啟動 
1.6 測試看看是否都正常啟用 (rsync的port為873)
netstat -tnlp | grep 873 
  1. tcp    0    0 0.0.0.0:873        0.0.0.0:*         LISTEN      4235/xinetd 
  2. [root@localhost ~]# telnet localhost 873   
  3. #若出現下列訊息表示正常
  4. Trying 127.0.0.1... 
  5. Connected to localhost.localdomain (127.0.0.1). 
  6. Escape character is '^]'. 
  7. @RSYNCD: 29 
2. 設定A主機
2.1 設定登入B主機的密碼
在A主機上只需要設定密碼就可以。

vim /etc/rsyncd.secrets 
  1. 12345  (設定格式:  密碼) 
  2. #修改rsyncd.secrets使用權限 (很重要)
  3. [root@localhost ~]# chmod 600 /etc/rsyncd.secrets 
  4. [root@localhost ~]# chown root.root  /etc/rsyncd.secrets 
2.3 測試rsync是否可傳輸資料
  1. /usr/bin/rsync -avrHS --delete --password-file=/etc/rsyncd.secrets  /opt root@192.168.1.122::web  
  2. #或是
  3. [root@localhost ~]#/usr/bin/rsync -rvlHpogDtS --delete --password-file=/etc/rsyncd.secrets  /opt/ root@192.168.1.122::web 
  4. #rsync結束後會看到下列訊息
  5. sent 19380160 bytes  received 1062 bytes  1685323.65 bytes/sec 
  6. total size is 19374133  speedup is 1.00 
3設定排程
如果剛才的同步指令沒有問題的話。就可以開始設定排程了
3.1先自行寫一個rsync 的script 用來設定排程

vim /root/bin/rsync.sh   #存放在/root/bin/ 
  1. #!/bin/sh
  2. /usr/bin/rsync -avrHS --delete --password-file=/etc/rsyncd.secrets  /opt root@192.168.1.122::web 
3.2設定排程
crontab –e
0 0 * * * /root/bin/rsync.sh
參數說明 :
-h –help show this help (-h works with no other options)
-v, –verbose 列出rsync過程 詳細內容
-q, –quiet 不列出rsync過程 詳細內容
-c, –checksum skip based on checksum, not mod-time & size
-a, –archive archive mode 權限保存模式,相當於-rlptgoD 參數(no -H)
–no-OPTION turn off an implied OPTION (e.g. –no-D)
-r, –recursive 複製所有下層的資料(遞迴)
-R, –relative 使用相對路徑
–no-implied-dirs don't send implied dirs with –relative
-b, –backup 目的端已經存在的檔案在傳輸或刪除前會被備份
(see –suffix & –backup-dir)
–backup-dir=DIR DIR設定為備份的資料夾路徑
–suffix=SUFFIX suffix設定備份檔案名稱字尾,預設為 ~
-u, –update skip files that are newer on the receiver
–inplace update destination files in-place (SEE MAN PAGE)
–append append data onto shorter files
-d, –dirs transfer directories without recursing
-l, –links 複製所有的連結
-L, –copy-links transform symlink into referent file/dir
–copy-unsafe-links only "unsafe" symlinks are transformed
–safe-links ignore symlinks that point outside the source tree
-k, –copy-dirlinks transform symlink to a dir into referent dir
-K, –keep-dirlinks 把連結到資料夾的檔案當成資料夾處理
-H, –hard-links 保留硬式連結
-p, –perms 保留檔案權限
-E, –executability 保留文件的可執行性
-A, –acls 保留 ACLs (implies –perms)
-X, –xattrs preserve extended attributes (implies –perms)
–chmod=CHMOD 改變目的地的權限
-o, –owner 保留檔按擁有者 (super-user only)
-g, –group 保留檔按群組
–devices preserve device files (super-user only)
–specials preserve special files
-D 保留device資訊
-t, –times 保留時間點
-O, –omit-dir-times 保留時間點 省略目錄
–super receiver attempts super-user activities
-S, –sparse 嘗試處理零星檔案,讓這些檔案在目的端佔去較少磁碟空間
-n, –dry-run 不實際執行傳送,只顯示將會有的傳輸動作
-W, –whole-file 複製整個文件 (without rsync algorithm)
-x, –one-file-system 不要跨越檔案系統分界(只在一個檔案系統處理)
-B, –block-size=SIZE 強制透過rsync程式去比對修復block-sizeforce
-e, –rsh=COMMAND 使用指定的 shell

以下是預設定xinetd.conf
[root@localhost ~]# vim /etc/xinetd.conf
defaults
{
# The next two items are intended to be a quick access place to
# temporarily enable or disable services.
# enabled = #啟用
# disabled = #禁用
# Define general logging characteristics.
log_type = SYSLOG daemon info #登錄檔的記錄服務類型
log_on_failure = HOST #發生錯誤時需要記錄的資訊為主機 (HOST)
log_on_success = PID HOST DURATION EXIT #成功啟動或登入時的記錄資訊
# Define access restriction defaults
# no_access =
# only_from =
# max_load = 0
cps = 50 10 #同一秒內最大連線數為 50 個,超過則暫停 10 秒
instances = 50 #同一服務的最大同時連線數
per_source = 10 # 同一來源的用戶端的最大連線數
# Address and networking defaults
# bind =
# mdns = yes
v6only = no #是否僅允許 IPv6
# setup environmental attributes
# passenv =
groups = yes
umask = 002
# Generally, banners are not used. This sets up their global defaults
# banner =
# banner_fail =
# banner_success =
}
includedir /etc/xinetd.d #在/etc/xinetd.d會有更多的設定值


useradd -d /c/backup/rbackup2 -s /bin/bash -g users -c "Rsync User2" rbackup2
rsync -av ./testsync/ rbackup2@10.1.36.28:
(Note: a –> archive, v –> verbose, n –> dry run)
Logfile: -logfile=\home\backup\rsync.log -logmode=append


















没有评论:

发表评论