欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Windows下编写批处理脚本来启动和重置Oracle数据库

程序员文章站 2022-04-12 21:50:02
cmd启动oracle数据库 新建一个bat文件,复制内容进去,双击即可启动. @echo off net start oraclexetnslistene...

cmd启动oracle数据库
新建一个bat文件,复制内容进去,双击即可启动.

@echo off 
net start oraclexetnslistener 2>nul 
net start oracleservicexe 2>nul 
@oradim -startup -sid xe -starttype inst > nul 2>&1 

oracle重置数据库命令
新建bat文件,复制以下内容,然后执行。

@echo off 
rem 
rem the script assumes that user can connect using "/ as sysdba" 
rem 
rem ================= 
rem restore procedure 
rem ================= 
rem 
rem if installed oracle home is also lost and oracle binaries were 
rem re-installed or the oracle is installed to new oracle home location 
rem compared to backup time, then user will be prompted to enter flash 
rem recovery area location. 
rem 
rem for database in noarchivelog mode, database is restored to last offline 
rem backup time/scn; 
rem for database in archive log mode, database is restored from last backup 
rem and a complete recovery is attempted. if complete recovery fails, 
rem user can open the database with resetlogs option provided the files 
rem are not recovery fuzzy. 
rem 
rem the restore log is saved in ?/database/oxe_restore.log 
rem 
 
setlocal 
 
set /p inp="this operation will shut down and restore the database. are you sure [y/n]?" 
:checkinp 
if /i "%inp%" == "y" goto :confirmedyes 
if /i "%inp%" == "n" exit 
:askagain 
set /p inp= 
goto :checkinp 
 
:confirmedyes 
 
echo restore in progress... 
 
echo db_name=xe >%temp%\rman_dummy.ora 
echo sga_target=270m >>%temp%\rman_dummy.ora 
 
 
net start oracleservicexe 
 
rem startup database in nomount mode using rman... 
@( 
echo set echo on^; 
echo startup nomount pfile=%temp%\rman_dummy.ora force^; 
) > %temp%\restore_rman0.dat 
rman target / @%temp%\restore_rman0.dat 
if not %errorlevel% == 0 set errorstr= rman error - could not startup dummy instance & goto :restorefailederr 
 
@( 
echo connect / as sysdba^; 
echo set head off 
echo set echo off 
echo set linesize 515 
echo variable var varchar2^(512^)^; 
echo execute :var := sys.dbms_backup_restore.normalizefilename^(^'spfile2init^'^)^; 
echo spool %temp%\spfile2init.log 
echo select sys.dbms_backup_restore.normalizefilename^(^'spfile2init.ora^'^) spfile2init from dual^; 
echo exit^; 
) > %temp%\spfile2init.sql 
sqlplus /nolog @%temp%\spfile2init.sql >nul 
for /f %%i in (%temp%\spfile2init.log) do set spfile2init=%%i 
 
@( 
echo connect / as sysdba; 
 echo set head off 
 echo set echo off 
 echo set linesize 515 
 echo variable var varchar2^(512^)^; 
 echo execute :var := sys.dbms_backup_restore.normalizefilename^(^'fra_loc^'^)^; 
 echo spool %temp%\restore_rmanlog.log 
 echo select sys.dbms_backup_restore.normalizefilename^(^'oxe_restore.log^'^) restore_rmanlog from dual^; 
 echo exit^; 
) > %temp%\restore_rmanlog.sql 
sqlplus /nolog @%temp%\restore_rmanlog.sql >nul 
for /f %%i in (%temp%\restore_rmanlog.log) do set restore_rmanlog=%%i 
 
if not exist ^"%spfile2init%^" goto get_rcvarea_loc 
@( 
 echo set echo on^; 
 echo shutdown immediate^; 
 echo startup nomount pfile=^"%spfile2init%^"^; 
 echo restore ^(spfile from autobackup^) ^(controlfile from autobackup^)^; 
 echo startup mount force^; 
 echo configure controlfile autobackup off^; 
 echo restore database^; 
) > %temp%\restore_rman1.dat 
rman target / @%temp%\restore_rman1.dat trace "%restore_rmanlog%" 
if not %errorlevel% == 0 set errorstr= rman error - see log for error & goto :restorefailederr 
goto restored_files 
 
:get_rcvarea_loc 
set /p rcvarea_loc="enter the flash recovery area location:" 
@( 
 echo set echo on^; 
 echo restore ^(spfile from autobackup db_recovery_file_dest=^'%rcvarea_loc%^'^)^; 
 echo startup nomount force^; 
 echo restore ^(controlfile from autobackup^)^; 
 echo alter database mount^; 
 echo configure controlfile autobackup off^; 
 echo restore database^; 
) > %temp%\restore_rman1.dat 
rman target / @%temp%\restore_rman1.dat trace "%restore_rmanlog%" 
if not %errorlevel% == 0 set errorstr= rman error - see log for error & goto :restorefailederr 
goto restored_files 
 
:restored_files 
@( 
 echo connect / as sysdba^; 
 echo declare cursor n1 is select name from v$tempfile^; 
 echo begin 
 echo for a in n1 
 echo loop 
 echo begin 
 echo sys.dbms_backup_restore.deletefile^(a.name^)^; 
 echo exception 
 echo when others then 
 echo null^; 
 echo end^; 
 echo end loop^; 
 echo end^; 
 echo / 
 echo exit^; 
 echo / 
) > %temp%\deltfile.sql 
sqlplus /nolog @%temp%\deltfile.sql >nul 
@( 
 echo connect / as sysdba^; 
 echo set head off 
 echo set echo off 
 echo spool %temp%\logmode.log 
 echo select log_mode from v$database^; 
 echo exit^; 
) > %temp%\logmode.sql 
sqlplus /nolog @%temp%\logmode.sql >nul 
for /f %%i in (%temp%\logmode.log) do set logmode=%%i 
 
if "%logmode%" == "noarchivelog" goto process_noarchivelog 
if "%logmode%" == "archivelog" goto process_archivelog 
set errorstr= unknown log mode : %logmode% 
goto :restorefailederr 
 
:process_noarchivelog 
@( 
 echo set echo on^; 
 echo alter database open resetlogs; 
) > %temp%\restore_rman2.dat 
rman target / @%temp%\restore_rman2.dat trace "%restore_rmanlog%" append 
if not %errorlevel% == 0 set errorstr= rman error - see log for details & goto :restorefailederr 
goto :restoresucess 
 
:process_archivelog 
@( 
 echo set echo on^; 
 echo recover database^; 
 echo alter database open resetlogs; 
) > %temp%\restore_rman2.dat 
rman target / @%temp%\restore_rman2.dat trace "%restore_rmanlog%" append 
if not %errorlevel% == 0 set errorstr= rman error - see log for details & goto :restorefailederr 
goto :restoresucess 
 
:restoresucess 
echo restore of the database succeeded. 
echo log file is at %restore_rmanlog%. 
pause press any key to exit 
exit 
goto :eof 
 
:restorefailederr 
echo ==================== error ============================= 
echo restore of the database failed. 
echo %errorstr%. 
echo log file is at %restore_rmanlog%. 
echo ==================== error ============================= 
pause press any key to exit 
exit 
goto :eof