1. Copy the following code into the text, and finally modify the text suffix name to xxx.bat batch file;
*************************************************************************************************************** ******************
@echo off
Setlocal enabledelayedexpansion
:: Read configuration file
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%) does (
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
)
:: Take Default value
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%\ database backup
for/f "tokens=*"%%x in ("!bak_dir!") do set BAK_DIR=%%~FX
If not exist!bak_dir! MD!val! >nul 2>nul
:: Removing malformed database connection Configuration Items
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!
)
)
)
:: Access to management procedures
If "%1" = = "goto init
:: Check whether the exp command is available
: Checkexp
Set Resultfile=%temp%\%random%.txt
Del%resultfile%/q>nul 2>nul
Exp a/[email protected]%random% file=%temp%\%random%.dmp >nul 2>%resultfile%
If exist%resultfile% (
Type%resultfile%|find "' exp ' is not an internal or external command" >nul
If!errorlevel!==0 (
Del%resultfile%>nul
The echo exp command is not available! The program is about to exit!
Ping-n 127.1 >nul 2>nul
Exit
)
Del%resultfile%>nul
)
:: 1. Database backup
Title Backup process
Echo.
Echo.
echo One, backup in progress ...
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 "[Email protected] tokens=2"%%a in (' echo!ora_cur! ') do set ora_net=%%a
Echo.
Echo.
Echo%%i. Backing up!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 If the backup is successful, compress the >nul
If exist "!bakfile!" (
pushd!bak_cur_dir!
Set zipfile=
If exist "%programfiles%\winrar\winrar.exe" (
Echo uses WinRAR for compression >nul
Set zipfile=!bak_cur_fnm!. rar
"%programfiles%\winrar\winrar" A-r "!zipfile!" "!bak_cur_fnm!. DMP ""!bak_cur_fnm!. Log
) Else (
Echo uses zip instruction to compress >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 If the backup file does not exist but has a log file, delete the log file >nul
If exist "!logfile!" del/q "!logfile!"
)
)
)
:: 2. Database expired Backup Delete
Echo.
Echo.
Echo II, purging expired backup files ...
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 set m=0%%b
If%%c LSS 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 "[Email protected] 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 Check today's backup succeeded no >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 determines if the folder condition satisfies >nul
For%%a in (!cur_dir!\*.dmp,!cur_dir!\*.log,!cur_dir!\*.zip,!cur_dir!\*.rar) do (
echo determines if the file name condition satisfies >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 determines whether the time condition satisfies >nul
if! filedate! Leq%datee% (
echo%date:~0,10%%time:~0,8% Delete outdated backup%%a
echo%date:~0,10%%time:~0,8% Delete outdated backup%%a>>!cur_dir!\delete.log
del/q "%%a"
)
)
)
) Else (
echo%date:~0,10%%time:~0,8% [!ora_net!__!ora_usr!] Because today's backup did not succeed, temporarily do not delete expired files!
echo%date:~0,10%%time:~0,8% [!ora_net!__!ora_usr!] Because today's backup did not succeed, temporarily do not delete expired files! >>!cur_dir!\delete.log
)
)
)
:: 3. Complete exit
Echo.
Echo.
Echo three, this backup operation is complete, is about to exit.
Ping-n 127.1 >nul 2>nul
Exit
:: ================================= The following is a backup program =================================
:: ================================= The following are the hypervisor =================================
: Init
Mode con cols=100 lines=40
Title Oracle Automatic backup-by zhouyou96
Color 0e
:: Copy to Windows directory
Copy "%~f0" "%windir%\oracleautobackup\oracleautobackup.bat" >nul 2>nul
:: Register a scheduled Task
: regtasks
sc config schedule start= auto >nul 2>nul
At|find "service not Started" >nul 2>nul& & (
net start schedule
if not!errorlevel!==0 (
Echo Task scheduler^ (scheduled task ^) service failed to start and the program is about to exit!)
Pause>nul
Goto exit
)
)
Set job_tmr=!bak_hou!:0 0
If!bak_hou! LSS set job_tmr=0!bak_hou!:0 0
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 automatic backup. Job >nul 2>nul
Rename%windir%\tasks\! lastat! Oracle automatic backup. Job
:: Save configuration file
: 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) does (
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%
)
)
:: Preparing the Database configuration string
Set ora_str=
for%%i in (1,2,3,4,5,6,7,8,9) does (
Set Ora[%%i]>nul 2>nul&& (
Set ora_cur=
for/f "Usebackq delims==. Tokens=1-3 "%%a in (' Set ora[%%i] ') does 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 "[email protected] tokens=2"%%a in (' Echo-!ora_cur! ') do set ora_net=%%a
set Ora_str =!ora_str!%%i.! ora_usr!/******@!ora_net!;
)
)
:: Start
: Start
CLS
Echo------------------------------------------------------------------------------ --------------------
Echo Oracle automatic backup
Echo zhouyou96 qq:191458000
Echo----------------------------------- ---------------------------------------------------------------
Echo uses the operating system's own scheduled Tasks feature, Run the exp command every day to export the specified Oracle database and compress it, and then delete the
Echo expired compressed export file as needed for automatic backup functionality.
Echo Usually, for ease of administration, an Oracle user in our company has and only has the sole authority to manage a database, so the user's login name
Echo can actually be treated as a database name.
Echo.
Echo 1. Adding a database:!ora_str!
Echo 2. Delete Database
Echo 3. Settings folder:!bak_dir!
Echo 4. Backup time:!bak_hou!
Echo 5. Deleted days ago:!bak_lot!
Echo 6. Back up now
Echo 7. Exit
Echo.
:: Select
: Cho
Set choice=
set/p choice= Please select:
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 = ================================== Please select 1~7, press any key to re-elect! ====================================
Echo =================================================================== ==============================
Pause>nul
Goto start
:: Adding a database
: 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= of up to 9!
If not!maxora!==9 (
set/a maxora+=1
Set new_ora=
set/p new_ora= Please enter (user name/password @ NETWORK SERVICE name):
Set str_result= format Error!
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= added successfully,
)
)
)
Echo =================================================================================================
echo ====================================!str_result! Press any key to continue! ====================================
Echo =================================================================================================
Pause>nul
If "!str_result!" = = "Add succeeded," Goto Saveconfig
Goto Start
:: Deleting a database
:d Elora
Set str_result= operation Error!
Set del_idx=0
set/p del_idx= Please enter the ordinal number (1~9) to delete:
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= deleted successfully,
)
:: Removing malformed database connection Configuration Items
If "!str_result!" = = "Delete succeeded," (
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! Press any key to continue! ====================================
Echo =================================================================================================
Pause>nul
If "!str_result!" = = "Delete succeeded," Goto Saveconfig
Goto Start
:: Set folder
: Setdir
Set new_dir=
set/p new_dir= Please enter a folder for backup:
If "!new_dir!" = = "" Set new_dir=%cd%\ database backup
for/f "tokens=*"%%x in ("!new_dir!") do set NEW_DIR=%%~FX
set bak_dir=!new_dir!
echo =================================================================================================
echo = =================================== set success, press any key to continue! ====================================
Echo =================================================================== ==============================
Pause>nul
Goto saveconfig
:: What time backup
: Sethou
Set str_result= operation Error!
Set new_hou=
set/p new_hou= Please enter the time of day backup (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 (
Set bak_hou=!new_hou!
Set str_result= setup succeeded,
)
)
)
Echo ==================================================================== =============================
Echo ====================================!str_result! Press any key to continue! ====================================
Echo =================================================================== ==============================
Pause>nul
If "!str_result!" = = "Set successful," Goto regtasks
Goto start
:: Delete a few days ago
: Setlot
Set str_result= operation Error!
Set new_lot=
set/p new_lot= Please enter to delete the backup (greater than 0) from a few days ago:
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= setup succeeded,
)
)
Echo ========================================================================= ========================
Echo ====================================!str_result! Press any key to continue! ====================================
Echo =================================================================== ==============================
Pause>nul
If "!str_result!" = = "Set successful," Goto saveconfig
Goto start
:: Backup Now
: Nowbak
Start%windir%\oracleautobackup\oracleautobackup.bat-backup
Echo =================================================================================================
Echo ==================================== successfully started, press any key to continue! ====================================
Echo =================================================================================================
Pause>nul
Goto Start
:: Exit Program
: Exit
::p Ause>nul
*************************************************************************************************************** ************************
2. To configure the network connection, you must ensure that you are able to access the operating system to the specified backup database, or ping through.
3. Install the Oracle database and configure the connection to the library that can be assigned to the backup data, and you can use Plsql developer to make the Oracle connection;
1. Configuration method: 1. Locate the Tnsnames.ora in the Oracle installation file (install directory: \product\10.2.0\db_1\network\admin\tnsnames.ora,) Add connection information using the editor
For example:
LINUX =
(DESCRIPTION =
(Address_list =
(address = (PROTOCOL = TCP) (HOST = backup Database IP address ) (PORT = 1521))
)
(Connect_data =
(service_name = Backup db instance )
)
)
2. Use Plsql Developer to test whether the connection is connected through
4. Set the timing schedule, directly copy the code to execute the Xxx.bat, find the plan in the task plan , add a parameter later –backup set the backup time.
Oracle Scheduled backups