1.將如下代碼復制到文本中,最後將文本後綴名稱修改成XXX.bat 批處理文件;
*********************************************************************************************************************************
@echo off
setlocal ENABLEDELAYEDEXPANSION
::讀取配置文件
md %windir%\OracleAutoBackup >nul 2>nul
set configFile=%windir%\OracleAutoBackup\config.ini
set i=0
if not exist %configFile% echo.>%configFile%
for /f "delims=" %%x in (%configFile%) do (
if !i!==0 set bak_hou=%%x
if !i!==1 set bak_lot=%%x
if !i!==2 set bak_dir=%%x
if !i! gtr 2 (
set/a gup=!i!-2
set ora[!gup!]=%%x
)
set/a i+=1
)
::取默認值
if "!bak_hou!"=="" set bak_hou=3
echo !bak_hou!|findstr "^[0-9]*$">nul || set bak_hou=3
if "!bak_lot!"=="" set bak_lot=7
echo !bak_lot!|findstr "^[0-9]*$">nul || set bak_lot=7
if "!bak_dir!"=="" set bak_dir=%cd%\數據庫備份
for /f "tokens=*" %%x in ("!bak_dir!") do set bak_dir=%%~fx
if not exist !bak_dir! md !val! >nul 2>nul
::去掉格式錯誤的數據庫連接配置項
set j=0
for %%i in (1,2,3,4,5,6,7,8,9) do (
set ora[%%i]>nul 2>nul&& (
set ora_cur=
for /f "usebackq delims==. tokens=1-3" %%a in (`set ora[%%i]`) do set ora_cur=%%b
set ora[%%i]=
echo !ora_cur!|findstr "\/">nul 2>nul && echo !ora_cur!|findstr "@">nul 2>nul && (
set/a j+=1
set ora[!j!]=!ora_cur!
)
)
)
::進入管理程序
if "%1"=="" goto init
::檢查exp命令是否可用
:checkexp
set resultFile=%temp%\%random%.txt
del %resultFile% /q>nul 2>nul
exp a/a@a%random% file=%temp%\%random%.dmp >nul 2>%resultFile%
if exist %resultFile% (
type %resultFile%|find "'exp' 不是內部或外部命令">nul
if !errorlevel!==0 (
del %resultFile%>nul
echo exp命令不可用!程序即將退出!
ping -n 10 127.1 >nul 2>nul
exit
)
del %resultFile%>nul
)
::1.數據庫備份
title 備份進程
echo.
echo.
echo 一、正在進行備份……
for %%i in (1,2,3,4,5,6,7,8,9) do (
set ora[%%i]>nul 2>nul&& (
set ora_cur=
for /f "usebackq delims==. tokens=1-3" %%a in (`set ora[%%i]`) do set ora_cur=%%b
set ora_usr=
set ora_net=
for /f "delims=/" %%a in ('echo !ora_cur!') do set ora_usr=%%a
for /f "delims=@ tokens=2" %%a in ('echo !ora_cur!') do set ora_net=%%a
echo.
echo.
echo %%i.正在備份 !ora_usr!/******@!ora_net!……
md !bak_dir!\!ora_net!__!ora_usr!\ >nul 2>nul
set ftmr=!time: =0!
set bak_cur_dir=!bak_dir!\!ora_net!__!ora_usr!\
for /f "tokens=*" %%x in ("!bak_cur_dir!") do set bak_cur_dir=%%~fx
set bak_cur_fnm=!ora_net!__!ora_usr!__!date:~0,4!!date:~5,2!!date:~8,2!-!ftmr:~0,2!!ftmr:~3,2!
set bakfile=!bak_cur_dir!!bak_cur_fnm!.dmp
set logfile=!bak_cur_dir!!bak_cur_fnm!.log
exp !ora_cur! file="!bakfile!" log="!logfile!"
echo 如果備份成功的話,就進行壓縮>nul
if exist "!bakfile!" (
pushd !bak_cur_dir!
set zipfile=
if exist "%ProgramFiles%\winrar\winrar.exe" (
echo 使用WinRAR進行壓縮>nul
set zipfile=!bak_cur_fnm!.rar
"%programfiles%\winrar\winrar" a -r "!zipfile!" "!bak_cur_fnm!.dmp" "!bak_cur_fnm!.log"
) else (
echo 使用ZIP指令進行壓縮>nul
set zipfile=!bak_cur_fnm!.zip
zip "!zipfile!" "!bak_cur_fnm!.dmp" "!bak_cur_fnm!.log">nul
)
if exist "!zipfile!" (
del /q "!bakfile!"
del /q "!logfile!"
)
popd
) else (
echo 如果不存在備份文件,但有日志文件,則刪除日志文件>nul
if exist "!logfile!" del /q "!logfile!"
)
)
)
::2.數據庫過期備份刪除
echo.
echo.
echo 二、正在清除過期的備份文件……
for /f "tokens=1,2,3 delims=-" %%a in ('echo wscript.echo date-!bak_lot! ^>t~.vbs ^& cscript //nologo t~.vbs ^& del t~.vbs') do (
set y=%%a&set m=%%b&set d=%%c
if %%b lss 10 set m=0%%b
if %%c lss 10 set d=0%%c
)
set DateE=!y!-!m!-!d!
for %%i in (1,2,3,4,5,6,7,8,9) do (
set ora[%%i]>nul 2>nul&& (
set ora_cur=
for /f "usebackq delims==. tokens=1-3" %%a in (`set ora[%%i]`) do set ora_cur=%%b
set ora_usr=
set ora_net=
for /f "delims=/" %%a in ('echo !ora_cur!') do set ora_usr=%%a
for /f "delims=@ tokens=2" %%a in ('echo !ora_cur!') do set ora_net=%%a
set cur_dir=!bak_dir!\!ora_net!__!ora_usr!
for /f "tokens=*" %%x in ("!cur_dir!") do set cur_dir=%%~fx
echo 檢查今天的備份成功了沒有 >nul
set fnm_pre=!cur_dir!\!ora_net!__!ora_usr!__!date:~0,4!!date:~5,2!!date:~8,2!-& set today_success=0
dir !fnm_pre!*.dmp !fnm_pre!*.zip !fnm_pre!*.rar /b >nul 2>nul && set today_success=1
if "!today_success!"=="1" (
echo 判斷文件夾條件是否滿足 >nul
for %%a in (!cur_dir!\*.dmp,!cur_dir!\*.log,!cur_dir!\*.zip,!cur_dir!\*.rar) do (
echo 判斷文件名稱條件是否滿足 >nul
set n=%%a&set n=!n:~-17,-9!&set n=!n:~0,4!-!n:~4,2!-!n:~6,2!
set t=%%~ta
set FileDate=!t:~0,10!
if "!n!"=="!FileDate!" (
echo 判斷時間條件是否滿足 >nul
if !FileDate! leq %DateE% (
echo %date:~0,10% %time:~0,8% 刪除過期備份 %%a
echo %date:~0,10% %time:~0,8% 刪除過期備份 %%a>>!cur_dir!\delete.log
del /q "%%a"
)
)
)
) else (
echo %date:~0,10% %time:~0,8% [!ora_net!__!ora_usr!]因為今天的備份沒有成功,暫時不刪除過期文件!
echo %date:~0,10% %time:~0,8% [!ora_net!__!ora_usr!]因為今天的備份沒有成功,暫時不刪除過期文件!>>!cur_dir!\delete.log
)
)
)
::3.完成退出
echo.
echo.
echo 三、本次備份操作完成,即將退出。
ping -n 10 127.1 >nul 2>nul
exit
::=================================以下是備份程序=================================
::=================================以下是管理程序=================================
:init
mode con cols=100 lines=40
title Oracle自動備份 - by zhouyou96
color 0e
::復制到 Windows 目錄
copy "%~f0" "%windir%\OracleAutoBackup\OracleAutoBackup.bat" >nul 2>nul
::注冊計劃任務
:regtasks
sc config schedule start= auto >nul 2>nul
at|find "服務尚未啟動">nul 2>nul&&(
net start schedule
if not !errorlevel!==0 (
echo Task Scheduler^(計劃任務^)服務未能啟動,程序即將退出!
pause>nul
goto exit
)
)
set job_tmr=!bak_hou!:00
if !bak_hou! lss 10 set job_tmr=0!bak_hou!:00
at !job_tmr! /every:1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31 %windir%\OracleAutoBackup\OracleAutoBackup.bat -backup >nul 2>nul
for /f "usebackq" %%i in (`dir %windir%\tasks\at*.job /b/o:d`) do set lastAt=%%i
del %windir%\tasks\Oracle自動備份.job >nul 2>nul
rename %windir%\tasks\!lastAt! Oracle自動備份.job
::保存配置文件
:saveconfig
echo !bak_hou!>%configFile%
echo !bak_lot!>>%configFile%
echo !bak_dir!>>%configFile%
for %%i in (1,2,3,4,5,6,7,8,9) do (
set ora[%%i]>nul 2>nul&& (
set ora_cur=
for /f "usebackq delims==. tokens=1-3" %%a in (`set ora[%%i]`) do set ora_cur=%%b
echo !ora_cur!>>%configFile%
)
)
::准備數據庫配置字符串
set ora_str=
for %%i in (1,2,3,4,5,6,7,8,9) do (
set ora[%%i]>nul 2>nul&& (
set ora_cur=
for /f "usebackq delims==. tokens=1-3" %%a in (`set ora[%%i]`) do set ora_cur=%%b
set ora_usr=
set ora_net=
for /f "delims=/" %%a in ('echo !ora_cur!') do set ora_usr=%%a
for /f "delims=@ tokens=2" %%a in ('echo !ora_cur!') do set ora_net=%%a
set ora_str=!ora_str!%%i. !ora_usr!/******@!ora_net!;
)
)
::開始
:start
cls
echo --------------------------------------------------------------------------------------------------
echo Oracle自動備份
echo 作者:zhouyou96 QQ:191458000
echo --------------------------------------------------------------------------------------------------
echo 使用操作系統自帶的計劃任務功能,每天定時運行exp命令導出指定的Oracle數據庫並壓縮,然後按需刪除
echo 已過期的壓縮的導出文件,以實現自動備份的功能。
echo 通常,為了便於管理,在我們公司一個oracle用戶有且僅有的全權管理一個數據庫,因此該用戶的登陸名稱
echo 其實可以視做為數據庫名稱。
echo.
echo 1.添加數據庫:!ora_str!
echo 2.刪除數據庫
echo 3.設置文件夾:!bak_dir!
echo 4.幾點鐘備份:!bak_hou!
echo 5.刪除幾天前:!bak_lot!
echo 6.立即備份
echo 7.退出
echo.
::選擇
:cho
set choice=
set /p choice=請選擇:
if not "%choice%"=="" set choice=%choice:~0,1%
if "%choice%"=="1" goto addora
if "%choice%"=="2" goto delora
if "%choice%"=="3" goto setdir
if "%choice%"=="4" goto sethou
if "%choice%"=="5" goto setlot
if "%choice%"=="6" goto nowbak
if "%choice%"=="7" goto exit
echo.
echo =================================================================================================
echo =================================== 請選擇1~7,按任意鍵重選!====================================
echo =================================================================================================
pause>nul
goto start
::添加數據庫
:addora
set maxora=0
for %%i in (1,2,3,4,5,6,7,8,9) do (set ora[%%i]>nul 2>nul&&(set maxora=%%i))
set str_result=最多9個!
if not !maxora!==9 (
set/a maxora+=1
set new_ora=
set/p new_ora=請輸入(用戶名/密碼@網絡服務名):
set str_result=格式錯誤!
if not "!new_ora!"=="" (
echo !new_ora!|findstr "\/">nul 2>nul && echo !new_ora!|findstr "@">nul 2>nul && (
set ora[!maxora!]=!new_ora!
set str_result=添加成功,
)
)
)
echo =================================================================================================
echo ==================================== !str_result!按任意鍵繼續!====================================
echo =================================================================================================
pause>nul
if "!str_result!"=="添加成功," goto saveconfig
goto start
::刪除數據庫
:delora
set str_result=操作錯誤!
set del_idx=0
set/p del_idx=請輸入要刪除的序數(1~9):
if not "%del_idx%"=="" set del_idx=%del_idx:~0,1%
if "!del_idx!"=="" set del_idx=0
echo !del_idx!|findstr "^[0-9]*$">nul || set del_idx=0
if not "!del_idx!"=="0" (
set ora[!del_idx!]=
set str_result=刪除成功,
)
::去掉格式錯誤的數據庫連接配置項
if "!str_result!"=="刪除成功," (
set j=0
for %%i in (1,2,3,4,5,6,7,8,9) do (
set ora[%%i]>nul 2>nul&& (
set ora_cur=
for /f "usebackq delims==. tokens=1-3" %%a in (`set ora[%%i]`) do set ora_cur=%%b
set ora[%%i]=
echo !ora_cur!|findstr "\/">nul 2>nul && echo !ora_cur!|findstr "@">nul 2>nul && (
set/a j+=1
set ora[!j!]=!ora_cur!
)
)
)
)
echo =================================================================================================
echo ==================================== !str_result!按任意鍵繼續!====================================
echo =================================================================================================
pause>nul
if "!str_result!"=="刪除成功," goto saveconfig
goto start
::設置文件夾
:setdir
set new_dir=
set/p new_dir=請輸入備份用的文件夾:
if "!new_dir!"=="" set new_dir=%cd%\數據庫備份
for /f "tokens=*" %%x in ("!new_dir!") do set new_dir=%%~fx
set bak_dir=!new_dir!
echo =================================================================================================
echo ==================================== 設置成功,按任意鍵繼續!====================================
echo =================================================================================================
pause>nul
goto saveconfig
::幾點鐘備份
:sethou
set str_result=操作錯誤!
set new_hou=
set/p new_hou=請輸入每天幾點鐘備份(0~23):
echo !new_hou!|findstr "^[0-9]*$">nul || set new_hou=
if not "!new_hou!"=="" (
if !new_hou! geq 0 (
if !new_hou! leq 23 (
set bak_hou=!new_hou!
set str_result=設置成功,
)
)
)
echo =================================================================================================
echo ==================================== !str_result!按任意鍵繼續!====================================
echo =================================================================================================
pause>nul
if "!str_result!"=="設置成功," goto regtasks
goto start
::刪除幾天前
:setlot
set str_result=操作錯誤!
set new_lot=
set/p new_lot=請輸入刪除幾天之前的備份(大於零):
echo !new_lot!|findstr "^[0-9]*$">nul || set new_lot=
if not "!new_lot!"=="" (
if !new_lot! gtr 0 (
set bak_lot=!new_lot!
set str_result=設置成功,
)
)
echo =================================================================================================
echo ==================================== !str_result!按任意鍵繼續!====================================
echo =================================================================================================
pause>nul
if "!str_result!"=="設置成功," goto saveconfig
goto start
::現在備份
:nowbak
start %windir%\OracleAutoBackup\OracleAutoBackup.bat -backup
echo =================================================================================================
echo ==================================== 成功啟動,按任意鍵繼續!====================================
echo =================================================================================================
pause>nul
goto start
::退出程序
:exit
::pause>nul
***************************************************************************************************************************************
2.配置網絡連接,必須確保能正常遠超訪問到指定備份數據庫的操作系統,或者ping通。
3.安裝oracle數據庫,並配置好能指定到備份數據的庫的連接,可以使用PLSQL Developer做oracle的連接;
1. 配置方法:1.到oracle的安裝文件中找到tnsnames.ora,(安裝目錄:\product\10.2.0\db_1\network\ADMIN\tnsnames.ora,)使用編輯器添加連接信息
例如:
LINUX =
(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 備份數據庫IP地址)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = 備份數據庫實例)
)
)
2.使用PLSQL Developer測試是否連接通過
4.設置定時計劃,直接復制下來的代碼執行XXX.bat,在任務計劃中找到該計劃,在後面添加一個參數 –backup 設置好備份時間即可