歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> 關於Linux >> RSync實現文件備份同步

RSync實現文件備份同步

日期:2017/3/2 9:55:01   编辑:關於Linux

rsync是類unix系統下的數據鏡像備份工具,從軟件的命名上就可以看出來了——remote sync。它的特性如下:
1、可以鏡像保存整個目錄樹和文件系統。
2、可以很容易做到保持原來文件的權限、時間、軟硬鏈接等等。
3、無須特殊權限即可安裝。
4、優化的流程,文件傳輸效率高。
5、可以使用rcp、ssh等方式來傳輸文件,當然也可以通過直接的socket連接。
6、支持匿名傳輸。

rsync實現網站的備份,文件的同步,不同系統的文件的同步,如果是windows的話,需要windows版本cwrsync]
一、什麼是rsync
rsync,remote synchronize顧名思意就知道它是一款實現遠程同步功能的軟件,它在同步文件的同時,可以保持原來文件的權限、時間、軟硬鏈接等附加信息。 rsync是用 “rsync 算法”提供了一個客戶機和遠程文件服務器的文件同步的快速方法,而且可以通過ssh方式來傳輸文件,這樣其保密性也非常好,另外它還是免費的軟件。
rsync 包括如下的一些特性:
能更新整個目錄和樹和文件系統;
有選擇性的保持符號鏈鏈、硬鏈接、文件屬於、權限、設備以及時間等;
對於安裝來說,無任何特殊權限要求;
對於多個文件來說,內部流水線減少文件等待的延時;
能用rsh、ssh 或直接端口做為傳輸入端口;
支持匿名rsync 同步文件,是理想的鏡像工具;
二、架設rsync服務器
架設rsync 服務器比較簡單,寫一個配置文件rsyncd.conf 。文件的書寫也是有規則的,我們可以參照rsync.samba.org 上的文檔來做。當然我們首先要安裝好rsync這個軟件才行;
A、rsync的安裝;
獲取rsync
rysnc的官方網站:http://rsync.samba.org/可以從上面得到最新的版本。目前最新版是3.05。當然,因為rsync是一款如此有用的軟件,所以很多Linux的發行版本都將它收錄在內了。
軟件包安裝


# sudo apt-get install rsync 注:在debian、ubuntu 等在線安裝方法;
# yum install rsync 注:Fedora、Redhat 等在線安裝方法;
# rpm -ivh rsync 注:Fedora、Redhat 等rpm包安裝方法;

其它Linux發行版,請用相應的軟件包管理方法來安裝。
源碼包安裝


tar xvf rsync-xxx.tar.gz
cd rsync-xxx
./configure --prefix=/usr
make
make install

注:在用源碼包編譯安裝之前,您得安裝gcc等編譯開具才行;

B、配置文件
rsync的主要有以下三個配置文件rsyncd.conf(主配置文件)、rsyncd.secrets(密碼文件)、rsyncd.motd(rysnc服務器信息)
服務器配置文件(/etc/rsyncd.conf),該文件默認不存在,請創建它。
具體步驟如下:


#touch /etc/rsyncd.conf #創建rsyncd.conf,這是rsync服務器的配置文件。
#touch /etc/rsyncd.secrets #創建rsyncd.secrets ,這是用戶密碼文件。
#chmod 600 /etc/rsyncd/rsyncd.secrets
#將rsyncd.secrets這個密碼文件的文件屬性設為root擁有, 且權限要設為600, 否則無法備份成功!
#touch /etc/rsyncd.motd

下一就是我們修改rsyncd.conf和rsyncd.secrets和rsyncd.motd文件的時候了。
設定/etc/rsyncd.conf
rsyncd.conf是rsync服務器主要配置文件。我們先來個簡單的示例,後面在詳細說明各項作用。
比如我們要備份服務器上的/home和/opt,在/home中我想把easylife和samba目錄排除在外;


# Distributed under the terms of the GNU General Public License v2
# Minimal configuration file for rsync daemon
# See rsync(1) and rsyncd.conf(5) man pages for help
# This line is required by the /etc/init.d/rsyncd script
pid file = /var/run/rsyncd.pid
port = 873
address = 192.168.1.171
#uid = nobody
#gid = nobody
uid = root
gid = root
use chroot = yes
read only = yes
#limit access to private LANs
hosts allow=192.168.1.0/255.255.255.0 10.0.1.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
[rhel4home]
path = /home
list=yes
ignore errors
auth users = root
secrets file = /etc/rsyncd.secrets
comment = This is RHEL 4 data
exclude = easylife/ samba/
[rhel4opt]
path = /opt
list=no
ignore errors
comment = This is RHEL 4 opt
auth users = easylife
secrets file = /etc/rsyncd/rsyncd.secrets

注:關於auth users是必須在服務器上存在的真實的系統用戶,如果你想用多個用戶以,號隔開,
比如auth users = easylife,root
設定密碼文件
密碼文件格式很簡單,rsyncd.secrets的內容格式為:
用戶名:密碼
我們在例子中rsyncd.secrets的內容如下類似的;在文檔中說,有些系統不支持長密碼,自己嘗試著設置一下吧。
easylife:keer
root:mike
chown root.root rsyncd.secrets  #修改屬主
chmod 600 rsyncd.secrets #修改權限
注:1、將rsyncd.secrets這個密碼文件的文件屬性設為root擁有, 且權限要設為600, 否則無法備份成功! 出於安全目的,文件的屬性必需是只有屬主可讀。
2、這裡的密碼值得注意,為了安全你不能把系統用戶的密碼寫在這裡。比如你的系統用戶easylife密碼是000000,為了安全你可以讓rsync中的easylife為keer。這和samba的用戶認證的密碼原理是差不多的。
設定rsyncd.motd 文件;
它是定義rysnc服務器信息的,也就是用戶登錄信息。比如讓用戶知道這個服務器是誰提供的等;類似ftp服務器登錄時,我們所看到的 linuxsir.org ftp ……。 當然這在全局定義變量時,並不是必須的,你可以用#號注掉,或刪除;我在這裡寫了一個 rsyncd.motd的內容為:
++++++++++++++++++++++++++++++++++++++++++++++
Welcome to use the mike.org.cn rsync services!
2002——2009
++++++++++++++++++++++++++++++++++++++++++++++
三、rsyncd.conf服務器的配置詳解
A、全局定義
在rsync 服務器中,全局定義有幾個比較關健的,根據我們前面所給的配置文件 rsyncd.conf 文件;
pid file = /var/run/rsyncd.pid 注:告訴進程寫到 /var/run/rsyncd.pid 文件中;
port = 873 注:指定運行端口,默認是873,您可以自己指定;
address = 192.168.1.171 注:指定服務器IP地址
uid = nobody
gid = nobdoy
注:服務器端傳輸文件時,要發哪個用戶和用戶組來執行,默認是nobody。 如果用nobody 用戶和用戶組,可能遇到權限問題,有些文件從服務器上拉不下來。所以我就偷懶,為了方便,用了root 。不過您可以在定義要同步的目錄時定義的模塊中指定用戶來解決權限的問題。
use chroot = yes
注:用chroot,在傳輸文件之前,服務器守護程序在將chroot 到文件系統中的目錄中,這樣做的好處是可能保護系統被安裝漏洞侵襲的可能。缺點是需要超級用戶權限。另外對符號鏈接文件,將會排除在外。也就是說,你在 rsync服務器上,如果有符號鏈接,你在備份服務器上運行客戶端的同步數據時,只會把符號鏈接名同步下來,並不會同步符號鏈接的內容;這個需要自己來嘗試
read only = yes
注:read only 是只讀選擇,也就是說,不讓客戶端上傳文件到服務器上。還有一個 write only選項,自己嘗試是做什麼用的吧;
#limit access to private LANs
hosts allow=192.168.1.0/255.255.255.0 10.0.1.0/255.255.255.0
注:在您可以指定單個IP,也可以指定整個網段,能提高安全性。格式是ip 與ip 之間、ip和網段之間、網段和網段之間要用空格隔開;
max connections = 5
注:客戶端最多連接數
motd file = /etc/rsyncd/rsyncd.motd
注:motd file 是定義服務器信息的,要自己寫 rsyncd.motd 文件內容。當用戶登錄時會看到這個信息。比如我寫的是:

<span style="color: rgb(51, 153, 51);">++++++++++++++++++++++++++++++++++++++++++++++</span>
Welcome to use the mike.<span style="color: rgb(32, 32, 32);">org</span>.<span style="color: rgb(32, 32, 32);">cn</span> rsync services<span style="color: rgb(51, 153, 51);">!</span>
<span>2002</span><span style="color: rgb(51, 153, 51);">------</span><span>2009</span>
<span style="color: rgb(51, 153, 51);">++++++++++++++++++++++++++++++++++++++++++++++</span>
log file <span style="color: rgb(51, 153, 51);">=</span> <span style="color: rgb(51, 153, 51);">/</span>var<span style="color: rgb(51, 153, 51);">/</span>log<span style="color: rgb(51, 153, 51);">/</span>rsync.<span style="color: rgb(32, 32, 32);">log</span>

注:rsync 服務器的日志;
transfer logging = yes
注:這是傳輸文件的日志

log format <span style="color: rgb(51, 153, 51);">=</span> <span style="color: rgb(51, 153, 51);">%</span>t <span style="color: rgb(51, 153, 51);">%</span>a <span style="color: rgb(51, 153, 51);">%</span>m <span style="color: rgb(51, 153, 51);">%</span>f <span style="color: rgb(51, 153, 51);">%</span>b
syslog facility <span style="color: rgb(51, 153, 51);">=</span> local3
timeout <span style="color: rgb(51, 153, 51);">=</span> <span>300</span>

Copyright © Linux教程網 All Rights Reserved