Rsync 實現遠程同步


  

 

    介紹

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

 

  常用場景一

無密碼同步

 

1、安裝rsync

[root@localhost /]# yum -y install rsync

2、新建rsyncd.conf文件

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
保存

3、執行命令

/usr/bin/rsync --daemon
mkdir -p /web/rsync/share_data  #新建共享目錄,實例中直接輸入要共享文件的就好,可省去次步

 

客戶端

 

1、安裝rsync

[root@localhost /]# yum -y install rsync

2、輸入命令進行同步

rsync -avz root@192.168.1.98:/var/space /home/hadoop/share_data
                     ↓            ↓                  ↓
                服務器IP    配置文件共享目錄名     客戶端收納目錄

3、限制流量同步

限制流量同步:
rsync -avz --bwlimit=50 --progress root@192.168.1.98::share_data /home/hadoop/share_data

 

  常用場景二

 

有密碼同步

 

服務端配置

1、配置文件修改

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

3、執行命令

echo "hadoop:password123" > /etc/rsyncd.passwd 
chmod 600 /etc/rsyncd.passwd
mkdir -p /web/rsync/auth_data

客戶端配置

1、保存密碼

echo "password123" > /home/hadoop/rsyncd.passwd 
chmod 600 /home/hadoop/rsyncd.passwd 

2、遠程同步

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

  常用場景三

寫入同步

服務端

1、配置文件

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

保存

2、執行命令

mkdir -p /web/rsync/write_data

客戶端

1、輸入同步命令

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或網段

服務端

1、配置文件修改

#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

 

常見情景例子

 

指定端口

 

rsync -avz --port=8081 --progress /home/hadoop/auth_data hadoop@192.168.1.98::auth_data 

 

限速

rsync --bwlimit=100 -avz --progress /home/hadoop/auth_data hadoop@192.168.1.98::auth_data 

限速100kb/s同步數據

 

Rsync通過SSH傳輸

rsync -e "ssh -p 22"  --progress /home/hadoop/auth_data hadoop@192.168.1.98::auth_data 

 

本文參考:ggjucheng

 

更多命令參考 

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

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

 


免責聲明!

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



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