Linux下rsync 資料鏡像備份
rsync特性:
可以鏡像儲存整個目錄樹和檔案系統
可以增量同步資料,檔案傳輸效率高,因而同步時間很短。
可以保持原有檔案的權限、時間等屬性。
加密傳輸資料,保證了資料的安全性
兩種模式:
client / server # 通過873端口建立資料同步
client / client # 通過SSH 預設22 端口建立資料同步
安裝rsync
yum install rsync
檢視rsync版本
rpm -qa rsync
rsync-3.1.2-4.el7.x86_64
或者 rsync
檢視rsync安裝位置
rpm -ql rsync
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsISPrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdsATOfd3bkFGazxCMx8VesATMfhHLlN3XnxCMwEzX0xiRGZkRGZ0Xy9GbvNGLpZTY1EmMZVDUSFTU4VFRR9Fd4VGdsYTMfVmepNHLrJXYtJXZ0F2dvwVZnFWbp1zczV2YvJHctM3cv1Ce-cmbw5SYwMDOmlTZjF2N3UzY3UTZwQGN4MzYkBjYwAzM2YzN28CX4EzLcFTM4EDMy8CXn9Gbi9CXzV2Zh1WavwVbvNmLvR3YxUjLxM3Lc9CX6MHc0RHaiojIsJye.png)
在伺服器端:
編輯rsync配置檔案
vim /etc/rsyncd
内容如下
uid = nobody
gid = nobody
use chroot = no
max connections = 10
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[rsync_module_1]
path = /rsync_data_1
comment = rsync_data for sharing to client server
ignore errors
read only = true
list = false
uid = root
gid = root
auth users = rsync_bak
secrets file = /etc/rsync_server.pass
按照配置檔案内容,建立/etc/rsync_server.pass 檔案,且權限為600
内容
rsync_bak:rsyncpwd
chmod 600 /etc/rsync_server.pass
根據配置檔案建立相應的目錄
mkdir -p /rsync_data_1
啟動rsync服務和驗證
/usr/bin/rsync --daemon
ps -ef | grep rsync
lsof -c rsync
netstat -antlp | grep rsync
用戶端
確定安裝了rsync元件
建立用戶端rsync的密碼檔案,其密碼和伺服器端一緻,且檔案權限為600
vim /etc/rsync_client.pass
cat /etc/rsync_client.pass
rsyncpwd
chmod 600 /etc/rsync_client.pass
用戶端指定/建立rsync的目錄
mkdir -p /rsync_data
從伺服器端拉取資料
/usr/bin/rsync -vzrtopg --delete --progress [email protected]::rsync_module_1 /rsync_data --password-file=/etc/rsync_client.pass
驗證是否成功從伺服器端拉取到資料
ll /rsync_data/
注意:無論用戶端對資料怎麼樣操作,隻要進行了資料同步操作,始終保持伺服器端的資料
Rsync 的 client / client 模式
例子:
rsync -vzrtopg --delete --progress -e 'ssh -p 9090' data_1 [email protected]:/tmp (推送模式)
rsync -vzrtopg --delete --progress -e 'ssh -p 9090' [email protected]:/tmp/data_1 /data/(拉取模式)
9090是sshd服務的自定義端口, 預設22端口的話不加 -e 選項。
轉載于:https://blog.51cto.com/snowlai/2318610