分类: 服务器与存储
2008-06-16 19:19:17
1. 检查你的数据库是否运行在Archive模式下。
以system连接数据库运行以下的查询语句。
SQL> select name,log_mode from v$database;
NAME LOG_MODE
--------- ------------
DEMO ARCHIVELOG
如果你的log_mode显示为archivelog, OK你的数据库可以进行在线的备份。否则参考文档修改数据为archive mode。
2. 建立教本管理目录 C:\\admin\admin script
3. 建立数据库备份的脚本
在上面建立的目录下建立如下的两个脚本。
3.1 建立执行备份任务的脚本(批处理文件)
文件内容如下:
@echo OFF
Rem ===========================================================================
Rem NAME - Open_Backup_Run.bat
Rem FUNCTION - Creates a backup script for a User Managed backup
Rem NOTES - This script will create a script and run OS copy commands
Rem on closed (cold) database
Rem MODIFIED - Tianliang Guo 02/08/12
Rem ===========================================================================
Rem For , set environment variables for the root path.
set ORACLE_SID=TSTDB
set ORACLE_CONNECTSTRING=tstdb
set ORACLE_BASE=c:\
set ORACLE_HOME=%ORACLE_BASE%\ora81
set ORACLE_DATA=%ORACLE_BASE%\oradata\%ORACLE_SID%
set ORACLE_ADMIN=%ORACLE_BASE%\admin\%ORACLE_SID%
%ORACLE_HOME%\bin\sqlplus /nolog @Open_Backup.sql > Open_Backup.sql.log
3.2 建立完成备份认为的命令文件(sqlplus内执行的sql脚本)
文件内容如下:
Rem =====================================================================================
Rem NAME - Open_Backup.sql
Rem FUNCTION - Creates a backup command file for a User Managed Open database backup
Rem NOTES - This script will create a script and run OS copy commands
Rem on an open (hot) database. The datafile backups will be inconsistent.
Rem MODIFIED - Mr. Tianliang Guo 2002/08/11
Rem =====================================================================================
Rem
Rem Set SQL*Plus variables to manipulate output
Rem
set heading off
set pagesize 0
set linesize 600
set verify off
set trimspool on
set feedback off
Rem Set SQL*Plus user variables used in script
define adm = 'sys'
define pwd = '8i'
Rem User variables
define dir = 'C:\\admin\TSTDB\backup'
define fil = 'open_backup_commands.sql'
define spo = 'open_backup_output.lst'
define ctl = '&dir\control.ctl'
define cpy = 'ocopy'
Rem login database
connect &adm/&pwd as sysdba;
Rem Create a file containing all the file copy commands needed for open user managed backup
set serveroutput on
spool &fil
prompt spool &spo
prompt archive log list;;
prompt alter system switch logfile;;
prompt alter system archive log all;;
DECLARE
CURSOR cur_tablespace IS
SELECT tablespace_name
FROM dba_tablespaces;
CURSOR cur_datafile (tn VARCHAR) IS
SELECT file_name
FROM dba_data_files
WHERE tablespace_name = tn;
BEGIN
FOR ct IN cur_tablespace LOOP
dbms_output.put_line ('alter tablespace '||ct.tablespace_name||' begin backup;');
FOR cd IN cur_datafile (ct.tablespace_name) LOOP
dbms_output.put_line ('host &cpy '||cd.file_name||' &dir');
END LOOP;
dbms_output.put_line ('alter tablespace '||ct.tablespace_name||' end backup;');
END LOOP;
END;
/
prompt alter system switch logfile;;
prompt alter database backup controlfile to '&ctl' REUSE;;
prompt alter database backup controlfile to trace;;
prompt archive log list;;
prompt spool off
spool off;
Rem Run the copy file commands from the operating system
@&
exit;
4. 修改教本定义的变量
在运行教本中定义的一些变量,将其改为本地数据库安装环境的变量。
需要修改的变量有,
在close_Backup_run.bat脚本中:
set ORACLE_SID=TSTDB
set ORACLE_CONNECTSTRING=tstdb
set ORACLE_BASE=c:\
set ORACLE_HOME=%ORACLE_BASE%\ora81
在close_backup.sql脚本中:
define adm = 'sys'
define pwd = '8i'
define dir = 'C:\\admin\TSTDB\backup'
define ctl = '&dir\control.ctl'
5. 执行数据库备份任务
在$ORACLE_BASE\admin\admin_script目录下运行open_Backup_run.bat
6. 执行自动备份
在windows系统中添加计划任务,定期运行open_Backup_run.bat