1. 程式人生 > >[CentOS 7系列]遠程數據同步

[CentOS 7系列]遠程數據同步

rsync

Rsync命令是一個遠程數據同步工具,可通過網絡快速同步多臺主機間的文件。它在同步文件的同時,可以保持原來文件的權限、時間、軟硬鏈接等附加信息,也可以做增量的拷貝。支持通過ssh方式來傳輸文件,這樣其保密性會非常好。rsync備份主要分為三種方式,一是本地到本地的備份,二是本地到網絡的備份,三是網絡到本地的備份。


▎命令格式:

       Local:  rsync [OPTION...] SRC... [DEST]
##拷貝本地文件。當SRC和DES路徑信息都不包含有單個冒號":"分隔符時就啟動這種工作模式。如:rsync -a /data /backup

       Access via remote shell:
         Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST]
         Push: rsync [OPTION...] SRC... [USER@]HOST:DEST
##使用一個遠程shell程序(如rsh、ssh)來實現將遠程機器的內容拷貝到本地機器。當SRC地址路徑包含單個冒號":"分隔符時啟動該模式。如:rsync -avz foo:src/bar /data
##使用一個遠程shell程序(如rsh、ssh)來實現將本地機器的內容拷貝到遠程機器。當DST路徑地址包含單個冒號":"分隔符時啟動該模式。如:rsync -avz *.c foo:src

       Access via rsync daemon:
         Pull: rsync [OPTION...] [USER@]HOST::SRC... [DEST]
               rsync [OPTION...] rsync://[USER@]HOST[:PORT]/SRC... [DEST]
         Push: rsync [OPTION...] SRC... [USER@]HOST::DEST
               rsync [OPTION...] SRC... rsync://[USER@]HOST[:PORT]/DEST
##從遠程rsync服務器中拷貝文件到本地機。當SRC路徑信息包含"::"分隔符時啟動該模式。如:rsync -av [email protected]
/* */::www /databack ##從本地機器拷貝文件到遠程rsync服務器中。當DST路徑信息包含"::"分隔符時啟動該模式。如:rsync -av /databack [email protected]::www Usages with just one SRC arg and no DEST arg will list the source files instead of copying. ##列遠程機的文件列表。這類似於rsync傳輸,不過只要在命令中省略掉本地機信息即可。如:rsync -v rsync://192.168.78.192/www

▎常用選項:

選項作用
-a包含-rtplgoD
-r同步目錄時要加上,類似cp時的-r選項
-v同步時顯示一些信息,讓我們知道同步的過程

-l

保留軟連接
-L加上該選項後,同步軟鏈接時會把源文件給同步
-p保持文件的權限屬性
-o保持文件的屬主
-g保持文件的屬組
-D保持設備文件信息
-t
保持文件的時間屬性
--delete刪除DEST中SRC沒有的文件
--exclude過濾指定文件不同步
-P
顯示同步過程,比如速率,比-v更加詳細
-u加上該選項後,如果DEST中的文件比SRC新,則不同步
-z傳輸時壓縮


測試示例:

本地到本地備份:
[[email protected] ~]# rsync -aP /tmp/bigfile /usr/local/
sending incremental file list
bigfile
   106070960 100%   30.24MB/s    0:00:03 (xfer#1, to-check=0/1)

sent 106083984 bytes  received 31 bytes  30309718.57 bytes/sec
total size is 106070960  speedup is 1.00

本地到網絡備份:
[[email protected]
/* */ ~]# rsync -av /tmp/bigfile 192.168.137.200:/tmp/ ##接收端也要安裝rsync工具 bash: rsync: 未找到命令 rsync: connection unexpectedly closed (0 bytes received so far) [sender] rsync error: remote command not found (code 127) at io.c(605) [sender=3.0.9] [[email protected] ~]# rsync -av /tmp/bigfile 192.168.137.200:/tmp/ sending incremental file list bigfile sent 106083984 bytes received 31 bytes 7316138.97 bytes/sec total size is 106070960 speedup is 1.00 [[email protected] ~]# ll /tmp/bigfile -rw-r--r--. 1 root root 106070960 7月 20 09:34 /tmp/bigfile [[email protected] tmp]# ll bigfile -rw-r--r--. 1 root root 106070960 7月 20 09:34 bigfile ##目標端查看信息完全一致 網絡到本地備份: [[email protected] ~]# rsync -av 192.168.137.200:/tmp/bigfile / receiving incremental file list bigfile sent 30 bytes received 106083989 bytes 6844130.26 bytes/sec total size is 106070960 speedup is 1.00


▎通過ssh方式同步:

[[email protected] ~]# rsync -av /tmp/ 192.168.137.200:/tmp/
sending incremental file list
./
bigfile
py.py
yum_save_tx.2017-07-20.09-37.LxNivf.yumtx

sent 106085634 bytes  received 77 bytes  7858200.81 bytes/sec
total size is 106072339  speedup is 1.00
[[email protected] ~]# rsync -av -e "ssh -p 22" /tmp/bigfile 192.168.137.200:/tmp/
sending incremental file list

sent 33 bytes  received 12 bytes  3.91 bytes/sec
total size is 106070960  speedup is 2357132.44


▎通過服務的方式同步:

①編輯配置文件/etc/rsyncd.conf

[[email protected] ~]# cat /etc/rsyncd.conf
# /etc/rsyncd: configuration file for rsync daemon mode

# See rsyncd.conf man page for more options.

# configuration example:

# uid = nobody
# gid = nobody
# use chroot = yes
# max connections = 4
# pid file = /var/run/rsyncd.pid
# exclude = lost+found/
# transfer logging = yes
# timeout = 900
# ignore nonreadable = yes
# dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2

# [ftp]
#        path = /home/ftp
#        comment = ftp export area
port=873
log file=/var/log/rsync/rsync.log
pid file=/var/run/rsyncd.pid
address=192.168.137.100
[test]
path=/root/rsync
use chroot=true
max connections=4   ##最大連接數,默認為0,表示無限制
read only=no        ##如果為true,則不能上傳到該模塊指定路徑下
list=true           ##用戶是否可以查詢該可用模塊
uid=root            ##傳輸時使用的UID
gid=root
auth users=test     ##傳輸時使用的用戶名
secrets file=/etc/rsyncd.passwd  ##密碼文件,不設置表示不使用。密碼文件權限為600。
hosts allow=192.168.137.200 1.1.1.1 2.2.2.2 ##被允許連接該模塊的主機

註:密碼文件格式:用戶名:密碼


②啟動服務rsync --daemon

[[email protected] ~]#/usr/bin/rsync --daemon --config=/etc/rsyncd/rsyncd.conf
                                                            ##config參數指定配置文件

如果需要關閉,可以kill進程。


③客戶端配置

Linux采用自帶rsync,Windows采用cwRsync(client)端。

Linux系統中,可以手動執行,也可以執行腳本中的rsync。

▽命令格式參考:

[[email protected] tmp]# rsync -av --progress [email protected]::backup ./test/


本文出自 “亂碼時代” 博客,請務必保留此出處http://juispan.blog.51cto.com/943137/1949196

[CentOS 7系列]遠程數據同步