我們在使用伺服器釋出我們的網站的時候,通常要考慮到檔案的備份,而檔案的備份比較高效的備份是增加備份,rsync軟體就是這樣的一個工具。為了實作多個伺服器負載均衡,我們需要這幾個伺服器之間進行資料同步,而rsync軟體也能勝任,下面我們來介紹如何架設rsync伺服器來達到檔案增量備份和資料同步的功能。
rsync 是一個快速增量檔案傳輸工具,它可以用于在同一主機備份内部的備分,我們還可以把它作為不同主機網絡備份工具之用。本文主要講述的是如何自架rsync伺服器,以實作檔案傳輸、備份和鏡像。相對tar和wget來說,rsync 也有其自身的優點,比如速度快、安全、高效。
在CentOS伺服器,我們可以執行以下指令安裝
yum install rsync
對于debian、ubuntu伺服器,則是以下指令
sudo apt-get install rsync
下面我們将涉及到三個檔案 rsyncd.conf,rsyncd.secrets 和rsyncd.motd。
rsyncd.conf 是rsync伺服器主要配置檔案。
rsyncd.secrets是登入rsync伺服器的密碼檔案。
rsyncd.motd是定義rysnc 伺服器資訊的,也就是使用者登入資訊。
下面我們分别建立這三個檔案。
mkdir /etc/rsyncd
注:在/etc目錄下建立一個rsyncd的目錄,我們用來存放rsyncd.conf 和rsyncd.secrets檔案;
touch /etc/rsyncd/rsyncd.conf
注:建立rsyncd.conf ,這是rsync伺服器的配置檔案;
touch /etc/rsyncd/rsyncd.secrets
注:建立rsyncd.secrets ,這是使用者密碼檔案;
chmod 600 /etc/rsyncd/rsyncd.secrets
注:為了密碼的安全性,我們把權限設為600;
touch /etc/rsyncd/rsyncd.motd
注:建立rsyncd.motd檔案,這是定義伺服器資訊的檔案。
下一就是我們修改 rsyncd.conf 和rsyncd.secrets 和rsyncd.motd 檔案的時候了。
rsyncd.conf檔案内容:
# 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/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
[linuxsirhome]
path = /home
list=yes
ignore errors
auth users = linuxsir
secrets file = /etc/rsyncd/rsyncd.secrets
comment = linuxsir home
exclude = beinan/ samba/
[beinan]
path = /opt
list=no
comment = optdir
auth users = beinan
密碼檔案:/etc/rsyncd/rsyncd.secrets的内容格式;
使用者名:密碼
linuxsir:222222
beinan:333333
注: 這裡的密碼值得注意,為了安全,你不能把系統使用者的密碼寫在這裡。比如你的系統使用者 linuxsir 密碼是 abcdefg ,為了安全,你可以讓rsync 中的linuxsir 為 222222 。這和samba的使用者認證的密碼原理是差不多的;
rsyncd.motd 檔案;
它是定義rysnc 伺服器資訊的,也就是使用者登入資訊。比如讓使用者知道這個伺服器是誰提供的等;類似ftp伺服器登入時,我們所看到的 linuxsir.org ftp ……。 當然這在全局定義變量時,并不是必須的,你可以用#号注掉,或删除;我在這裡寫了一個 rsyncd.motd的内容為:
+++++++++++++++++++++++++++
+ linuxsir.org rsync 2002-2007 +