rsync命令詳解

來源:互聯網
上載者:User

標籤:注釋   rysnc   rsh   format   時間戳記   接收   sync   rms   本地   


安裝
yum install rsync

rsync 有六種不同的工作模式。
rsync [OPTION]... SRC DEST
rsync [OPTION]... SRC [[email protected]]host:DEST
rsync [OPTION]... [[email protected]]HOST:SRC DEST
rsync [OPTION]... [[email protected]]HOST::SRC DEST
rsync [OPTION]... SRC [[email protected]]HOST::DEST
rsync [OPTION]...
rsync://[[email protected]]HOST[:PORT]/SRC [DEST]
拷貝本地檔案。當 SRC 和 DES 路徑資訊都不包含有單個冒號 ":" 分隔字元時就啟動這種工作模式。
rsync -a /data /backup
使用一個遠程 shell 程式(如 rsh、ssh)來實現將本地機器的內容拷貝到遠程機器。當 DST 路徑地址包含單個冒號 ":" 分隔字元時啟動該模式。
rsync -avz *.c foo:src
使用一個遠程 shell 程式(如 rsh、ssh)來實現將遠程機器的內容拷貝到本地機器。當 SRC 地址路徑包含單個冒號 ":" 分隔字元時啟動該模式。
rsync -avz foo:src/bar /data
從遠程 rsync 伺服器中拷貝檔案到本地機。當 SRC 路徑資訊包含 "::" 分隔字元時啟動該模式。
rsync -av [email protected]::www /databack
從本地機器拷貝檔案到遠程 rsync 伺服器中。當 DST 路徑資訊包含 "::" 分隔字元時啟動該模式。
rsync -av /databack [email protected]::www
列遠程機的檔案清單。這類似於 rsync 傳輸,不過只要在命令中省略掉本地機資訊即可。
rsync -v rsync://192.168.78.192/www

參數選項說明
-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給出某些檔案的傳輸狀態。
--log-format=formAT指定記錄檔格式。
--password-file=FILE從FILE中得到密碼。
--bwlimit=KBPS限制I/O頻寬,KBytes per second。
-h,--help顯示協助資訊。


使用方式
無密碼同步化
服務端修改設定檔 /etc/rsyncd.conf
#This is the rsync daemon configuration

#global settings
pid file = /var/run/rsyncd.pid
port = 873
lock file = /var/run/rsyncd.lock
log file = /var/log/rsync.log
gid = root
uid = root

#module settings
[share_data]
path = /web/rsync/share_data
use chroot = no
max connections = 15
read only = yes
write only = no
list = no
ignore errors = yes
timeout = 120
/usr/bin/rsync --daemon
mkdir -p /web/rsync/share_data
用戶端同步
rsync -avz --progress [email protected]::share_data /home/hadoop/share_data
用戶端限制流量同步
rsync -avz --bwlimit=50 --progress [email protected]::share_data /home/hadoop/share_data


有密碼同步化
服務端修改設定檔
#global settings
pid file = /var/run/rsyncd.pid
port = 873
lock file = /var/run/rsyncd.lock
log file = /var/log/rsync.log
gid = root
uid = root

#module settings
[auth_data]
path = /web/rsync/auth_data
use chroot = no
max connections = 15
read only = yes
write only = no
list = no
ignore errors = yes
timeout = 120
auth users = hadoop
secrets file = /etc/rsyncd.passwd
echo "hadoop:password123" > /etc/rsyncd.passwd
chmod 600 /etc/rsyncd.passwd
mkdir -p /web/rsync/auth_data


用戶端同步
echo "password123" > /home/hadoop/rsyncd.passwd
chmod 600 /home/hadoop/rsyncd.passwd
rsync -avz --progress --password-file=/home/hadoop/rsyncd.passwd [email protected]::auth_data /home/hadoop/auth_data


寫入同步
服務端修改設定檔
#global settings
pid file = /var/run/rsyncd.pid
port = 873
lock file = /var/run/rsyncd.lock
log file = /var/log/rsync.log
gid = root
uid = root

#module settings
[write_data]
path = /web/rsync/write_data
use chroot = no
max connections = 15
read only = no
list = no
ignore errors = yes
timeout = 120
auth users = hadoop
secrets file = /etc/rsyncd.passwd
mkdir -p /web/rsync/write_data


用戶端同步
echo "123" > /home/hadoop/write_file
export RSYNC_PASSWORD="password123"
rsync -avz --progress --delete /home/hadoop/write_file [email protected]::write_data


限定 IP 或者網段
#global settings
pid file = /var/run/rsyncd.pid
port = 873
lock file = /var/run/rsyncd.lock
log file = /var/log/rsync.log
gid = root
uid = root

#module settings
[write_data]
path = /web/rsync/write_data
use chroot = no
max connections = 15
read only = no
list = no
ignore errors = yes
timeout = 120
auth users = hadoop
secrets file = /etc/rsyncd.passwd
hosts allow = 192.168.2.32 192.168.1.0/24

配置說明
pid file = <FILE>進程寫入檔案。
port =指定連接埠,預設是 873。
address =指定伺服器 IP 位址。
uid =伺服器端傳輸檔案時,指定使用者執行,預設 nobody。
gid =伺服器端傳輸檔案時,指定使用者組執行,預設 nobody。
use chroot =伺服器精靈將 chroot 到檔案系統的目錄中,好處在於保護系統不被安裝漏洞侵襲。缺點是需要超級使用者權限。
read only = yes|no唯讀選擇,不讓用戶端上傳檔案到伺服器上。
write only = yes|no寫入選擇。
hosts allow =限制 IP 或網段,可以指定單個 IP,也可以指定整個網段,提高安全性。格式是空格間隔。
max connections =用戶端最多串連數。
motd file = <FILE>用於定義伺服器資訊,當使用者登入時會看到檔案中的內容資訊。
log file = <FILE>伺服器的日誌。
transfer logging = yes|no是否列印傳輸日誌
log format =日誌格式(%t %a %m %f %b)
lock file = <FILE>加鎖檔案。
path = <DIR>指定同步檔案目錄所在位。
auth users =認證使用者,必須在伺服器上存在的使用者。
list = yes|no是否把 rsync 伺服器上提供同步資料的目錄在伺服器上模組顯示出來。預設為 yes。
ignore errors忽略 IO 錯誤。
secrets file = <FILE>密碼存放檔案,有密碼登入時用到。
comment =注釋可自訂。
exclude =排除目錄或檔案,目錄之間可用空格分開。
rsync 的主要有三個設定檔 rsyncd.conf(主設定檔)、rsyncd.passwd(密碼檔案)、rsyncd.motd(rysnc 伺服器資訊)。
將 rsyncd.passwd 密碼檔案的檔案屬性設為 root 擁有,且許可權要設為 600,否則無法備份成功。
出於安全目的,檔案的屬性必需是只有屬主可讀。
密碼檔案格式為:
使用者名稱:密碼

 

rsync命令詳解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.