您的位置:首页 > 数据库 > Oracle

Oracle 热备份batch脚本 Windows

2013-10-03 11:23 155 查看
1.最初来源于网络。

2.根据环境和喜好自己修改。

3.实测是可以完成备份任务的。

4.不推荐用于实际环境。

bak.bat:执行时执行此脚本,其他脚本是调用和生成或者生成之后再调用。(需要自己修改先)

@ECHO OFF
REM ################################################################
REM version 2013.10.02
REM First database need to be ARCHIVELOG module.
REM     1.Check the database current tablespace and generate backup script dynamic.
REM     2.The log file save as date.log and very convenience.
REM     3.Compressing the backup content and named as date format,configuration by yourself is necessary.
REM     4.Store the compressing file to FTP server,configuration by yourself is necessary.
REM Usage:
REM     1.copy bak.bat and Hot_gen.bat to oracle database server.
REM     2.Change the sid,username and passeword if you need.Contain destination address and where your log store path.
REM     3.Construct schedule task and add bak.bat to system and run the time you specific.
REM The principle of the script:
REM          bak.bat invoke Hot_gen.bat to generate script. and using generate script to backup database.
rem
REM ###############################################################
ECHO.
REM Backup DataBase script LogFile
set "logdir=d:\ora\log"
::SET logdir=d:\ora\log
REM B_SCRIPT_TARGET B_BACKUP_TARGET
SET B_SCRIPT_TARGET=d:\ora\scripttarget
SET B_BACKUP_TARGET=d:\ora\baktarget
if not exist %logdir% mkdir %logdir%
if not exist %B_SCRIPT_TARGET% mkdir %B_SCRIPT_TARGET%
if not exist %B_BACKUP_TARGET% mkdir %B_BACKUP_TARGET%
>>"%logdir%\hot_gen%date:~0,4%%date:~5,2%%date:~8,2%.log" call d:\Hot_gen.bat %B_SCRIPT_TARGET% %B_BACKUP_TARGET%

::call d:\Hot_gen.bat %B_SCRIPT_TARGET% %B_BACKUP_TARGET%>>"%logdir%\hot_gen%date:~0,4%%date:~5,2%%date:~8,2%.log"

::To avoid blank space to effect the command,write like this is recommend.
::>>"%logdir%\hot_backup%date:~0,4%%date:~5,2%%date:~8,2%.log" call %B_SCRIPT_TARGET%\hot_backup.CMD

::@ECHO off
pause


Hot_gen.bat:被bak.bat调用生成是备份脚本(自己修改先,先读)

::@ECHO off
rem #####################################################################################################
REM This script will create the scripts necessary for a complete hot backup of an Oracle database on NT.
REM Datafiles and controlfiles are backed up in this script.
REM These scripts can then be run in batch. Use the AT scheduler to schedule the backup job.
REM Edit the SID, CONNECT and INIT strings used in this command file.
rem #####################################################################################################
rem Modify history:
REM     Author: Craig MacPherson - Oracle Corporation Canada Inc. June/97
REM     Edited: Stephen Morse - Oracle Corporation US, November 97
REM     Edited: Hao Wang - Samsung SDS China 2005-2-28
rem     Edited: Xiaoqiang Jiang -ECA 2013-10-02 Add some comment
ECHO.
REM HOT_GEN.CMD Usage:
REM      Enter HOT_GEN SCRIPT_TARGET BACKUP_TARGET
REM      where SCRIPT_TARGET is the location for the backup scripts e.g. c:\oraback\sid\COLD
REM      and BACKUP_TARGET is the location for the Oracle datafile
REM     backups when batch is executed

REM SETlocal 10-DEC-1999
REM 1) commented the above help and pause out
REM 2) replaced the connects as sysdba by connect internal 25-JAN-2000 added SET ORACLE_SID= in hot_backup.cmd
REM 3) Add FTP and Compress Function 2005-2-28
REM example uses SID=TTV817
REM %ORACLE_HOME%=c:\oracle\ora817

REM !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
REM These values cannot be derived, please SET them to reflect your environment
REM !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

SET ORACLE_SID=orcl

::    The oracle home may probably: [HardDisk]:\app\Administrator\product\11.2.0\dbhome_1
::    The Oracle document:The directory path to install Oracle components (for example, /u01/app/oracle/product/11.2.0/db_n).
::    You are prompted to enter an Oracle home in the Path field of the Specify File Locations window.

::My oracle 11g database home
SET ORACLE_HOME=D:\app\Administrator\product\11.2.0\dbhome_1

REM !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
REM SET O_CONNECT="sys/change_on_install as sysdba"
REM !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
SET O_SQL=connect sys/change_on_install@%ORACLE_SID% as sysdba

rem !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
rem    The oracle initial file store path:
rem My initial path is: D:\app\Administrator\product\11.2.0\dbhome_1\srvm\admin
rem !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
SET O_INIT=D:\app\Administrator\product\11.2.0\dbhome_1\srvm\admin\init.ora

rem !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
REM Oracle Binaries,Oracle binaries tool
rem !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
SET O_PLUS=%ORACLE_HOME%\bin\sqlplus.exe /nolog
SET O_COPY=%ORACLE_HOME%\bin\ocopy.exe

rem !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
REM Ftp User and Password,have not test yet.
rem !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
SET F_server=192.168.1.88
SET F_user=Administator
SET F_passwd=ccir
SET F_target=/

REM Compress location
SET R_PATH=d:\

rem %1 is call command first parameter
rem For example:a.cmd 1,b. The %1 specify the 1,the %2 specify the b and so on.

SET O_BACKPATH=%2
SET O_SCRIPTPATH=%1
if %O_SCRIPTPATH%.==. goto help
if %O_BACKPATH%.==. goto help

REM ***************************************************************************
REM HOT BACKUP OF THE DATABASE
REM ***************************************************************************

ECHO.
ECHO *************************************************************************
ECHO - Create hot_backup.CMD script to coordinate all backup activities for AT scheduling
ECHO *************************************************************************
ECHO.
ECHO REM Backup the init file >%O_SCRIPTPATH%\hot_backup.CMD
ECHO copy %O_INIT% %O_BACKPATH% >>%O_SCRIPTPATH%\hot_backup.CMD
ECHO SET ORACLE_SID=%ORACLE_SID% >>%O_SCRIPTPATH%\hot_backup.CMD
ECHO REM Run the svrmgr script to backup the datafiles >>%O_SCRIPTPATH%\hot_backup.CMD

::O_PLUS is the sqlplus.exe path
ECHO %O_PLUS% @%O_SCRIPTPATH%\svrmgr1.sql >>%O_SCRIPTPATH%\hot_backup.CMD
ECHO REM Run the svrmgr2.sql script to backup the controlfiles >>%O_SCRIPTPATH%\hot_backup.CMD
ECHO %O_PLUS% @%O_SCRIPTPATH%\svrmgr2.sql >>%O_SCRIPTPATH%\hot_backup.CMD
ECHO REM compress %O_BACKPATH% FILES >>%O_SCRIPTPATH%\hot_backup.CMD
ECHO rar.exe a %R_PATH%\%date:~0,4%%date:~5,2%%date:~8,2%.rar %O_BACKPATH%\*.* >>%O_SCRIPTPATH%\hot_backup.CMD

ECHO %F_user%>%O_SCRIPTPATH%\FTP_CMD.txt
ECHO %F_passwd%>>%O_SCRIPTPATH%\FTP_CMD.txt
ECHO bin>>%O_SCRIPTPATH%\FTP_CMD.txt
ECHO cd %F_target%>>%O_SCRIPTPATH%\FTP_CMD.txt
ECHO mput %R_PATH%\%date:~0,4%%date:~5,2%%date:~8,2%.rar>>%O_SCRIPTPATH%\FTP_CMD.txt
ECHO BYE>>%O_SCRIPTPATH%\FTP_CMD.txt

ECHO ftp -i -s:%O_SCRIPTPATH%\FTP_CMD.txt %F_server% >>%O_SCRIPTPATH%\hot_backup.CMD
ECHO rem del /S/F/Q %R_PATH%\*.* >>%O_SCRIPTPATH%\hot_backup.CMD
ECHO rem del /S/F/Q %O_BACKPATH%\*.* >>%O_SCRIPTPATH%\hot_backup.CMD
ECHO.
ECHO **********************************************************
ECHO -- Create a SQL*PLUS script for the datafile backups
ECHO **********************************************************
ECHO.
ECHO connect sys/change_oninstall@%ORACLE_SID% as sysdba >%O_SCRIPTPATH%\plus1.sql
ECHO SET heading off; >>%O_SCRIPTPATH%\plus1.sql
ECHO SET feedback off; >>%O_SCRIPTPATH%\plus1.sql
ECHO SET linesize 1000; >>%O_SCRIPTPATH%\plus1.sql
ECHO spool %O_SCRIPTPATH%\svrmgr1.sql; >>%O_SCRIPTPATH%\plus1.sql
ECHO select 'connect sys/change_oninstall@%ORACLE_SID% as sysdba' from dual;>>%O_SCRIPTPATH%\plus1.sql
ECHO select 'alter system switch logfile;' from dual; >>%O_SCRIPTPATH%\plus1.sql
ECHO select 'alter tablespace '^|^|tablespace_name^|^|' begin backup;'^|^|' >>%O_SCRIPTPATH%\plus1.sql
ECHO '^|^|'host start /wait %O_COPY% '^|^|file_name^|^|' %O_BACKPATH%;'^|^|' >>%O_SCRIPTPATH%\plus1.sql
ECHO '^|^|'alter tablespace '^|^|tablespace_name^|^|' end backup;' from dba_data_files; >>%O_SCRIPTPATH%\plus1.sql
ECHO select 'alter system switch logfile;' from dual; >>%O_SCRIPTPATH%\plus1.sql
ECHO select 'exit;' from dual; >>%O_SCRIPTPATH%\plus1.sql
ECHO exit; >>%O_SCRIPTPATH%\plus1.sql

ECHO.
ECHO **********************************************************
ECHO -- Run the sql*plus script to create the svrmgr1.sql script
ECHO **********************************************************
ECHO.

:: %O_PLUS% is the sqlplus.exe path,sqlplus.exe invoke the plus2.sql to generate svrmgr2.sql scripts
%O_PLUS% @%O_SCRIPTPATH%\plus1.sql

ECHO.
ECHO **********************************************************
ECHO -- Create a SQL*PLUS script for the control files
ECHO **********************************************************
ECHO.
ECHO connect sys/change_on_install@%ORACLE_SID% as sysdba >%O_SCRIPTPATH%\plus2.sql
ECHO SET heading off; >>%O_SCRIPTPATH%\plus2.sql
ECHO SET feedback off; >>%O_SCRIPTPATH%\plus2.sql
ECHO SET linesize 1000; >>%O_SCRIPTPATH%\plus2.sql
ECHO spool %O_SCRIPTPATH%\svrmgr2.sql; >>%O_SCRIPTPATH%\plus2.sql
ECHO select 'connect sys/change_oninstall@%ORACLE_SID% as sysdba' from dual;>>%O_SCRIPTPATH%\plus2.sql
ECHO select 'alter database backup controlfile to '''^|^|'%O_BACKPATH%\'^|^|substr(name,instr(name,'\',-1)+1)^|^|''' REUSE;' from v$controlfile; >>%O_SCRIPTPATH%\plus2.sql
ECHO select 'alter database backup controlfile to trace;' from dual; >>%O_SCRIPTPATH%\plus2.sql
ECHO select 'exit;' from dual; >>%O_SCRIPTPATH%\plus2.sql
ECHO spool off; >>%O_SCRIPTPATH%\plus2.sql
ECHO exit; >>%O_SCRIPTPATH%\plus2.sql

ECHO.
ECHO **********************************************************
ECHO -- Run the sql*plus script to create the svrmgr2.sql scripts
ECHO **********************************************************
ECHO.

::sqlplus.exe invoke the plus2.sql to generate svrmgr2.sql scripts
%O_PLUS% @%O_SCRIPTPATH%\plus2.sql

ECHO.
ECHO **********************************************************
ECHO -- Hot Backup Complete
ECHO **********************************************************
ECHO.
goto END_OF_FILE;

REM ***************************************************************************
REM USER HELP
REM ***************************************************************************
:HELP
ECHO.
ECHO HOT_GEN.CMD Usage:
ECHO Enter HOT_GEN SCRIPT_TARGET BACKUP_TARGET
ECHO where SCRIPT_TARGET is the location for the backup
ECHO scripts e.g. c:\oraback\sid\HOT
ECHO and BACKUP_TARGET is the location for the Oracle datafile backups when batch is executed
ECHO.
goto END_OF_FILE

:HELP2
ECHO.
ECHO Error - Cannot write to %O_BACKPATH%
ECHO.
goto END_OF_FILE

REM ***************************************************************************
REM HANDLE ERRORS HERE
REM ***************************************************************************
findstr /in "error" %O_BACKPATH%\backup.log
&& findstr /in "error" %O_BACKPATH%\backup.log >%O_BACKPATH%\error.log
findstr /in "ora-" %O_BACKPATH%\backup.log
&& findstr /in "ora-" %O_BACKPATH%\backup.log >%O_BACKPATH%\error.log
findstr /in "cannot" %O_BACKPATH%\backup.log
&& findstr /in "cannot" %O_BACKPATH%\backup.log >%O_BACKPATH%\error.log
findstr /in "not logged" %O_BACKPATH%\backup.log
&& findstr /in "not logged" %O_BACKPATH%\backup.log >%O_BACKPATH%\error.log
findstr /in "failure" %O_BACKPATH%\backup.log
&& findstr /in "failure" %O_BACKPATH%\backup.log >%O_BACKPATH%\error.log
if exist %O_BACKPATH%\error.log c:\ntreskit\logevent -s E "BACKUP FAILURE!"
pause

endlocal
:END_OF_FILE


PS:上传到FTP和压缩部分没有测试,根据实际环境取舍,压缩时给出压缩软件的详细路径(D:\..\..\*.exe)和类型(7zip、rar..........)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: