rsync命令詳解

YatHo發表於2017-03-02

rsync命令是一個遠端資料同步工具,可通過LAN/WAN快速同步多臺主機間的檔案。rsync使用所謂的“rsync演算法”來使本地和遠端兩個主機之間的檔案達到同步,這個演算法只傳送兩個檔案的不同部分,而不是每次都整份傳送,因此速度相當快。 rsync是一個功能非常強大的工具,其命令也有很多功能特色選項,我們下面就對它的選項一一進行分析說明。

 

常用場景

無密碼同步

服務端:vim /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 root@192.168.1.98::share_data /home/hadoop/share_data

 限制流量同步

rsync -avz --bwlimit=50 --progress root@192.168.1.98::share_data /home/hadoop/share_data

 

有密碼同步

服務端  

vim /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 
[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  hadoop@192.168.1.98::auth_data /home/hadoop/auth_data

 

或者是

export RSYNC_PASSWORD="password123"
rsync -avz --progress hadoop@192.168.1.98::auth_data /home/hadoop/auth_data

 

寫入同步

服務端

vim /etc/rsyncd.conf

複製程式碼
#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 hadoop@192.168.1.98::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
複製程式碼

 

 

 

更多命令參考 

客戶端 https://download.samba.org/pub/rsync/rsync.html

服務端 https://download.samba.org/pub/rsync/rsyncd.conf.html