同步windows伺服器軟體至linux伺服器實現
1、 一般linux上預設安裝有rsync軟體
一.查看及安裝
查看是否安裝rsync
命令# rpm –qa | grep rsync
<!--[endif]-->
假如出現對應的rsync版本,則說明對應的linux上已有rsync。
否則,需要手動下載安裝。具體的方式有
軟體安裝過於簡單,現在Linux各大發行版都提供這個軟體包,當然您也可以自己編譯安裝,在目前的情況下,我看沒太大的必要;
[root@linuxsir:beinan]$ sudo apt-get install rsync 註:在debian、ubuntu 等線上安裝方法;
[root@linuxsir:beinan]# slackpkg install rsync 註:Slackware軟體包線上安裝;
[root@linuxsir:beinan]# yum install rsync 註:Fedora、Redhat等系統安裝方法;
其它Linux發行版,請用相應的軟體包管理方法來安裝;如果是源碼包,也就是用下面的辦法;
[root@linuxsir:/home/beinan]# tar xvf sync-xxxx.tar.gz 或sync-xxx.tar.bz2
[root@linuxsir:/home/beinan]# cd sync-xxx
[root@linuxsir:/home/beinan/sync-xxx]# ./configure --prefix=/usr ;make ;make install 註:在用源碼包編譯安裝之前,您得安裝gcc等編譯開具才行;
二修改rsync的設定檔
可以看到rysnc服務是關閉的(disable = yes),這裡把它開啟,把disable的值改為no
三、rsync伺服器的配置文件rsyncd.conf ;
我們可以參照rsyncd.conf.html。具體步驟如下;
[root@linuxsir:~]#mkdir /etc/rsyncd 註:在/etc目錄下建立一個rsyncd的目錄,我們用來存放rsyncd.conf 和rsyncd.secrets檔案;
[root@linuxsir:~]#touch /etc/rsyncd/rsyncd.conf 註:建立rsyncd.conf ,這是rsync伺服器的設定檔;
[root@linuxsir:~]#touch /etc/rsyncd/rsyncd.secrets 註:建立rsyncd.secrets ,這是使用者密碼檔案;
[root@linuxsir:~]#chmod 600 /etc/rsyncd/rsyncd.secrets 註:為了密碼的安全性,我們把許可權設為600;必須
[root@linuxsir:~]#ls -lh /etc/rsyncd/rsyncd.secrets
-rw------- 1 root root 14 2007-07-15 10:21 /etc/rsyncd/rsyncd.secrets
[root@linuxsir:~]#touch /etc/rsyncd/rsyncd.motd
下一就是我們修改 rsyncd.conf 和rsyncd.secrets 和rsyncd.motd 檔案的時候了;
rsyncd.conf 是rsync伺服器主要設定檔,我們來個簡單的樣本;比如我們要備份伺服器上的 /home 和/opt ,在/home中,我想把beinan和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
uid = root #這個使用者是系統使用者,當rsync用戶端串連上伺服器後,會映射成這個使用者上傳或下載
use chroot = no
max connections = 4 #最大允許並法連結數
strict modes = yes
port = 873 #rsync服務對應的連接埠
[demo] ## 模組名字,自己命名必須指定且唯一
path = /home/pigga/test #需同步的檔案夾
comment = This is test
auth users = scihoo #rsync的使用者名稱是用戶端使用的,串連成功後會映射到上面的uid使用者
uid = root #這個使用者是系統使用者,當rsync用戶端串連上伺服器後,會映射成這個使用者上傳或下載
gid = root #組名效果同上
secrets file = /etc/rsyncd/rsyncd.secrets #密碼所在檔案
read only = no #不是唯讀模式這樣使用者就有上傳的許可權了
list = yes #使用者具有list目錄的許可權,上傳之後的目錄可見,且列表展示
hosts allow = * #允許所有網段的地址串連至伺服器,可以指定具體的ip以/分割
註:關於 auth users 是必須在伺服器上存在的真實的系統使用者,如果你想用多個使用者,那就以,號隔開;比如 auth users = beinan , linuxsir
密碼檔案:/etc/rsyncd/rsyncd.secrets的內容格式;
使用者名稱:密碼 例:
Scihoo:scihoo
註:這裡的密碼值得注意,為了安全,你不能把系統使用者的密碼寫在這裡。比如你的系統使用者 linuxsir 密碼是 abcdefg ,為了安全,你可以讓rsync 中的linuxsir 為 222222 。這和samba的使用者認證的密碼原理是差不多的;
rsyncd.motd 檔案;
它是定義rysnc 伺服器資訊的,也就是使用者登入資訊。比如讓使用者知道這個伺服器是誰提供的等;類似ftp伺服器登入時,我們所看到的 linuxsir.org ftp ……。 當然這在全域定義變數時,並不是必須的,你可以用#號注掉,或刪除;我在這裡寫了一個 rsyncd.motd的內容為:
+++++++++++++++++++++++++++
+ linuxsir.org rsync 2002-2007 +
+++++++++++++++++++++++++++
四.啟動rsync 伺服器及防火牆的設定;
4.1 啟動rsync伺服器;
啟動rsync 伺服器相當簡單,–daemon 是讓rsync 以伺服器模式運行;
[root@linuxsir:~]#/usr/bin/rsync --daemon --config=/etc/rsyncd/rsyncd.conf
註:如果你找不到rsync 命令,你應該知道rsync 是安裝在哪了。比如rsync 可執行命令可能安裝在了 /usr/local/rsync/bin/rsync目錄;也就是如下的命令;
[root@linuxsir:~]#/usr/local/rsync/bin/rsync --daemon --config=/etc/rsyncd/rsyncd.conf
4.2 rsync伺服器和防火牆;
Linux 防火牆是用iptables,所以我們至少在伺服器端要讓你所定義的rsync 伺服器連接埠通過,用戶端上也應該讓通過。
[root@linuxsir:~]#iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 873 -j ACCEPT
[root@linuxsir:~]#iptables -L 查看一下防火牆是不是開啟了 873連接埠;
我在這裡通過圖形介面在防火牆上添加連接埠的方式。
查看是否開啟了對應連接埠 也可以使用netstat –an 然後在另外的機器使用telnet判斷是否可以通過防火牆。
另:查看系統進程。Ps –ef,查看是否開啟rsync
2、 window用戶端
在windows機器上安裝cwRsyncServer用戶端,本地使用cwRsyncServer_4.0.6_Installer.zip。可在互連網下載;
安裝完畢後,我們寫個批處理來實現下載和上傳的功能
上傳:即將window指定路徑的檔案上傳至linux
<!--[if !supportLineBreakNewLine]-->
<!--[endif]-->
@ECHO OFF
d:
cd "Program Files\cwRsyncServer\bin"
rsync -vzrtopg –progress --delete /cygdrive/d/websoft/ scihoo@192.168.1.43::demo --password-file=/cygdrive/d/rsyncd.secrets
其中/cygdrive/e/表示的是windows的E盤
註:該部分d/websoft/加上/為不包含websoft的上傳,去掉/為帶有websoft檔案夾的上傳
d:/rsyncd.secrets的密碼檔案,裡面只包含密碼,不包含使用者
下載:即從linux伺服器指定路徑下載到window用戶端
<!--[if !supportLineBreakNewLine]-->
<!--[endif]-->
@ECHO OFF
d:
cd "Program Files\cwRsyncServer\bin"
rsync -vrtopg --progress --delete scihoo@192.168.1.43::demo /cygdrive/d/websoft/
3、密碼每次都需要手動輸入的解決方案
這算是個老問題了,每次在windows主機上通過cwrsync向服務端同步資料的時候都會遇到,這次總結記錄下吧。錯誤碼為: