通過shell備份oracle使用者資料,並將巡檢結果發送至windows跳板機

來源:互聯網
上載者:User

標籤:環境變數   系統   ras   cron   比較   開始   ace   xterm   pen   

背景:生產環境有oracle伺服器,有資料庫使用者若干,需要通過exp方式備份資料庫使用者資料,需要巡檢Database Backup的結果。

目標:通過Linux shell+exp+crontab進行周期備份,通過ssh命令跨系統將Linux下備份結果傳輸至windows。

說明:

oracle client 11g        oracle server:   oracle11g

作業系統:sentos6 or redhat6

連接埠:1521  

Oracle _sid = shwhcq

 

操作過程:

一、備份

1、建立目錄

  以oracle使用者登入系統

  mkdir /home/oracle/script            ------指令碼目錄

  mkdir /ipi_bak/db/gg                -------資料檔案備份目標目錄(這裡給oracle使用者w許可權)

  說明:這裡我是以使用者匯出資料的,所以在/ipi_bak/db/gg  下以各使用者的名稱建立子目錄

 

2、建立指令碼

以xssp使用者的匯出指令碼為例:vi xssp-exp.sh

#!/bin/bash export ORACLE_BASE=/app/oracleexport ORACLE_HOME=$ORACLE_BASE/product/11.2.0/db_1export ORACLE_SID=shwhcq1export LANG=Cexport NLS_LANG="SIMPLIFIED CHINESE_CHINA.ZHS16GBK"export ORACLE_TERM=xtermexport PATH=$ORACLE_HOME/bin:/usr/sbin:$PATHexport LD_LIBRARY_PATH=$ORACLE_HOME/lib:/lib:/usr/lib TIME=`date "+%F_%H:%M:%S"`/grid/11.2.0/grid_1/bin/exp xssp/[email protected]ip/shwhcq grants=y file=/ipi_bak/db/gg/xssp/xssp_$TIME.dmp log=/ipi_bak/db/gg/xssp/xssp_$TIME.log owner=xssp &> /dev/null

給執行許可權:chmod +x xssp-exp.sh 

註:以上代碼為Oracle資料庫運行帳號oracle的系統內容變數設定,必須添加,否則crontab任務計劃不能執行。

 

3、建立計劃任務

[[email protected] script]$ crontab -e 

5 2 * * *        /bin/sh  /home/oracle/script/xssp-exp.sh #每天02:05分執行10 2 * * *       /bin/sh  /home/oracle/script/artcms-exp.sh15 2 * * *       /bin/sh  /home/oracle/script/caee_scan-exp.sh25 2 * * *       /bin/sh  /home/oracle/script/ipijbpm-exp.sh35 2 * * *       /bin/sh  /home/oracle/script/ipiqxt-exp.sh

 註:crontab 執行記錄在/var/log/cron

 

二、備份結果輸出到windows跳板機

1、將備份日誌的最後一行執行情況輸出到指定檔案,檔案以日期命名

cat /home/oracle/script/db_bak.sh 

#!/bin/bashTIME=`date "+%F"`xssp_log=/ipi_bak/db/gg/xssp/xssp_$TIME.logartcms_log=/ipi_bak/db/gg/artcms/cms_$TIME.logcaee_log=/ipi_bak/db/gg/caee/caee_$TIME.logipiqxt_log=/ipi_bak/db/gg/ipiqxt/ipiqxt_$TIME.logipijbpm_log=/ipi_bak/db/gg/ipijbpm/ipijbpm_$TIME.logdblog=/home/oracle/db_baklog/$TIME.logecho "xssp_log" >> $dblogtail -1 $xssp_log >> $dblogecho "artcms_log" >> $dblogtail -1 $artcms_log >> $dblogecho "caee_log" >> $dblogtail -1 $caee_log >> $dblogecho "ipiqxt_log" >> $dblogtail -1 $ipiqxt_log >> $dblog echo "ipijbpm_log" >> $dblogtail -1 $ipijbpm_log >> $dblog

2、windows上用ssh的scp命令下載備份執行結果日誌,這樣就不用每次登陸去查看

批處理:cat_linux_bak.bat

cd D:\Program Files (x86)\SSH Communications Security\SSH Secure Shellscp2.exe [email protected]Database Backup伺服器ip:/home/oracle/db_baklog/* D:\ipi_bak\script\linux_dbbak_logs\

 註:想要Linux和win要通過指令碼自動互傳檔案不互動。需要進行Linux和win的信任配置:http://blog.csdn.net/jiangshouzhuang/article/details/50683049

簡單敘述:

我們把Windows上面產生的Public Key放到Linux伺服器上指定使用者家目錄下面的.ssh目錄中,並添加公開金鑰內容到.ssh目錄下面的authorized_keys檔案。

如果我們開始從Windows(用戶端)上面通過ssh方式遠程Linux(伺服器)時,此時用戶端軟體就會向伺服器發出請求,請求用密匙進行安全驗證。伺服器收到請求之後,先在該伺服器上的主目錄下尋找公匙,然後把它和發送過來的公匙進行比較。如果兩個密匙一致,伺服器就用公匙加密“質詢”並把它發送給用戶端軟體。用戶端軟體收到“質詢”之後就可以用私匙解密再把它發送給伺服器,此時因為密鑰能匹配上,所以可以直接登入到Linux伺服器。

 1、在Windows本機上產生密鑰

D:\SSHCommunications Security\SSH Secure Shell>ssh-keygen2-t rsa

注釋:我們這裡使用rsa的密鑰,預設是2048bit(位),同樣我們也可以使用dsa方式的密鑰。

在執行ssh-keygen2 -t rsa產生金鑰組時,會提示你輸入Passphrase的值,我們要求免密碼登入伺服器,所以直接斷行符號。 

有提示:

Private key saved to C:/Users/Administrator/ApplicationData/SSH/UserKeys/id_rsa

_2048_a

Public key saved to C:/Users/Administrator/ApplicationData/SSH/UserKeys/id_rsa_

2048_a.pub

根據提示知道了產生的金鑰組的路徑,但是經過我實際尋找,我的windows環境的金鑰組位於:C:\Users\Administrator\AppData\Roaming\SSH\UserKeys下面。

 

 2、然後我們將Windows的C:\Users\Administrator\AppData\Roaming\SSH\UserKeys目錄下的id_rsa_2048_a.pub檔案上傳到Linux伺服器的home/user/.ssh目錄下面。

因為我們的公開金鑰是在Windows上面產生的,Linux的Openssh不識別,所以需要進行轉換後再追加到authorized_keys中:

$ ssh-keygen -i -f id_rsa_2048_a.pub >>authorized_keys

退出之前使用密碼登入的SSH Secure Shell用戶端,然後重新登入並在登入認證狀態欄中選擇Public Key方式,這樣就可以免密碼登入了。

3、測試

 使用SSH Secure Shell用戶端內建的scp2命令下載Linux伺服器的檔案

D:\SSH Communications Security\SSHSecure Shell>scp2.exe -r -d [email protected]:/home/user/testhivedata  D:\test\

.000000_0.crc                          |   12B |  12B/s | TOC: 00:00:01 | 100%

000000_0                               |   96B |  96B/s | TOC: 00:00:01 | 100%

datacity.txt                           |  95kB | 95kB/s | TOC: 00:00:01 | 100%

可以看出遠程拷貝也不需要輸入密碼。

 

最後查看執行結果:以後可以輕鬆的查看oracle的備份結果

 

通過shell備份oracle使用者資料,並將巡檢結果發送至windows跳板機

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.