rsync資料同步使用
使用rsync實作資料同步。
一、基于伺服器--用戶端方式的資料同步。
1、server端配置
檢視軟體包是否安裝
rsync-3.0.6-5.el6_0.1.x86_64
2、開啟rsync服務
rsync不是一個獨立服務,需要xinetd支援,是以確定系統已經安裝了xinetd
[root@localhost ~]# rpm -qa |grep xinetd
xinetd-2.3.14-31.el6.x86_64
3、 啟動服務
[root@localhost ~]# service xinetd restart
Stopping xinetd: [ OK ]
Starting xinetd: [ OK ]
4、 檢視是否啟動了rsync服務,rsync服務使用的端口是873
[root@localhost ~]# netstat -antlp |grep 873
tcp 0 0 :::873 :::* LISTEN 7889/xinetd
5、 因為rsync服務預設沒有配置檔案,是以需要手動建立配置檔案
[root@localhost ~]# vim /etc/rsyncd.conf
motd file = /etc/rsyncd.motd #描述檔案
#全局配置
uid = nobody
gid = nobody
use chroot = no #開啟的話需要root使用者
max connections = 4 #最大連接配接數
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsync.log
[www] #子產品1,可以定義多個
path = /var/www/html #備份的目錄
comment = www server
read only = true #隻讀
list = false
hosts allow = 192.168.0.0/24 #允許的位址
hosts deny = 0.0.0.0/32
auth users = zhaoyun #同步時用到的使用者
secrets file = /etc/rsyncd.secrets #使用者密碼檔案
[mysql] #子產品2
path = /var/lib/mysql
comment = mysql data backup
read only = true
list = false
auth users = zhao
secrets file = /etc/rsyncd.secrets
#建立使用者密碼檔案
[root@localhost ~]# echo zhaoyun:123456 > /etc/rsyncd.secrets
#歡迎資訊
[root@localhost ~]# echo "歡迎使用rsync軟體同步資料" > /etc/rsyncd.motd
修改配置檔案權限,所有配置檔案使用600的權限。
[root@localhost ~]# chmod 600 /etc/rsyncd.*
好了,服務端配置完成,重新開機服務,使配置生效。
[root@localhost ~]# service xinetd restart
Stopping xinetd: [ OK ]
Starting xinetd: [ OK ]
用戶端測試:
使用方法1:
[root@localhost-backup ~]#mkdir /backup-www
[root@localhost-backup ~]# rsync -av --progress [email protected]::www /backup-www/
歡迎使用rsync軟體同步資料
Password: 輸入密碼
receiving incremental file list
./
index.html
0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=0/2)
sent 78 bytes received 192 bytes 77.14 bytes/sec
total size is 0 speedup is 0.00
[root@localhost-backup backup-www]# ls
可以看到同步了一個檔案。 index.html
使用方法2:
使用自動輸入密碼的方式
建立密碼檔案,權限需要改為600,否則不能生效。
[root@localhost-backup backup-www]# echo 123456 >/etc/rsync.pass
[root@localhost-backup backup-www]# rsync -av --progress --password-file=/etc/rsync.pass [email protected]::www /backup-www/
password file must not be other-accessible
continuing without password file
Password:
@ERROR: auth failed on module www
rsync error: error starting client-server protocol (code 5) at main.c(1503) [receiver=3.0.6]
[root@localhost-backup backup-www]#
[root@localhost-backup backup-www]# chmod 600 /etc/rsync.pass
[root@localhost-backup backup-www]# rsync -av --progress --password-file=/etc/rsync.pass [email protected]::www /backup-www/
sent 78 bytes received 192 bytes 540.00 bytes/sec
以上的同步做的是,将用戶端在服務端沒有的檔案,就是說隻做的是增加檔案後的同步。服務端删除檔案後不會同步。
如果要和服務端資料保證一緻性,就需要使用下面的方式同步了。
測試:
使用方法3:
首先将服務端的index.html檔案删除,再在用戶端做測試。
[root@localhost-backup backup-www]# rsync -av --delete --password-file=/etc/rsync.pass [email protected]::www /backup-www/
receiving incremental file list
deleting index.html 這裡顯示删除掉index.html檔案。
sent 56 bytes received 131 bytes 374.00 bytes/sec
[root@localhost-backup backup-www]# ls #再檢視檔案已經沒有了,因為在服務端已經删除了。
[root@localhost-backup backup-www]#
二、基于ssh同步,不用配置服務端。
1、 建立ssh信任,不需要密碼認證,建立密鑰對。
使用文法
rsync -av --delete [email protected]:/var/www/html /backup-www/
rsync -Rav [email protected]:/var/www/html /backup-www/
rsync -Rav -e 'ssh -p 2208' [email protected]:/var/www/html /backup
三、使用任務計劃實作自動同步
[root@localhost-backup backup-www]# crontab -e
*/10 * * * * rsync -az --delete [email protected]:/var/www/html /backup-www/
本文轉自zhaoyun00 51CTO部落格,原文連結:http://blog.51cto.com/zhaoyun/829443