標籤:des 使用 os io 檔案 資料 for ar
什麼是rsync
rsync 是一個快速增量檔案傳輸工具,它可以用於在同一主機備份內部的備分,我們還可以把它作為不同主機網路備份工具之用。本文主要講述的是如何自架rsync伺服器,以實現檔案傳輸、備份和鏡像。相對tar和wget來說,rsync 也有其自身的優點,比如速度快、安全、高效。
rsync的安裝
在CentOS伺服器,我們可以執行以下命令安裝
yum install rsync
對於debian、ubuntu伺服器,則是以下命令
sudo apt-get install rsync
rsync伺服器的設定檔rsyncd.conf
下面我們將涉及到三個檔案 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檔案內容:
# 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.0.231
#uid = nobody
#gid = nobody
uid = root
gid = root
use chroot = yes
read only = no
#limit access to private LANs
hosts allow=192.168.0.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
[webuserFile]
path = /data/tool
list=yes
ignore errors
auth users = webuser
secrets file = /etc/rsyncd/rsyncd.secrets
comment = wangxing directory
#exclude = beinan/ samba/
[mysqlFile]
path = /data/tool/rsyncd
list=no
ignore errors
comment = mysql directory
auth users = nouserjken
secrets file = /etc/rsyncd/rsyncd.secrets
密碼檔案:/etc/rsyncd/rsyncd.secrets的內容格式:
使用者名稱:密碼
webuser:222222
nouserjken:111
說明一下,這裡的webuser可以是系統使用者也可以不是,這裡定義的使用者只提供rsyncd使用,所以,和系統使用者關係不大
rsyncd.motd 檔案內容:
+++++++++++++++++++++++++++++++++++++
+ xinfilm.com rsync 2012-2017 +
+++++++++++++++++++++++++++++++++++++
rsyncd.conf檔案代碼說明
pid file = /var/run/rsyncd.pid
註:告訴進程寫到 /var/run/rsyncd.pid 檔案中;
port = 873
註:指定運行連接埠,預設是873,您可以自己指定;
address = 192.168.0.231
註:指定伺服器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.0.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 檔案內容。當使用者登入時會看到這個資訊。
log file = /var/log/rsync.log
註:rsync 伺服器的日誌;
transfer logging = yes
註:這是傳輸檔案的日誌;
[webuserFile]
註:模組,它為我們提供了一個連結的名字,連結到哪呢,在本模組中,連結到了/home目錄;要用[name] 形式;
path = /home
註:指定檔案目錄所在位置,這是必須指定的;
auth users = webuser
註:認證使用者是webuser ,是必須在 伺服器上存在的使用者;
list=yes
註:list 意思是把rsync 伺服器上提供同步資料的目錄在伺服器上模組是否顯示列出來。預設是yes 。如果你不想列出來,就no ;如果是no是比較安全的,至少別人不知道你的伺服器上提供了哪些目錄。你自己知道就行了;
ignore errors
註:忽略IO錯誤,詳細的請查文檔;
secrets file = /etc/rsyncd/rsyncd.secrets
註:密碼存在哪個檔案;
comment = webuser home data
註:注釋可以自己定義,寫什麼都行,寫點相關的內容就行;
exclude = beinan/ samba/
註:exclude 是排除的意思,也就是說,要把/home目錄下的beinan和samba 排除在外; beinan/和samba/目錄之間有空格分開 ;
啟動rsync 伺服器及防火牆的設定
啟動rsync伺服器
啟動rsync 伺服器相當簡單,--daemon 是讓rsync 以伺服器模式運行;
/usr/bin/rsync --daemon --config=/etc/rsyncd/rsyncd.conf
rsync伺服器和防火牆
Linux 防火牆是用iptables,所以我們至少在伺服器端要讓你所定義的rsync 伺服器連接埠通過,用戶端上也應該讓通過。
iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 873 -j ACCEPT
查看一下防火牆是不是開啟了 873連接埠;
iptables -L
因為rsync是超級服務,所以需要通過xinetd進行啟動。
(這裡,當然可以直接加入/etc/rc.local命令方式進行開機啟動)
每台rsync主機需要安裝和配置xinet服務:
yum -y install xinetd
//配置rsync服務
vi /etc/xinetd.d/rsync
# default: off
# description: The rsync server is a good addition to an ftp server, as it \
# allows crc checksumming etc.
service rsync
{
disable = no
flags = IPv6
socket_type = stream
wait = no
user = root
server = /usr/bin/rsync
server_args = --daemon --config=/etc/rsyncd.conf #指定rsync的設定檔路徑
log_on_failure += USERID
}
通過rsync用戶端來同步資料
rsync -avzP [email protected]::webuserFile webuserFile
Password: 這裡要輸入webuser的密碼,是伺服器端提供的,在前面的例子中,我們用的是 222222,輸入的密碼並不顯示出來;輸好後就斷行符號;
註: 這個命令的意思就是說,用webuser 使用者登入到伺服器上,把webuserFile資料,同步到本地目錄webuserFile上。當然本地的目錄是可以你自己定義的,比如 webuser也是可以的;當你在用戶端上,當前操作的目錄下沒有webuserFile這個目錄時,系統會自動為你建立一個;當存在webuserFile這個目錄中,你要注意它的寫入權限。
說明:
-a 參數,相當於-rlptgoD,-r 是遞迴 -l 是連結檔案,意思是拷貝連結檔案;-p 表示保持檔案原有許可權;-t 保持檔案原有時間;-g 保持檔案原有使用者組;-o 保持檔案原有屬主;-D 相當於塊裝置檔案;
-z 傳輸時壓縮;
-P 傳輸進度;
-v 傳輸時的進度等資訊,和-P有點關係,自己試試。可以看文檔;
rsync -avzP --delete [email protected]::webuserFile webuserFile
這回我們引入一個 --delete 選項,表示用戶端上的資料要與伺服器端完全一致,如果 webuserFile目錄中有伺服器上不存在的檔案,則刪除。最終目的是讓webuserFile目錄上的資料完全與伺服器上保持一致;用的時候要小心點,最好不要把已經有重要數所據的目錄,當做本地更新目錄,否則會把你的資料全部刪除;
rsync -avzP --delete --password-file=rsync.password [email protected]::webuserFile webuserFile
這次我們加了一個選項 --password-file=rsync.password ,這是當我們以webuser使用者登入rsync伺服器同步資料時,密碼將讀取 rsync.password 這個檔案。這個檔案內容只是webuser使用者的密碼。我們要如下做;
touch rsync.password
chmod 600 rsync.password
上面的這個使用權限設定是必須的,否則不能使用這個密碼檔案
echo "222222"> rsync.password
rsync -avzP --delete --password-file=rsync.password [email protected]::webuserFile webuserFile
註: 這樣就不要求輸入密碼了;其實這是比較重要的,因為伺服器通過crond 計劃任務還是有必要的.