Академический Документы
Профессиональный Документы
Культура Документы
Recovering a
database
22
C H A P T E R
✦ ✦ ✦ ✦
In This Chapter
Restoring a
NOARCHIVELOG
restoring an Oracle database. You have to know the difference
mode database
between restoring a database that runs in NOARCHIVELOG
mode and one that runs in ARCHIVELOG mode. You need
to know how to handle the redo log files, and you need to Requiring media
understand media recovery. In this chapter, you learn how recovery
to restore lost datafiles, and then how to recover the database
to the point in time at which the files were lost. Recovering from a
lost datafile
Terminating an
Requiring Recovery incomplete recovery
Rarely would you ever lose an entire Oracle database. It’s far Restoring a database
more common to have a single drive go bad, causing you to from an export
lose just the files that are on that drive. How you recover
from such a loss depends largely on whether your database ✦ ✦ ✦ ✦
is running in ARCHIVELOG mode. If you’re not running in
ARCHIVELOG mode and you lose a database file, your only
option is to restore the entire database from the most recent
backup.
Any changes made since the backup was performed are lost.
In addition, you must shut down the entire database while it
is being restored. Because it’s usually not acceptable in a
production setting to lose data or to shut down the database
for an extended period, most Oracle production databases are
run in ARCHIVELOG mode.
recovery to bring those files up to date. You can do all this while the database is
running, and without any committed changes being lost.
In the context of Oracle, the term recovery has a very specific meaning. Recovery
refers to the process of reading redo log entries from archived and online redo log
files and applying those changes to a datafile to bring it up to date. Figure 22-1
illustrates this.
Restored datafile is
current as of this point
in time.
Datafile
Log File
Log File
Datafile
After the recovery process
is complete, the datafile is
up-to-date.
Figure 22-1: The recovery process
When you restore a file from a backup, the file represents the state of the database
at the time the file was backed up, not when it was lost. Usually, you want to
recover all the changes that were made during the interim — between the time the
file was backed up and the time it was lost. Because all changes are written to the
log files, it’s possible to read those log files and apply the changes again to the file
that you restored. This is the core process on which all Oracle recovery operations
are based.
4623-6 ch22.f.qc 1/28/00 12:33 PM Page 567
$sqlplus /nolog
With the instance completely shut down, you can proceed to restore the files.
The reason that you have to restore all the control files and datafiles is that Oracle
requires these files to be consistent with respect to each other. If you have one
datafile that’s a day older than another, that’s not consistent. Since you have no
archived logs, you won’t be able to apply changes from those logs to make them
consistent either. Your only option is to restore all the files.
Note NOARCHIVELOG mode is suitable only in situations where you can afford to lose
all the changes that you’ve made since the most recent backup. Development and
test databases are often run in NOARCHIVELOG mode. Read-only databases may
also be run in NOARCHIVELOG mode.
Oracle stores the names and locations of all database files in the control file. The
reason you get an error like this is that Oracle is trying to open a file named in the
control file, and that file cannot be found.
You can use two methods to tell Oracle that you have moved a database file. One
approach is to issue an ALTER DATABASE RENAME FILE command. That command
allows you to change the file names and locations recorded in the database control
file. Another approach to the same task is to re-create the entire control file. Issuing
ALTER DATABASE RENAME FILE commands is probably the safer approach. Creating
a new control file allows you to rename all your files at once, but if you make a
mistake, you could cause more problems, and you could lose data.
Replace original_filename with the full path and file name that Oracle is currently
using. Replace new_filename with the full path and file name representing where the
file is currently located. The format for these strings, and whether they are case-
4623-6 ch22.f.qc 1/28/00 12:33 PM Page 569
sensitive, depends on the operating system. UNIX systems typically have case-
sensitive file names. Windows NT systems do not.
To rename a database file, the database must be mounted but not open. It must be
mounted because the changes need to be recorded in the control file. Oracle opens
the control file when you mount the database. You can’t rename a file when the
database is open because the files would then be in use. You can’t rename files that
are currently open. Listing 22-1 shows the ALTER DATABASE RENAME FILE command
being used to specify that you have renamed USERS01.DBF to USERS0199.DBF.
Database altered.
Note that this command changes the name and location of a file only as recorded in
the database control file. It doesn’t rename the file at the operating-system level,
nor does it physically move the file to the new location. You must perform both of
those tasks yourself. The ALTER DATABASE RENAME FILE command does a search
and replace, taking the original file name that you provided and replacing it with
the new file name.
To issue the ALTER DATABASE RENAME FILE command, you need to know what the
original file names in your database are. How can you find that out? If your database
is operational, you can query the V$DATAFILE view for this information. However, if
you’re restoring the database, it’s not going to be operational, so consider generating
a list of file names in advance. You may want to periodically run a SQL script such as
the following:
SPOOL datafile_list
SELECT name FROM v$datafile;
SPOOL OFF
4623-6 ch22.f.qc 1/28/00 12:33 PM Page 570
You can even run a slightly more complex script periodically that generates the
ALTER DATABASE RENAME FILE commands for you. For example:
SET PAGESIZE 0
SPOOL rename_file_commands.sql
SELECT ‘ALTER DATABASE RENAME FILE ‘
|| ‘’’’ || name || ‘’’ TO ‘’’’;’
FROM v$datafile;
SPOOL OFF
The result of executing this script will be a file containing an ALTER DATABASE
RENAME FILE command for every datafile in your database. The contents will look
like the following:
You could easily edit this file, type in new file names, and then execute it.
If all else fails and you don’t have a list of original file names to work from, you can
always attempt to open the database, get the file name from the error message, and
then rename that file. You would have to repeat this process once for each file that
you move.
This command actually generates a trace file containing the necessary CREATE
CONTROLFILE command, together with other necessary commands. Although you
could write the CREATE CONTROLFILE command yourself, it’s not easy to do,
especially if you don’t have access to the database anymore. You also risk losing
data if you get it wrong, so let Oracle generate it for you. Listing 22-2 provides an
example of a script generated when you back up the control file to trace.
4623-6 ch22.f.qc 1/28/00 12:33 PM Page 571
This script is meant to be executed from Server Manager. If you decide to execute it
from SQL*Plus, you will need to change the pound signs (#) to a double dash (--),
or delete the comment lines entirely, because SQL*Plus doesn’t recognize pound
signs as comment characters. You will also want to edit the trace file and remove all
the messages that Oracle places before and after these commands.
Also before you execute the script, be sure to edit it and change all the file names to
reflect the new locations and names of the database files. If your original control
files still exist, leave in the keyword REUSE, and the new files will be created over
the top of the originals. Otherwise, remove the keyword REUSE to create totally new
files. If you are restoring the database from a backup, you should delete the
RECOVER DATABASE and ALTER DATABASE OPEN commands and open the database
yourself.
RESETLOGS is an option of the ALTER DATABASE OPEN command. One issue that
you may run into when you try to use it is that Oracle will allow the option to be
used only in two cases:
Backup control files are those created with the ALTER DATABASE BACKUP
CONTROLFILE command (but not using the TO TRACE option). Because restoring
from an offline backup doesn’t involve media recovery, to use the RESETLOGS
option, you will need to use a backup control file. What if you don’t have a backup
control file? Fortunately, you can make one. Just follow these steps:
With the backup control file in place, you are now ready to open the database using
the RESETLOGS option.
Note This information applies only to Oracle8 and Oracle8i. Prior releases of Oracle did
not allow you to use RESETLOGS on backup control files.
Listing 22-3 shows backup control files being created on a Windows NT server.
The end result of this cumbersome process is that you replace your database
control files with backup control files. You can now go back into Server Manager,
start up the instance, and open the database using the RESETLOGS option. Consider
this example:
Given the requirement that you issue RESETLOGS in conjunction with a backup
control file, it’s probably best if you routinely make backup control files as part of
your backup process. Then you can use those backup control files when restoring
the database, instead of having to go through the rather cumbersome process
shown here.
The online redo log files allow Oracle to perform media recovery to recover from a
system crash or the sudden failure of a database instance. Whenever you commit a
transaction, Oracle makes certain that the changes from that transaction are written
to the redo log files. The changes may not be written to the datafiles until later. If the
system crashes before the changes can be written to the datafiles, Oracle will detect
the crash when you restart the database, read the lost changes from the redo log
files, and apply them to the datafiles. Media recovery, when performed in response
to a crash like this, is referred to as crash recovery.
You know that Oracle cycles through the set of redo log files for a database, writing
to each file in turn. Throughout all this, Oracle ensures that redo log entries are not
overwritten until their changes have been written to the datafiles. This ensures that
crash recovery can always be performed. Imagine, though, if redo log entries were
4623-6 ch22.f.qc 1/28/00 12:33 PM Page 575
kept indefinitely. You could use such a log to replay all the changes that took place
over a period of days, weeks, or even months. Not only could you recover from a
crash, but you could also recover from losing a file due to drive failure. This is
where archived redo logs come into play.
When you run a database in ARCHIVELOG mode, Oracle makes a copy of each redo
log file as it is filled. These copies, known as archived log files, together with any
online redo log files that have not yet been copied, form a continuous record of all
changes made to the database. If you lose a datafile and are forced to restore it
from a backup, the information in the archived log files can be used to reapply all
the changes to that file that were made since the backup took place. The net effect
is that you suffer zero data loss.
How long archived log files are retained is up to you. In fact, if you’re not using
RMAN and an automated scheduling tool such as what Enterprise Manager
provides, you will need to write your own scripts to purge old archived log files.
As long as you have all the archived log files generated since the most recent full
backup of a database file, you will always be able to recover that file.
Note If the lost file is part of the SYSTEM tablespace, then you won’t be able to keep the
database open while it is being restored.
1. Take the datafile offline if Oracle hasn’t already done that for you.
2. Restore the file from the most recent backup.
3. Recover the file.
4. Bring the file back online.
The process is usually quite painless, especially if you have the necessary archived
log files still on disk and in the directory pointed to by the ARCHIVE_LOG_DEST
parameter.
4623-6 ch22.f.qc 1/28/00 12:33 PM Page 576
STATUS NAME
------- --------------------------------------------------
SYSTEM E:\ORACLE\ORADATA\JONATHAN\SYSTEM01.DBF
OFFLINE E:\ORACLE\ORADATA\JONATHAN\USERS0199.DBF
ONLINE E:\ORACLE\ORADATA\JONATHAN\RBS01.DBF
In this case, you can see that the USERS0199.DBF file is offline. If the file that you’ve
lost is not yet offline, you can take it offline by issuing this command:
ALTER DATABASE
DATAFILE ‘filename’ OFFLINE;
Replace filename with the full path and name of the file, as reported by the
v$datafile view. With the file safely offline, you can proceed to restore and
recover it. Users of the database will still be able to do work that doesn’t require
access to the data in the file that you’ve taken offline.
RECOVER DATABASE
Using RECOVER DATABASE causes Oracle to check all the files and recover any that
need recovering. A more surgical approach would be to use RECOVER DATAFILE
and list the files that you specifically want to recover. The syntax for doing that is
as follows:
Replace filename with the path and name of a file that you want to recover. You
can list multiple files in the command. Separate the file names with commas.
Once connected, you can issue the RECOVER command. As Oracle proceeds to
recover the datafile, it will prompt you each time it needs to access an archived log
file. You can respond to each prompt by pressing the Enter key, or you can use the
keyword AUTO to specify that Oracle proceed without prompting you all the time.
Listing 22-4 shows the use of RECOVER DATAFILE to bring the USERS0199.DBF file
up to date.
...
Log applied.
Media recovery complete.
In this example, the keyword AUTO is used as the response to the first prompt. This
works because all the needed archived log files are still online, and they are still in
the directory pointed to by the ARCHIVE_LOG_DEST parameter.
4623-6 ch22.f.qc 1/28/00 12:33 PM Page 578
If the needed archived log files are on disk but not where Oracle expects them to
be, you can respond to the prompt by supplying the correct path and file name for
the archived log file that Oracle is seeking. If the needed archived log files aren’t on
disk but are instead on a backup medium such as a tape, you need to restore them
to disk before you can recover the datafile. This is why many DBAs keep archived
log files online long enough to cover the period since the most recent full backup.
Database altered.
Now you can sit back, relax, and have a coffee. The file has been recovered, it’s
back online, and the users can work as normal.
You might perform an incomplete recovery for both voluntary and involuntary
reasons. The involuntary reasons include the following:
✦ Loss of an online redo log file. The last part of the recovery process reads
changes from the online redo log files and applies those changes to the files
being recovered. If you’ve lost your online redo log files, then you can’t
recover the changes that were recorded in those files.
✦ Loss of an archived log file. If you lose an archived log file and you have no
backups of that file, then you will be able to recover changes only up to the
point in time represented by that file.
These two points illustrate the need to carefully protect your online redo logs and
to back up your archived log files. The recovery process always proceeds from the
oldest change forward, through the most recent change. If you get to a point where
you are missing a file, that’s where you have to stop.
4623-6 ch22.f.qc 1/28/00 12:33 PM Page 579
Note The first point, that of losing online redo log files, is why Oracle recommends
against backing up those files. If you get flustered in a recovery situation, and
unthinkingly restore redo log files from a backup, you will have totally and utterly
lost your ability to recover up to the point of failure.
✦ A processing error results in lost or damaged data and you want to rerun the
process.
✦ A user error results in important data being deleted.
Both of these scenarios are really the same. Either way, something went wrong at a
certain point in time, and data was corrupted or lost. You might, for example, have
a billing process that runs each night. If a major error occurs, you may want to be
able to rerun the entire billing process again. Incomplete recovery allows you to do
that. You restore the entire database from the last backup and recover it up to the
moment just prior to when the billing process was run. Then you can correct the
source of the error and rerun the process.
Note Incomplete recovery doesn’t always represent the ideal way to recover from pro-
cess failures. If online transactions are taking place at the same time that a batch
process is running, those transactions will be lost if you perform an incomplete
recovery to rerun the batch process.
You specify how you want to approach incomplete recovery when you issue the
RECOVERY command.
4623-6 ch22.f.qc 1/28/00 12:33 PM Page 580
RECOVER DATABASE
[UNTIL CANCEL]
[UNTIL CHANGE scn]
[UNTIL TIME ‘datetime’]
rest_of_command;
After performing an incomplete recovery, you must open the database using the
RESETLOGS option. This defines a new incarnation of the database and tells Oracle
that the data currently in the online redo log files is no longer needed for recovery.
Except for issuing the actual RECOVER command, the process for performing an
incomplete recovery is the same as for a complete recovery. Restore the files, in
this case, all of them; make sure the required archived log files are available; and
issue the command. Listing 22-5 shows a database being restored to its state as of
8:00 am on October 10, 1999.
4623-6 ch22.f.qc 1/28/00 12:33 PM Page 581
Log applied.
Media recovery complete.
4623-6 ch22.f.qc 1/28/00 12:33 PM Page 582
The database has now been brought back to the same state it was in at 8:00 am on
October 10, 1999. All that remains is to open the database using the RESETLOGS
option, as shown in this example:
Database altered.
In spite of all this, if all you have to work with is an export file, you can follow these
steps to use an export file to recover your database to the way it was when the
export was performed.
1. Remove all traces of the database that you are trying to recover. Delete all
datafiles, log files, control files, and so forth.
2. Create a new database from scratch. Create only the SYSTEM tablespace and
some rollback segments. Run the CATALOG.SQL and CATPROC.SQL scripts.
3. Import into the new database using the export file from the old database as
the source. Specify IGNORE=Y on the import command line.
While importing, you may encounter some errors as the Import utility attempts to
create objects owned by SYSTEM that are already present in the new database. The
IGNORE=Y option tells the Import utility to ignore these errors. If everything else
works well, the Import utility should create all your tablespaces, create all your
schema objects, and load those with data.
4623-6 ch22.f.qc 1/28/00 12:33 PM Page 583
For more information on the Import utility, see Chapter 9, “Using Oracle8i’s Import
Utility.” For more information on the Export utility, see Chapter 8, “Using Oracle8i’s
Export Utility.”
Summary
In this chapter, you learned:
✦ If your database is running in noarchivelog mode and you lose a file, your only
recourse is to restore the entire database from the most recent backup. You
will lose all changes made since the backup.
✦ By running your database in archivelog mode, you enable yourself to restore a
file and recover all changes made up until the moment that the file was lost.
Production databases should almost always be run in archivelog mode.
✦ When you restore a noarchivelog mode database, don’t restore the redo log
files. Instead, use the RESETLOGS option when reopening the database.
✦ If you have to restore a file to a new location as the result of a bad disk, have
Oracle record the new location in the database control file. You can do this by
issuing an ALTER DATABASE RENAME FILE command, or you can re-create
the control file completely.
✦ To recover a file that you’ve restored, issue the RECOVER DATABASE
command. For the recovery to be complete, all archived log files generated
since the backup must be available, and you must have all online redo log files
available as well. The last few changes to be applied will always come from
the online redo log files, so it’s important to preserve these.
✦ Crash recovery is the recovery process that Oracle employs after a system
crash. Data is read from the redo log files and applied to the database files to
restore any lost changes. Media recovery provides the same process, but the
term media is used when you restore files from a backup and you recover
using archived log files.
✦ Incomplete recovery is the process of recovering a database to the way it was
at some point in time in the past. After performing incomplete recovery, use
the RESETLOGS option when opening the database.
✦ ✦ ✦
4623-6 ch22.f.qc 1/28/00 12:33 PM Page 584