現在的存儲系統,本身都具備很強的遷移以及備份策略,雖然還是基於網絡傳輸,有相對延遲,但是方便了不少。另外,現在使用的存儲系統,讀寫瓶頸的問題,也大都改爲對象存儲。
而我們那時候做文件存儲,最頭疼的就是數據遷移與異地備份。最好用<ku,bi>的同步工具就是rsync。
【基礎介紹】
rsync:rsync - a fast, versatile, remote (and local) file-copying tool。快速、通用、遠程(和本地)文件複製工具。
顯著特點:
1,快速--基於獨有的“rsync”算法。實現增量同步。
2,簡單--基於開源協議,安裝、配置簡單。且特定場景下簡單同步只需要配置服務端即可。客戶端基於linux發行版自帶。
3,方便--強大的參數支持,可以在同步過程中,自定義實現保留符號鏈接、硬鏈接、權限、時間、歸屬等信息。
4,安全--配置文件可完成密碼訪問、讀寫權限、IP控制等安全限制。
5,完整--仍是基於“rsync算法”,實現文件同步的數據完整性,以及支持目錄同步、壓縮同步及遞歸拷貝常見使用場景。
rsync默認開啓873端口,以原生rsync傳輸協議或通過遠程shell提供文件。
【安裝及配置】
獲取源碼包:#wget --no-check-certificate https://download.samba.org/pub/rsync/src/rsync-3.1.3.tar.gz
安裝與部署:
# tar zxvf rsync-3.1.3.tar.gz && cd rsync-3.1.3
# ./configure
# make && make install
# touch /etc/rsyncd.conf #默認沒有配置文件,需要自己創建。
相關文件說明:
二進制文件:/usr/local/bin/rsync
配置文件:/etc/rsyncd.conf
配置文件說明:官方文檔==https://download.samba.org/pub/rsync/rsyncd.conf.html
官方文檔大致介紹:
[描述]
rsync配置文件控制身份驗證、訪問、日誌記錄和可控模塊。
[文件格式]
rsync配置文件由模塊和參數組成,一個模塊以方括號中的模塊名稱開始,一直持續到下一個模塊開始。模塊中的參數類型爲name = value。
rsync配置文件是基於行的,也就是說,每個新行終止的行,代表一個註釋、一個模塊名或一個參數。
在參數中,只有第一個等號是重要的,在第一個等於號之前或之後的空格被丟棄。在模塊和參數名稱中,引號、拖尾和內部空格是不相關的。參數值內的內部空白被逐字保留。
任何以#開頭的行都被當做空格忽略(如果一個#發生在除開頭的空白之外的任何東西之後,它就被認爲是該行內容的一部分)。
任何以\結尾的行都繼續以常規的UNINX方式進行下一行。
參數中,等號後面的值要麼是一個字符串(不需要引號),要麼是一個布爾值。它可以是yes/no,0/1或true/false。
【參數說明】
[全局參數]
文件中的第一個參數(在[模塊]頭之前)是全局參數,rsync還允許使用[global]模塊名稱來表示一個或多個全局參數部分的開頭(名稱必須小寫)。
rsync同樣支持在配置文件的全局部分包含任何模塊的參數,在這種情況下,所提供的值將覆蓋該參數的默認值。、
rsync同樣支持參數值引用環境變量。將文字%插入到一個值中的最安全方法是使用%%。
motd file:該參數允許指定一個“當日消息”,以便在每個連接上顯示給客戶機。默認沒有mot文件,當啓動守護進程時,可以被-dparam=motdfile=文件命令行選項覆蓋。
pid file:該參數告訴rsync守護進程將進程ID寫入該文件,如果文件已經存在,守護進程將終止而不是覆蓋文件。當啓動守護進程時,可以被-dparam=pidfile=文件命令行選項覆蓋。
port:該參數告訴rsync對應的監聽端口,默認是873。如果這個守護進程是由inetd運行的,並且被--prot命令行選項替代,那麼就會忽略它。
address:該參數告訴rsync對應的監聽IP地址,如果這個守護進程是由inetd運行的,並且被--address命令行選項替代,那麼就會忽略它。
socket options:該參數可以將系統接口調到最大程度。可以設置各種各樣的套接字選項,使傳輸更快(或更慢)讀取setsockopt()系統的man頁,以瞭解可能設置的一些選項的詳細信息。默認情況下,不設置特殊的套接字選項。
listen backlog:當守護進程監聽連接時,可以覆蓋默認的backlog值。它默認爲5。
[模塊參數]
模塊參數遵循全局參數的基礎信息,如支持環境變量。
rsync中模塊名稱不能包含斜線或方括號,如果包含空格,則每個內部序列被改爲單個空格。
comment:該參數指定客戶端獲取可用模塊列表時顯示在模塊名稱旁邊的說明字符串。默認無。
path:該參數指定守護進程文件系統中的目錄,使其在此模塊中可用,必須爲每個模塊指定此參數。通過百分號包圍變量名稱,可以將路徑的值從環境變量中出去,甚至可以引用用戶連接時由rsync設置的變量。如果最終目錄有一個尾隨空格,請在路徑後面追加一個斜槓避免丟失。
use chroot:如果爲true,則在開始於客戶端進行文件傳輸之前,rsync守護進程根據“路徑”進行chroot。具有額外保護的優點,需要具有超級用戶特權。默認無。
daemon chroot:同上,用於整個守護進程,且需要安裝os/lib/etc文件才能發揮作用。默認無。
numeric ids:啓用該參數將禁用當前守護進程模塊的名稱映射用戶和組。默認無。
munge symlinks:該參數告訴rsync修改所有符號鏈接。默認無。
charset:指定模塊文件名的字符集名稱。如果客戶端使用--iconv選項,守護進程將使用“charset”參數的值,而不管客戶端實際傳遞的字符集是什麼。默認無。
max connections:該參數允許指定允許的最大併發數。默認爲0,表示沒有限制。
log file:當該參數設置爲非空字符串時,rsync守護進程會將消息記錄到指定的文件而不是使用syslog。如果守護進程無法打開指定文件,它將回退到使用syslog並輸出關於失敗的錯誤。
syslog facility:該參數允許指定在從rsync守護進程記錄消息時使用的系統日誌工具名稱。
syslog tag:該參數允許指定在從rsync守護進程記錄消息時使用的syslog標記。默認值是rsyncd。
max vervbosity:該參數允許控制守護進程生成的詳細細信息的最大數量。默認是1,也會影響用戶請求更高級別的--info和--debug日誌記錄的能力。
lock file:該參數用於支持“最大連接數”參數的文件。rsynfc守護進程在此文件上使用記錄鎖定,以確保共享鎖文件模塊的最大連接數限制不會超出。默認是/var/run/rsyncd.lock
read only:該參數確定客戶端是否能夠上傳文件,如果“只讀”爲真,則任何嘗試上傳都將失敗。如果“只讀”爲假,則如果文件權限允許,可以上傳。默認情況下,所有模塊都是隻讀的。“auth users”可以基於每個用戶覆蓋該選項。
write only:該參數確定客戶端是否能夠下載文件,如果“只寫”爲真,則任何嘗試下載都將失敗。如果“只寫”爲假,則如果文件權限運維,可以下載。默認情況下,此參數被禁用。
list:該參數確定當客戶要求提供可用模塊列表時,是否列出此模塊。默認情況下,模塊可以列出。
uid:該參數指定在守護進程以root用戶身份運行時,該文件傳入和傳出該模塊的用戶名或用戶標識。超級用戶運行時,默認值是切換到系統的“nobody”用戶,非超級用戶的默認設置是不嘗試更改用戶。RSYNC_USER_NAME環境變量可用於請求將rsync作爲授權用戶運行。
gid:該參數指定訪問模塊時將使用的一個或多個組名/ID,第一個是默認組,其他都被設置爲不充足。也可以在列表中,指定一個*作爲第一個gid.
daemon uid:該參數指定守護進程將在哪個uid下運行,通常以root身份運行,並在未設置時保持用戶不變。
daemon gid:該參數指定守護進程將在哪個gid下運行,通常以組的根目錄運行,當未設置時,組保持不變。
fake super:等同於守護進程--fake-super命令行選項,允許存儲文件的完整屬性,而不必讓守護進程實際上以root身份運行。
filter:過濾器選項,決定了允許客戶端訪問的文件。該鏈不會發送給客戶端。按照優先級順序從過濾器、飽漢子、包含、排除和排除參數構建而成。
exclude:該參數採取空格分割的守護進程排除模式列表,作爲客戶端-排除選項。
include:使用“include”來覆蓋“exclude”參數的效果。
exclude from/include from:該參數指定守護進程上包含守護進程排除/包含模式的文件的名稱,每行一個。
inconming chmod:該參數允許指定一組逗號分隔的chmod字符串,將影響所有傳入文件的權限。這些更貴發生在所有其他權限計算之後,並且甚至會在客戶端未指定--perms時覆蓋目標默認/現有權限。
outgoing chmod:該參數允許指定一組逗號分隔的chmod字符串,將影響所有傳出文件的權限。這些更改首先發生,使得發送的權限看起來與存儲在文件系統本身中的權限不同。
auth users:該參數指定授權規則的逗號和斜槓或空格分割列表,以最簡單的形式列出將被允許鏈接到該模塊的用戶名。用戶名不需要存在於本地系統上,規則可能包含shell通配符。支持@,:等,如果需要的話,詳細參考幫助文檔。
secrets file:該參數指定包含用於驗證此模塊的user:password的文件名稱,只有在指定了‘auth users’參數時纔會查閱此文件,此文件是基於行的,並且每行包含一個名稱:密碼對。密碼可以包含是任何字符,但會被警告限制可以再客戶端輸入的密碼長度。
strict modes:該參數決定是否檢查密碼文件的權限。如果爲真,那麼密碼文件不能被除了運行rsync守護進程只爲的任何用戶表示讀取。如果爲假,則不執行檢查。默認爲真。
hosts allow:該參數允許指定與連接客戶端的主機名和IP地址相匹配的以逗號或空格分割的模式列表。如果沒有任何模式匹配,則連接被拒絕。
hosts deny:該參數允許指定與連接的客戶端的主機名和IP地址相匹配的以逗號或空格分割的模式列表。如果模式匹配,則連接被拒絕。
reverse lookup:反向查找,默認啓用。控制守護進程是否對客戶端的IP地址執行反向查找以確定其主機名。
forward lookup:正向查找,默認啓用。允許使用連接IP的反向DNS不會返回顯式主機名。
ignore errors:當決定是否運行傳輸的delete階段時,該參數指示rsync忽略守護進程上的I/O錯誤。正常情況下,發生任何I/O錯誤,rsync將跳過--delete步驟,以防止由於臨時資源短缺或其他I/O錯誤導致的額災難性刪除。
ignore nonreadable:該參數高數守護進程完全忽略用戶不可讀的文件。
transfer logging:該參數使得每個文件的日誌記錄下載和上傳的格式,總是在最後記錄傳輸,如果傳輸被終止,則日誌文件不再記錄。
log format:該參數允許在啓用傳輸日誌記錄時,指定用於記錄文件傳輸的格式。格式是一個文本字符串,其中包含以%字符爲前綴的嵌入式單字符穿衣序列。默認格式:“%o%h[%a]%m (%u) %f%l”
%a遠程IP地址(僅適用於守護進程)
%b實際傳輸的字節數
%B文件的權限位(例如rwxrwxrwt)
%c爲基礎文件接收的塊校驗和的總大小(僅當發送時)
%C如果文件已知,則爲全文件校驗和。對於較舊的rsync協議/版本,校驗和被醃製,因此不是有用的值(並且在這種情況下不顯示)。對於文件輸出的校驗和,或者--checksum選項必須有效,或者文件必須已經被傳輸而沒有使用鹽醃校驗和。請參閱--checksum-choice選項以選擇算法。
%f文件名(發件人的長表單;沒有結尾的“/”)
%G文件的gid(十進制)或“DEFAULT”
%h遠程主機名(僅適用於守護進程)
%i我正在更新的項目列表
%l文件的長度(以字節爲單位)
%L字符串“ - > SYMLINK”,“=> HARDLINK”或“”(其中SYMLINK或HARDLINK是文件名)
%m模塊名稱
%M文件的上次修改時間
%n表示文件名(縮寫形式;尾部“/”表示dir)
操作的%,即“發送”,“接收”或“刪除”。(後者包括後期)
%p此rsync會話的進程ID
%P模塊路徑
%t當前日期時間
%u認證用戶名或空字符串
%U文件的uid(十進制)
timeout:該參數允許覆蓋客戶端選擇此模塊的I/O超市,該參數可以確保rsync不會永遠等待客戶端。超時時間以秒爲單位,默認爲0,表示沒有超時時間。匿名rsync守護進程推薦600s。
refuse options:該參數允許指定將由rsync守護進程聚聚的rsync命令行選項的空格分割列表。
dont compress:該參數允許根據通配符模式來選擇文件名,並且傳輸過程中,不壓縮。
配置文件示例:
A simple rsyncd.conf file that allow anonymous rsync to a ftp area at /home/ftp
would be:
[ftp] path = /home/ftp comment = ftp export area
A more sophisticated example would be:
uid = nobody gid = nobody use chroot = yes syslog facility = local5 pid file = /var/run/rsyncd.pid [ftp] path = /var/ftp/./pub comment = whole ftp area (approx 6.1 GB) [sambaftp] path = /var/ftp/./pub/samba comment = Samba ftp area (approx 300 MB) [rsyncftp] path = /var/ftp/./pub/rsync comment = rsync ftp area (approx 6 MB) [sambawww] path = /public_html/samba comment = Samba WWW pages (approx 240 MB) [cvs] path = /data/cvs comment = CVS repository (requires authentication) auth users = tridge, susan secrets file = /etc/rsyncd.secrets
The /etc/rsyncd.secrets file would look something like this:
tridge:mypass
susan:herpass
【rsync算法解析】
1,算法是由Andrew Tridgell發明的。他還是rzip/KnightCap的原作者。
2,整體算法,請參考酷殼陳皓前輩的詳解<下邊有鏈接>。
3,根據以上詳解,整理如下:
a,分塊checksum算法:用於將目標文件平均切分成若干塊,然後對每塊計算兩個checksum<一弱:用於區別不同。一強:用於確認相同>。
b,傳輸算法:同步目標端,會把目標文件的checksum列表<強,弱,文件塊編號>傳給同步源。
c,checksum查找算法:同步源拿到目標文件的checksum數組後,把數據存在hash table裏,用弱做hash。
d,對比算法:
1,取源文件的第一個文件塊,做弱計算,計算好的值去hash table中查找。
2,如果查到了,說明發現在目標文件又潛在相同的文件塊。再比較強計算。如果弱強都相同,標記這一塊在目標文件的文件編號。
3,如果沒查到不用計算強,算法往後setp 1個字節,取源文件的第二個塊做弱計算。
4,根據以上,找出源文件相鄰兩次匹配中的文本字符,就是需要往目標同步的文件內容了。
【基本使用-客戶端】
本地傳輸: Local: rsync [OPTION...] SRC... [DEST] 示例:rsync -avzP /home/jing/1.txt /home/jing/2.txt 遠程命令: Pull-從遠程目錄拉文件: rsync [OPTION...] [USER@]HOST:SRC... [DEST] 示例:rsync -avzP [email protected]:/home.jing/1.txt /home/jing/ Push-從本地目錄推文件: rsync [OPTION...] SRC... [USER@]HOST:DEST 示例:rsync -avzP /home/jing/2.txt [email protected]:/home/jing/ 遠程模塊: Pull-從遠程目錄拉文件: rsync [OPTION...] [USER@]HOST::SRC... [DEST] rsync [OPTION...] rsync://[USER@]HOST[:PORT]/SRC... [DEST] 示例:rsync -avzP [email protected]::test /home/jing/ Push-從本地目錄推文件: rsync [OPTION...] SRC... [USER@]HOST::DEST rsync [OPTION...] SRC... rsync://[USER@]HOST[:PORT]/DEST 示例:rsync -avzP /home/jing/* [email protected]::test
【參數說明】
-v, --verbose increase verbosity --info=FLAGS fine-grained informational verbosity --debug=FLAGS fine-grained debug verbosity --msgs2stderr special output handling for debugging -q, --quiet suppress non-error messages --no-motd suppress daemon-mode MOTD (see caveat) -c, --checksum skip based on checksum, not mod-time & size -a, --archive archive mode; equals -rlptgoD (no -H,-A,-X) --no-OPTION turn off an implied OPTION (e.g. --no-D) -r, --recursive recurse into directories -R, --relative use relative path names --no-implied-dirs don't send implied dirs with --relative -b, --backup make backups (see --suffix & --backup-dir) --backup-dir=DIR make backups into hierarchy based in DIR --suffix=SUFFIX backup suffix (default ~ w/o --backup-dir) -u, --update skip files that are newer on the receiver --inplace update destination files in-place --append append data onto shorter files --append-verify --append w/old data in file checksum -d, --dirs transfer directories without recursing -l, --links copy symlinks as symlinks -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 tree --munge-links munge symlinks to make them safer -k, --copy-dirlinks transform symlink to dir into referent dir -K, --keep-dirlinks treat symlinked dir on receiver as dir -H, --hard-links preserve hard links -p, --perms preserve permissions -E, --executability preserve executability --chmod=CHMOD affect file and/or directory permissions -A, --acls preserve ACLs (implies -p) -X, --xattrs preserve extended attributes -o, --owner preserve owner (super-user only) -g, --group preserve group --devices preserve device files (super-user only) --specials preserve special files -D same as --devices --specials -t, --times preserve modification times -O, --omit-dir-times omit directories from --times -J, --omit-link-times omit symlinks from --times --super receiver attempts super-user activities --fake-super store/recover privileged attrs using xattrs -S, --sparse turn sequences of nulls into sparse blocks --preallocate allocate dest files before writing -n, --dry-run perform a trial run with no changes made -W, --whole-file copy files whole (w/o delta-xfer algorithm) --checksum-choice=STR choose the checksum algorithms -x, --one-file-system don't cross filesystem boundaries -B, --block-size=SIZE force a fixed checksum block-size -e, --rsh=COMMAND specify the remote shell to use --rsync-path=PROGRAM specify the rsync to run on remote machine --existing skip creating new files on receiver --ignore-existing skip updating files that exist on receiver --remove-source-files sender removes synchronized files (non-dir) --del an alias for --delete-during --delete delete extraneous files from dest dirs --delete-before receiver deletes before xfer, not during --delete-during receiver deletes during the transfer --delete-delay find deletions during, delete after --delete-after receiver deletes after transfer, not during --delete-excluded also delete excluded files from dest dirs --ignore-missing-args ignore missing source args without error --delete-missing-args delete missing source args from destination --ignore-errors delete even if there are I/O errors --force force deletion of dirs even if not empty --max-delete=NUM don't delete more than NUM files --max-size=SIZE don't transfer any file larger than SIZE --min-size=SIZE don't transfer any file smaller than SIZE --partial keep partially transferred files --partial-dir=DIR put a partially transferred file into DIR --delay-updates put all updated files into place at end -m, --prune-empty-dirs prune empty directory chains from file-list --numeric-ids don't map uid/gid values by user/group name --usermap=STRING custom username mapping --groupmap=STRING custom groupname mapping --chown=USER:GROUP simple username/groupname mapping --timeout=SECONDS set I/O timeout in seconds --contimeout=SECONDS set daemon connection timeout in seconds -I, --ignore-times don't skip files that match size and time --size-only skip files that match in size -@, --modify-window=NUM set the accuracy for mod-time comparisons -T, --temp-dir=DIR create temporary files in directory DIR -y, --fuzzy find similar file for basis if no dest file --compare-dest=DIR also compare received files relative to DIR --copy-dest=DIR ... and include copies of unchanged files --link-dest=DIR hardlink to files in DIR when unchanged -z, --compress compress file data during the transfer --compress-level=NUM explicitly set compression level --skip-compress=LIST skip compressing files with suffix in LIST -C, --cvs-exclude auto-ignore files in the same way CVS does -f, --filter=RULE add a file-filtering RULE -F same as --filter='dir-merge /.rsync-filter' repeated: --filter='- .rsync-filter' --exclude=PATTERN exclude files matching PATTERN --exclude-from=FILE read exclude patterns from FILE --include=PATTERN don't exclude files matching PATTERN --include-from=FILE read include patterns from FILE --files-from=FILE read list of source-file names from FILE -0, --from0 all *from/filter files are delimited by 0s -s, --protect-args no space-splitting; wildcard chars only --address=ADDRESS bind address for outgoing socket to daemon --port=PORT specify double-colon alternate port number --sockopts=OPTIONS specify custom TCP options --blocking-io use blocking I/O for the remote shell --outbuf=N|L|B set out buffering to None, Line, or Block --stats give some file-transfer stats -8, --8-bit-output leave high-bit chars unescaped in output -h, --human-readable output numbers in a human-readable format --progress show progress during transfer -P same as --partial --progress -i, --itemize-changes output a change-summary for all updates -M, --remote-option=OPTION send OPTION to the remote side only --out-format=FORMAT output updates using the specified FORMAT --log-file=FILE log what we're doing to the specified FILE --log-file-format=FMT log updates using the specified FMT --password-file=FILE read daemon-access password from FILE --list-only list the files instead of copying them --bwlimit=RATE limit socket I/O bandwidth --write-batch=FILE write a batched update to FILE --only-write-batch=FILE like --write-batch but w/o updating dest --read-batch=FILE read a batched update from FILE --protocol=NUM force an older protocol version to be used --iconv=CONVERT_SPEC request charset conversion of filenames --checksum-seed=NUM set block/file checksum seed (advanced) -4, --ipv4 prefer IPv4 -6, --ipv6 prefer IPv6 --version print version number (-h) --help show this help (see below for -h comment)
【問題及記錄】
1,報錯信息:rsync: getaddrinfo: xxxxxx 873: Temporary failure in name resolution
字面解釋:解析失敗。
錯誤原因:無法正常解析域名地址。
解決方案:修改/etc/resolv.conf文件,添加對應DNS解析IP。
[root@XXXX bak]# cat /etc/resolv.conf
nameserver 114.114.114.114
nameserver 8.8.8.8
【參考文件】
維基百科:https://zh.wikipedia.org/wiki/Rsync
rsync官網:https://www.samba.org/ftp/rsync/rsync.html
rsync配置文件文檔:https://download.samba.org/pub/rsync/rsyncd.conf.html
酷殼的算法解析:https://coolshell.cn/articles/7425.html
Andrew Tridgell維基:https://en.wikipedia.org/wiki/Andrew_Tridgell