---
title: "Oracle数据库备份与恢复之日志备份"
author: "hecal"
date: "2007-12-26T16:39:00Z"
tags: []
slug: "oracle"
draft: false
lastmod: "2011-12-07T18:10:01Z"
id: 1296522
url: "https://hecal.iteye.com/blog/1296522"
---

# Oracle数据库备份与恢复之日志备份

　　以前大家了解了[Oracle](http://www.qqread.com/oracle/index.html)数据库的在线[备份](http://www.qqread.com/tag/1152/index.html)，另外，进行数据库全备份后需要备份归档的日志文件。

**1. 建立[数据库备份](http://www.qqread.com/tag/1829/index.html)的脚本**

在上面建立的目录下建立如下的两个脚本。

1.1 建立&lt;nobr oncontextmenu="return false;" onmousemove="kwM(1);" id="key1" onmouseover="kwE(event,1, this);" style="COLOR: #6600ff; BORDER-BOTTOM: #6600ff 1px dotted; BACKGROUND-COLOR: transparent; TEXT-DECORATION: underline" onclick="return kwC();" onmouseout="kwL(event, this);" target="\_blank"&gt;执行&lt;/nobr&gt;备份任务的脚本(批处理文件）

文件内容如下：

@echo OFF  
Rem ====================================  
Rem NAME - archive\_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](http://www.qqread.com/keys/os/index.html) copy commands  
Rem on closed (cold) database  
Rem MODIFIED - ksmith 01/01/02 Original File  
Rem ===============================

Rem For Windows, set environment variables for the root path.  
set ORACLE\_SID=TSTDB  
set ORACLE\_CONNECTSTRING=tstdb  
set ORACLE\_BASE=c:/oracle  
set ORACLE\_HOME=%ORACLE\_BASE%/ora92  
set ORACLE\_DATA=%ORACLE\_BASE%/oradata/%ORACLE\_SID%  
set ORACLE\_ADMIN=%ORACLE\_BASE%/admin/%ORACLE\_SID%

%ORACLE\_HOME%/bin/sqlplus /nolog @archive\_backup.sql &gt; archive\_backup.log

1.2 建立完成备份认为的命令文件(sqlplus内执行的sql脚本）

文件内容如下：

Rem ===============================  
Rem NAME - archive\_backup.sql  
Rem FUNCTION - Creates a backup command file for a User Managed Archive Log backup  
Rem NOTES - This script will create a script and run OS copy commands  
Rem on an open or closed database.  
Rem MODIFIED - Mr. Tianliang Guo 2002/08/19  
Rem ==========================================

Rem Set SQL\*Plus variables to manipulate output  
set feedback off  
set pagesize 0  
set heading off  
set verify off  
set linesize 150  
set trimspool on

Rem Set SQL\*Plus user variables used in script  
define adm = 'sys'  
define pwd = 'oracle9i'

Rem for Windows  
define dir = 'C:/oracle/admin/TSTDB/backup/archive log'  
define fil = 'archive\_backup\_commands.sql'  
define spo = 'archive\_backup\_output.lst'  
define cpy = 'copy'

Rem login database  
connect &adm/&pwd as sysdba;

Rem Create a file containing all the file copy commands needed for open physical backup  
spool &  
prompt spool &;  
prompt alter system archive log all;;

SELECT 'host &cpy '||name||' &dir'  
FROM v$archived\_log  
WHERE completion\_time &gt;= trunc(sysdate)-1  
AND completion\_time &lt; trunc(sysdate);

prompt spool off;;  
spool off;

Rem Run the copy file commands from the operating system  
rem @&  
exit;

**2. 修改教本定义的变量**

在运行教本中定义的一些变量，将其改为本地[数据库安装](http://www.qqread.com/z/database/del/index.html)环境的变量。

需要修改的变量有,

在close\_Backup\_run.bat脚本中：  
set ORACLE\_SID=TSTDB  
set ORACLE\_CONNECTSTRING=tstdb  
set ORACLE\_BASE=c:/oracle  
set ORACLE\_HOME=%ORACLE\_BASE%/ora81

在close\_backup.sql脚本中：  
define adm = 'sys'  
define pwd = '&lt;nobr oncontextmenu="return false;" onmousemove="kwM(2);" id="key2" onmouseover="kwE(event,2, this);" style="COLOR: #6600ff; BORDER-BOTTOM: #6600ff 1px dotted; BACKGROUND-COLOR: transparent; TEXT-DECORATION: underline" onclick="return kwC();" onmouseout="kwL(event, this);" target="\_blank"&gt;oracle8i&lt;/nobr&gt;'  
define dir = 'C:/oracle/admin/TSTDB/backup'  
define ctl = '&dir/control.ctl'

**3. 执行数据库备份任务**

在$ORACLE\_BASE/admin/admin\_script目录下运行open\_Backup\_run.bat

**4. 执行自动备份**

在windows&lt;nobr oncontextmenu="return false;" onmousemove="kwM(0);" id="key0" onmouseover="kwE(event,0, this);" style="COLOR: #6600ff; BORDER-BOTTOM: #6600ff 1px dotted; BACKGROUND-COLOR: transparent; TEXT-DECORATION: underline" onclick="return kwC();" onmouseout="kwL(event, this);" target="\_blank"&gt;系统&lt;/nobr&gt;中添加计划任务，定期运行open\_Backup\_run.bat