使用rsync同步資料

svoid發表於2016-03-18

rsync介紹

Rsync(Remote Synchronize)是一個遠端資料同步工具,可通過LAN/WAN快速同步多臺主機,Rsync使用所為的“Rsync演演算法”來使本地主機和遠端主機之間達到同步,這個演演算法並不是每次都整份傳送,它只傳送兩臺計算機之間所備份的資料不同的部分,因此速度相當快。

Rsync的優點如下:

  • 能更新整個目錄樹和檔案系統;
  • 有選擇性的保持原始檔的符號鏈鏈、硬連結、檔案屬於、許可權、裝置以及時間等;
  • 安裝無任何特殊許可權要求;
  • 對於多個檔案來說,內部流水線減少檔案等待的延時;
  • 能用rsh、ssh 或直接埠做為傳輸埠;
  • 支援匿名rsync 同步檔案,是理想的映象工具;

rsync安裝

rsync官方網站:https://rsync.samba.org 。

yum安裝方式:
shell> yum install rsync

原始碼安裝方式:
shell> tar zxvf rsync-3.1.2.tar.gz
shell> cd rsync-3.1.2
shell> ./configure --prefix=/usr
shell> make -j4 && make install

rsync配置

rsync的主要有以下三個配置檔案:

  • rsyncd.conf:主配置檔案
  • rsyncd.secrets:密碼檔案
  • rsyncd.motd:rysnc登入顯示資訊
shell> touch /etc/rsyncd.conf
shell> touch /etc/rsyncd.secrets
shell> chmod 600 /etc/rsyncd.secrets
shell> touch /etc/rsyncd.motd

shell> more /etc/rsyncd.conf
# This line is required by the /etc/init.d/rsyncd script
pid file = /var/run/rsyncd.pid
port = 873
address = 10.106.58.172
# 指定同步使用者
uid = root
gid = root

use chroot = yes
read only = yes

#limit access to private LANs
hosts allow=10.106.58.0/255.255.255.0
hosts deny=*

max connections = 5
motd file = /etc/rsyncd.motd

#This will give you a separate log file
log file = /var/log/rsync.log

#This will log every file transferred - up to 85,000+ per user, per sync
#transfer logging = yes

log format = %t %a %m %f %b
syslog facility = local3
timeout = 300

# 指定同步模組資訊
[test]
path = /test
list=yes
ignore errors
auth users = root
secrets file = /etc/rsyncd.secrets
comment = Test Rsync
exclude = data/

rsync密碼檔案使用者名稱和密碼可以隨便定義,最好不要和系統帳戶一致,同時要把密碼檔案許可權設定為600。
shell> more /etc/rsyncd.secrets
root:root123

設定rsync登陸顯示資訊,可以不設定
shell> more /etc/rsyncd.motd
++++++++++++++++++++++++++++++++++++++++++++++
Welcome to use the rsync services!
++++++++++++++++++++++++++++++++++++++++++++++

啟動rsync伺服器

A、使用--daemon引數方式啟動

shell> /usr/bin/rsync --daemon --config=/etc/rsyncd.conf

B、使用xinetd方式啟動

shell> more /etc/xinetd.d/rsync
# default: off
# description: The rsync server is a good addition to an ftp server, as it \
#       allows crc checksumming etc.
service rsync
{
        disable = no
        flags           = IPv6
        socket_type     = stream
        wait            = no
        user            = root
        server          = /usr/bin/rsync
        server_args     = --daemon
        log_on_failure  += USERID
}

重啟xinetd服務
shell> /etc/init.d/xinetd restart

rsync用法及常用引數

  • -a,--archive:歸檔模式,表示以遞迴方式傳輸檔案,並保持所有檔案屬性,等於-rlptgoD
  • -v --verbose:詳細模式輸出
  • -r --recursive:對子目錄以遞迴處理
  • -p --perms:保持檔案原有許可權
  • -o --owner:保持檔案原有屬主
  • -g --group:保持檔案原有使用者組
  • -t --times:保持檔案原有時間
  • --delete:刪除那些DST中存在而SRC中不存在的檔案或目錄
  • -z --compress:對檔案在傳輸時進行壓縮處理
  • --exclude=PATTERN:指定不需要傳輸的目錄
  • --include=PATTERN:指定需要傳輸的目錄
  • --exclude-from=FILE:排除FILE中指定模式的檔案
  • --include-from=FILE:不排除FILE中指定模式匹配的檔案
  • --progress:顯示詳細的傳輸進度情況
  • --password-file:指定密碼檔案,使用時無需互動式地輸入驗證密碼

rsync使用測試

建立客戶端密碼檔案
shell> more /etc/rsyncd.secrets
root123

列出rsync 伺服器上的所提供的同步內容
shell> rsync --list-only  root@10.106.58.172::
++++++++++++++++++++++++++++++++++++++++++++++
Welcome to use the rsync services!
++++++++++++++++++++++++++++++++++++++++++++++

test               Test Rsync

shell> rsync --list-only --password-file=/etc/rsyncd.secrets root@10.106.58.172::test
++++++++++++++++++++++++++++++++++++++++++++++
Welcome to use the rsync services!
++++++++++++++++++++++++++++++++++++++++++++++

drwxr-xr-x        4096 2016/03/17 23:37:28 .
-rw-r--r--           0 2016/03/17 23:32:06 abs.cfg
-rw-r--r--      685426 2016/03/17 23:37:28 setuptools-19.4.zip
drwxr-xr-x        4096 2016/03/17 23:36:35 test

使用rsync同步rsync伺服器指定資料測試
shell> rsync -avzP --delete --password-file=/etc/rsyncd.secrets root@10.106.58.172::test /tmp
++++++++++++++++++++++++++++++++++++++++++++++
Welcome to use the rsync services!
++++++++++++++++++++++++++++++++++++++++++++++

receiving incremental file list
deleting .ICE-unix/
deleting mysql.sock
./
abs.cfg
           0 100%    0.00kB/s    0:00:00 (xfer#1, to-check=3/5)
setuptools-19.4.zip
      685426 100%   12.11MB/s    0:00:00 (xfer#2, to-check=2/5)
test/
test/Python-2.7.9.tgz
    16657930 100%   15.28MB/s    0:00:01 (xfer#3, to-check=0/5)

sent 95 bytes  received 17288397 bytes  2659768.00 bytes/sec
total size is 17343356  speedup is 1.00

本地rsync,類似cp,將/tmp目錄下內容拷貝到/backup目錄下
shell> rsync -azP /tmp/* /backup

類似scp命令,將backup下目錄傳輸到174伺服器/tmp目錄下
shell> rsync -az /backup/ root@10.106.58.174:/tmp
root@10.106.58.174's password:

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29733787/viewspace-2059495/,如需轉載,請註明出處,否則將追究法律責任。

相關文章