1. 程式人生 > 實用技巧 >實用工具使用

實用工具使用

wget使用

wget下載帶遞迴時出現錯誤,頂層不讓你遞迴!

wget url //下載該網頁將其儲存為index.html檔案
wget -i index.html -F -B url //解析index.html檔案將其的連結加上url進行下載

wget下載src_link網站中所有的rpm格式的檔案

wget -r -c -np -nd $src_link --accept=rpm  --directory-prefix=$dirname

rsync+rsync同步和網站實時映象

本文連結:https://blog.csdn.net/Z_xiao_feng/article/details/90270467

2.rsync+rsync同步

採用rsync+SSH的遠端同步時,使用起來是最簡單的,但是目標使用者也被允許SSH登入到遠端主機的Shell環境。在某些情況下,企業會希望只提供需要認證的同步目錄資源,但並不希望提供遠端登入,這時候就可以採用rsync+rsync同步。
沿用練習一,需要完成的配置任務如下:
1)在rsync源端將/usr/src 目錄釋出為同步資源:共享名為tools,僅允許使用者 ruser 以密碼 pwd123 訪問
2)在rsync操作端測試 rsync+rsync下行同步
使用兩臺RHEL6虛擬機器,其中一臺為rsync同步提供源目錄(192.168.4.5),另外一臺作為rsync同步操作的發起端(192.168.4.205)
在這裡插入圖片描述

實現此案例需要按照如下步驟進行。
步驟一: 配置rsync服務端,釋出tools同步資源
1 建立同步賬號檔案

[root@svr5 ~]# vim  /etc/rsyncd_users.db
ruser:pwd123  									//使用者名稱:密碼,每行一個使用者
othername:123456

[root@svr5 ~]# chmod  600  /etc/rsyncd_users.db  	//嚴格許可權,否則同步會失敗

2 建立 /etc/rsyncd.conf 共享設定

[root@svr5 ~]# vim  /etc/rsyncd.conf
[tools]  											//定義共享名
    path = /usr/src  								//被共享的目錄位置
    comment = Rsync Share Test  					//同步資源說明
    read only = yes  								//只讀
    dont compress = *.gz *.bz2 *.tgz *.zip  		//同步時不再壓縮的文件型別
    auth users = ruser  							//允許誰訪問
    secrets file = /etc/rsyncd_users.db  			//指定賬號檔案的路徑

在上述配置檔案中,若不新增最後兩行認證配置,則預設以匿名方式提供。
3 啟用 rsync --daemon 服務端

**[root@svr5 ~]# du -sh /usr/src/  					//確認待發布的同步目錄
163M    /usr/src/
[root@svr5 ~]# yum  -y  install  xinetd
[root@svr5 ~]# chkconfig  rsync  on  				//開啟rsync服務開關
[root@svr5 ~]# chkconfig  xinetd  on
[root@svr5 ~]# service  xinetd  restart  			//通過xinetd啟動**

步驟二: rsync + rsync下行同步測試
1 檢視及列表同步資源
檢視遠端主機提供了哪些同步資源:

[root@pc205 ~]# rsync 192.168.4.5::
tools           Rsync Share Test 					//共享名、共享說明
列出指定同步資源下的文件:
[root@pc205 ~]# rsync  [email protected]::tools  	//瀏覽共享
Password:  										//驗證ruser使用者的口令
drwxr-xr-x        4096 2009/10/01 22:58:39 debug
drwxr-xr-x        4096 2009/10/01 22:58:39 kernels
.. ..

2)rsync下行同步

[root@pc205 ~]# rsync -avz  [email protected]::tools/  /root/mysrc/ 
													//下行同步,刪除多餘檔案
Password:  										//驗證密碼pwd123
.. .. 
sent 271848 bytes  received 37119880 bytes  598267.65 bytes/sec
total size is 130075707  speedup is 3.48

[root@pc205 ~]# du -sh /root/mysrc/  				//確認同步結果
163M    /root/mysrc/

3.網站實時映象

公司的網站伺服器有兩個映象站點,分別放在北京和上海的IDC機房。現在要求利用rsync同步機制實現“伺服器A–>伺服器B”的實時映象同步。
需要完成的配置任務如下:
1 雙方的目錄均為 /var/www/html/
2 以 svr5 為同步發起方,配置 inotify+rsync 同步操作
3 以 pc205 為同步目標,基於SSH方式進行驗證
使用兩臺RHEL6虛擬機器,其中一臺作為伺服器A(192.168.4.5),另外一臺作為伺服器B(192.168.4.205),兩臺主機都安裝httpd網站軟體
在這裡插入圖片描述

安裝並啟用inotify-tools工具,就可以在同步發起端實現對指定目錄的監控,一旦出現更改、增加檔案等操作,立即觸發相應的命令操作(本例中即上行同步)。根據監控結果觸發同步操作,其中用到了一部分Shell控制語句,最好建立專用指令碼來實現,本例中只需理解指令碼的用途即可。
實現此案例需要按照如下步驟進行。
步驟一:準備網頁環境
1 在svr5上,啟用httpd網站服務、部署測試網頁

[root@svr5 ~]# yum -y install httpd
.. ..
[root@svr5 ~]# service httpd restart
停止 httpd:                                               [確定]
正在啟動 httpd:                                           [確定]
[root@svr5 ~]# chkconfig httpd on
[root@svr5 ~]# echo "Welcome to Tarena" > /var/www/html/index.html
[root@svr5 ~]# elinks -dump http://192.168.4.5  	//訪問測試網頁
   Welcome to Tarena

2 在pc205上,啟用httpd網站服務,先不用部署網頁

[root@pc205 ~]# yum -y install httpd
.. ..
[root@pc205 ~]# service httpd restart
停止 httpd:                                               [確定]
正在啟動 httpd:                                           [確定]
[root@pc205 ~]# chkconfig httpd on

[root@pc205 ~]# ls /var/www/html/*  			//網頁目錄為空
ls: 無法訪問/var/www/html/*: 沒有那個檔案或目錄

步驟二:配置、啟用實時同步指令碼

1 在svr5上,安裝inotify-tools工具包

[root@svr5 ~]# tar  xf  inotify-tools-3.13.tar.gz
[root@svr5 ~]# cd  inotify-tools-3.13
[root@svr5 inotify-tools-3.13]# ./configure
.. ..
[root@svr5 ~]# make  &&  make  install

2)建立並部署SSH公鑰,實現免密碼驗證

[root@svr5 ~]# ssh-keygen  								//建立金鑰對
Generating public/private rsa key pair. 
Enter file in which to save the key (/root/.ssh/id_rsa):  		//回車
Enter passphrase (empty for no passphrase):  					//回車
Enter same passphrase again:  									//回車
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
.. ..

[root@svr5 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub [email protected]
  														//上傳公鑰
[email protected]'s password:  							//驗證對方密碼
Now try logging into the machine, with "ssh '[email protected]'", and check in:

  .ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

[root@svr5 ~]# ssh [email protected]  					//驗證免密碼登入
Last login: Thu Dec 24 00:53:00 2015 from 192.168.4.5
[root@pc205 ~]# exit 										//返回客戶機
logout
Connection to 192.168.4.205 closed.

3 建立inotify實時同步指令碼檔案
為了方便指令碼的移植使用,在指令碼中定義了兩個變數:TARGET_DIR用來指定監控的目標資料夾,而RSYNC_CMD用來指定需要觸發的同步操作。注意給指令碼新增x執行許可權,實際使用時根據需要變更這兩個變數的值即可

[root@svr5 ~]# vim  /root/isync.sh  				//新建指令碼
#!/bin/bash
TARGET_DIR="/var/www/html"   						#//指定監控目錄
RSYNC_CMD="rsync  -az  --delete  /var/www/html/  192.168.4.205:/var/www/html/"
    												#//指定同步操作
inotifywait  -mrq  -e  modify,move,create,delete,attrib  /opt | while read  -n5  X 
do
    $RSYNC_CMD
done  &

[root@svr5 ~]# chmod  +x  /root/isync.sh  			//新增執行許可權

4 啟動實時同步指令碼程式
此指令碼一旦執行後,會一直在後臺執行;如果有必要,可以將此指令碼新增為開機自啟動任務。

[root@svr5 ~]# /root/isync.sh  					//執行指令碼
[root@svr5 ~]#

步驟三:測試實時同步效果

1 在svr5上向/var/www/html/目錄下新增一個檔案

[root@svr5 ~]# touch /var/www/html/a.html
[root@svr5 ~]# ls -lh /var/www/html/*.html
-rw-r–r--. 1 root root 0 12月 17 09:02 /var/www/html/a.html
-rw-r–r--. 1 root root 18 12月 17 08:37 /var/www/html/index.html

2 在pc205上觀察/var/www/html目錄下的變化

[root@pc205 ~]# ls -lh /var/www/html/*.html
-rw-r--r--. 1 root root  0 12月 17 09:02 /var/www/html/a.html
-rw-r--r--. 1 root root 18 12月 17 08:37 /var/www/html/index.html
[root@pc205 ~]# 

3 在svr5上刪除剛新增的檔案a.html

[root@svr5 ~]# rm -rf /var/www/html/a.html 
[root@svr5 ~]# ls -lh /var/www/html/*.htm
-rw-r--r--. 1 root root 18 12月 17 08:37 /var/www/html/index.html

4 在pc205上再次觀察/var/www/html目錄下的變化

[root@pc205 ~]# ls -lh /var/www/html/*.html
-rw-r--r--. 1 root root 18 12月 17 08:37 /var/www/html/index.html
[root@pc205 ~]#