rsync命令比對文件及增量同步


A fast,versatile,remote (and local) file-copying tool.

rsync基於ssh協議實現高效率遠程或本地文件復制,傳輸速度比scp快。復制文件時會比對本地文件與遠程主機的文件,僅復制有差異的文件。

常用選項:

-q,--quiet:suppress non-error messages 靜默模式
-v,--verbose:increase verbosity
-a,--archive:archive mode; equals -rlptgoD (no -H,-A,-X) 歸檔模式,相當於-rlptgoD,不包括(no -H,-A,-X);最常用的參數
-H,--hard-links:preserve hard links 保留硬鏈接
-A,--acls:preserve ACLs (implies --perms) 保留ACL權限
-X,--xattrs:preserve extended attributes 保留擴展屬性
-c, --checksum:skip based on checksum, not mod-time & size
-r,--recursive:recurse into directories 遞歸
-l,--links:copy symlinks as symlinks 保留軟鏈接,而不跟蹤原文件
-p,--perms:preserve permissions 保留權限
-t,--times:preserve modification times 保留mtime
-g,--group:preserve group 保留屬組
-o,--owner:preserve owner (super-user only) 保留屬主
-D:same as --devices,--specials 保留設備文件和特殊文件
--devices:preserve device files (super-user only)
--specials:preserve special files

-z,--compress:compress file data during the transfer 輸過程中壓縮文件數據
-n, --dry-run:perform a trial run with no changes made 干跑測試
-u,--update:skip files that are newer on the receiver 增量同步,跳過比本地較新的文件
--delete:delete extraneous files from destination dirs 刪除目標目錄多余文件
--progress:show progress during transfer 顯示傳輸進度

本地復制

# rsync -av ansible_auto/public/uy-s-192-v01.cfg objects/ansible_auto/public/uy-s-192-v01.cfg

比對文件

# cd /usr/local/nagios/etc/
# rsync -acvn ansible_auto/ objects/ansible_auto/ > diff

增量同步(合並目錄文件,最終與目標目錄一致)

# rsync -avzu --progress /opt/* devops@11.0.10.8:/opt/

刪除同步(刪除目標目錄多余文件,最終與源目錄一致)

# rsync -avzu --delete --progress /opt/* devops@11.0.10.8:/opt/

另外,可以設置計划任務實時同步和備份文件

# crontab -e
*/1 * * * * rsync -avzu /opt/media/* 192.168.201.123:/opt/media


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM