43
6 Copyright © 2007, Oracle. All rights reserved. Performing User-Managed Backup and Recovery

Les 06 Perform Rec

Embed Size (px)

DESCRIPTION

Oracle DBA Interview Questions and Answers, CBT, Oracle E-books, Certifications http://oracledba-interview.blogspot.com

Citation preview

Page 1: Les 06 Perform Rec

6Copyright © 2007, Oracle. All rights reserved.

Performing User-Managed Backup and Recovery

Page 2: Les 06 Perform Rec

Copyright © 2007, Oracle. All rights reserved.6 - 2

Objectives

After completing this lesson, you should be able to:

• Describe the difference between user-managed and server-managed backup and recovery

• Manually back up a data file, tablespace, and database

• Back up and recover a control file

• Perform user-managed complete database recovery

• Perform user-managed incomplete database recovery

• Recover from a lost TEMP file

• Recover from a lost redo log group

Page 3: Les 06 Perform Rec

Copyright © 2007, Oracle. All rights reserved.6 - 3

Restoring and Recovering

Restore

Recover

Redo log

Page 4: Les 06 Perform Rec

Copyright © 2007, Oracle. All rights reserved.6 - 4

Causes of File Loss

File loss can be caused by:

• User error

• Application error

• Media failure

Page 5: Les 06 Perform Rec

Copyright © 2007, Oracle. All rights reserved.6 - 5

Critical Versus Noncritical

A noncritical file loss is one where the database can continue to function.

You fix the problem by taking one of these actions:

• Create a new file.

• Rebuild the file.

• Recover the lost or damaged file.

Page 6: Les 06 Perform Rec

Copyright © 2007, Oracle. All rights reserved.6 - 6

Losing a TEMPFILE

SQL statements that require TEMP space to execute fail if one of the tempfiles is missing.

SQL> select * from big_table order by 1,2,3,4,5,6,7,8,9,10,11,12,13;select * from big_table order by 1,2,3,4,5,6,7,8,9,10,11,12,13 *ERROR at line 1:ORA-01565: error in identifying file '/u01/app/oracle/oradata/orcl/temp01.dbf'ORA-27037: unable to obtain file statusLinux Error: 2: No such file or directory

Page 7: Les 06 Perform Rec

Copyright © 2007, Oracle. All rights reserved.6 - 7

Recovering from a TEMPFILE Loss

Passwordfile

Parameter file

SYSTEM

SYSAUX

UNDO

USERS

INDEX

Redo logfile 1B

Redo logfile 1A

Redo logfile 2B

Redo logfile 2A

Controlfiles

TEMP

Page 8: Les 06 Perform Rec

Copyright © 2007, Oracle. All rights reserved.6 - 8

Log Group Status: Review

A redo log group has a status of one of the following values at any given time:

• CURRENT: The LGWR process is currently writing redo data to it.

• ACTIVE: It is no longer being written to, but it is still required for instance recovery.

• INACTIVE: It is no longer being written to, and it is no longer required for instance recovery.

Page 9: Les 06 Perform Rec

Copyright © 2007, Oracle. All rights reserved.6 - 9

Recovering from the Loss of aRedo Log Group

Groupstatus

Inactive

Active

Fixmedia?

Yes

No

Clear log file Performcheckpoint

CKPTsuccessful?

Yes

Done

No

Restore andrecover

until cancel

Start

Current

Page 10: Les 06 Perform Rec

Copyright © 2007, Oracle. All rights reserved.6 - 10

Clearing a Log File

Log filearchived?

Yes

No

Start

ALTER DATABASE CLEAR LOGFILE ...

ALTER DATABASE CLEAR LOGFILE UNARCHIVED ...

Needed fordata file?

No

ALTER DATABASE CLEAR LOGFILE UNARCHIVED ... UNRECOVERABLE DATAFILE

Yes

Page 11: Les 06 Perform Rec

Copyright © 2007, Oracle. All rights reserved.6 - 11

Re-Creating Indexes

Use options to reduce the time it takes to create the index:

• PARALLEL• NOLOGGING

SQL> CREATE INDEX rname_idx 2 ON hr.regions (region_name) 3 PARALLEL 4;

Page 12: Les 06 Perform Rec

Copyright © 2007, Oracle. All rights reserved.6 - 13

Recovering from a Lost Index Tablespace

• A tablespace that contains only indexes may be recovered without performing a RECOVER task.

• If a data file that belongs to an index-only tablespace is lost, it may be simpler to re-create the tablespace and re-create the indexes.

Page 13: Les 06 Perform Rec

Copyright © 2007, Oracle. All rights reserved.6 - 14

Remote databaseadministration

Local databaseadministration

Yes Yes

No

No

Do youhave a secureconnection?

Use OSauthentication.

Use apassword file.

Authentication Methodsfor Database Administrators

Do you want to use OS

authentication?

Page 14: Les 06 Perform Rec

Copyright © 2007, Oracle. All rights reserved.6 - 15

Re-Creating a Password Authentication File

$ orapwd file=$ORACLE_HOME/dbs/orapworcl password=ora entries=5

SQL> grant sysdba to admin2;grant sysdba to admin2*ERROR at line 1:ORA-01994: GRANT failed: password file missing or disabled

To recover from the loss of a password file:

1. Re-create the password file by using orapwd.

2. Add users to the password file and assign appropriate privileges to each user.

Page 15: Les 06 Perform Rec

Copyright © 2007, Oracle. All rights reserved.6 - 17

Comparing Complete and Incomplete Recovery

Recovery can have two kinds of scope:

• Complete recovery: Brings the database up to the present, including all committed data changes made to the point in time when the recovery was requested

• Incomplete recovery: Brings the database up to a specified point in time in the past, before the recovery operation was requested

Recoverytask startedat this time

Completerecovery

Incompleterecovery

Missing transactionsafter incomplete recovery

Restore fromthis backup

Time ofcrash

Page 16: Les 06 Perform Rec

Copyright © 2007, Oracle. All rights reserved.6 - 18

Complete Recovery Process

Restoreddata files

Data files containingcommitted and uncommitted

transactionsRecovereddata files

Changes appliedUndo applied

6

5

3

2

1

Archivedlog Archived

log OnlineRedo log

4

Databaseopened

Page 17: Les 06 Perform Rec

Copyright © 2007, Oracle. All rights reserved.6 - 19

Incomplete Recovery Process

Data files containingcommitted anduncommitted

transactions up to PIT

PIT-recovereddata files

Archivedlog Archived

log OnlineRedo logX

X

Changes applied to point in time (PIT)

Restored datafiles from as far

back as necessary

Undo applied

6

5

31

24

Databaseopened

Page 18: Les 06 Perform Rec

Copyright © 2007, Oracle. All rights reserved.6 - 21

Types of Backup and Recovery Practices

Types of database backup and recovery are:

• User-managed: Does not use RMAN.– Uses OS commands to move files around– Keeps some records of backup activity manually

• Server-managed: Uses RMAN

Page 19: Les 06 Perform Rec

Copyright © 2007, Oracle. All rights reserved.6 - 22

Performing a User-ManagedBackup of the Database

ARCHIVELOGmode?

Shutdown Put tablespaces into backup mode

Copy files

No Yes

Page 20: Les 06 Perform Rec

Copyright © 2007, Oracle. All rights reserved.6 - 23

The Need for Backup Mode

Database block

A DML statement updatesa database block:

Different parts of the block arewritten to at different times:

t2t1 t3

Copydata file

whileonline

If the block is copied at time t2, then the block is fractured.

Page 21: Les 06 Perform Rec

Copyright © 2007, Oracle. All rights reserved.6 - 24

Identifying Files to Manually Backup

SQL> select name from v$datafile;

NAME------------------------------------------------------------------------/u01/app/oracle/oradata/ORCL/datafile/o1_mf_system_36mky81f_.dbf/u01/app/oracle/oradata/ORCL/datafile/o1_mf_sysaux_36mky81p_.dbf/u01/app/oracle/oradata/ORCL/datafile/o1_mf_undotbs1_36mky857_.dbf/u01/app/oracle/oradata/ORCL/datafile/o1_mf_users_36mky876_.dbf/u01/app/oracle/oradata/ORCL/datafile/o1_mf_example_36ml2cmh_.dbf/u01/app/oracle/oradata/ORCL/datafile/survey01.dbf

SQL> select name from v$controlfile;

NAME------------------------------------------------------------------------/u01/app/oracle/oradata/ORCL/controlfile/o1_mf_36ml1f8x_.ctl/u01/app/oracle/flash_recovery_area/ORCL/controlfile/o1_mf_36ml1fkk_.ctl

Page 22: Les 06 Perform Rec

Copyright © 2007, Oracle. All rights reserved.6 - 25

Manually Backing Up a NOARCHIVELOG Database

• Shut down the database:SQL> SHUTDOWN IMMEDIATE

$ cp $ORACLE_BASE/ORCL/datafile/*.dbf \> /u02/backup/datafile

SQL> STARTUP

• Copy the data files to the backup location:

• Start up the database:

Page 23: Les 06 Perform Rec

Copyright © 2007, Oracle. All rights reserved.6 - 26

Manually Backing Up an ARCHIVELOG Database

SQL> ALTER TABLESPACE users BEGIN BACKUP;

• Identify tablespaces and their datafiles:

$ cp $ORACLE_HOME/oradata/orcl/users*.dbf /u02/backup/datafile

• Copy the data files for that tablespace to the backup location:

• Bring the tablespace out of backup mode:SQL> ALTER TABLESPACE users END BACKUP;

SQL> select file_name, tablespace_name from dba_data_files;FILE_NAME TABLESPACE_NAME--------------------------------------------- ---------------/u01/app/oracle/oradata/orcl/users01.dbf USERS/u01/app/oracle/oradata/orcl/users02.dbf USERS/u01/app/oracle/oradata/orcl/undotbs1.dbf UNDOTBS1/u01/app/oracle/oradata/orcl/sysaux01.dbf SYSAUX/u01/app/oracle/oradata/orcl/system01.dbf SYSTEM/u01/app/oracle/oradata/orcl/example01.dbf EXAMPLE

For each tablespace:

• Put the tablespace into backup mode:

Page 24: Les 06 Perform Rec

Copyright © 2007, Oracle. All rights reserved.6 - 27

Backing Up the Control File

SQL> ALTER DATABASE BACKUP CONTROLFILE TO 2> '/u01/backup/controlfile.bak';

Database altered.

Back up the control file:

• As an image copy, to a specifically named file:

• By generating a script that re-creates it, in a trace file:

SQL> ALTER DATABASE BACKUP CONTROLFILE TO TRACE;

Database altered.

Page 25: Les 06 Perform Rec

Copyright © 2007, Oracle. All rights reserved.6 - 28

Performing User-ManagedComplete Database Recovery: Overview

User-managed complete database recovery:

• Recovers the database to the most recent SCN

• Can be done with the entire database at once, or a data file or tablespace at a time

• Requires a current control file

• Requires backups of all files to be recovered

• Requires all archive logs up to the present

Page 26: Les 06 Perform Rec

Copyright © 2007, Oracle. All rights reserved.6 - 29

Performing Complete Closed DatabaseRecovery: Overview

Databaseopen?

Shutdown.

No

Yes

Query for files to recover.

Query for needed archive logs.

Restore damaged files and archive logs.

Repair hardware.

Mount database.

Open database.

Recover data files.Bring data files online.

Page 27: Les 06 Perform Rec

Copyright © 2007, Oracle. All rights reserved.6 - 30

Identifying Recovery-Related Files

SQL> SELECT file#, error FROM v$recover_file;

SQL> SELECT archive_name FROM v$recovery_log;

• Identify data files that need to be recovered:

• Identify archive log files that are required to complete recovery:

Page 28: Les 06 Perform Rec

Copyright © 2007, Oracle. All rights reserved.6 - 31

Restoring Recovery-Related Files

Data files

Archive logs

/disk2/datafile.dbf/disk1/datafile.dbf

ONLINE

1

2

3

Page 29: Les 06 Perform Rec

Copyright © 2007, Oracle. All rights reserved.6 - 33

Applying Redo Data

SQL> RECOVER AUTOMATIC FROM '/u01/arch_temp' DATABASE;

1. Apply redo data using the RECOVER command:

Apply each redo log without prompting. Alternate location for

restored archive log files

Could be DATABASE, TABLESPACE, or DATAFILE

2. Open the database:

SQL> ALTER DATABASE OPEN;

Page 30: Les 06 Perform Rec

Copyright © 2007, Oracle. All rights reserved.6 - 34

Performing Complete Open Database Recovery

Query for files to recover.

Query for needed archive logs.

Restore damaged files and archive logs.

Diagnose media problem.

Recover data files.

Bring tablespaces online.

Take damaged data files offline.

Can bringdata files online?

Yes

No

Done

Page 31: Les 06 Perform Rec

Copyright © 2007, Oracle. All rights reserved.6 - 36

Performing User-ManagedIncomplete Recovery: Overview

Recover the database to a past point in time in the following situations:

• You want the database to be in the state that existed before a user error or an administrative error occurred.

• The database contains corrupt blocks.

• You are unable to perform complete database recovery because some of the redo log files are missing.

• You want to create a test database that is in the state at some time in the past.

• One or more unarchived redo log files and a data file are lost.

Page 32: Les 06 Perform Rec

Copyright © 2007, Oracle. All rights reserved.6 - 37

Choosing an Incomplete Recovery Method

Indicate when to stop applying redo data by:

• Specifying a time at which to stop

• Specifying an SCN at which to stop

• Issuing a CANCEL command while the recovery is executing

Applying redo data

SCN = 1200

Time = August 23, 2007 01:00 AM

CANCEL

Now

Page 33: Les 06 Perform Rec

Copyright © 2007, Oracle. All rights reserved.6 - 38

Performing User-ManagedIncomplete Recovery

• Recover a database until time:

• Recover a database until cancel:

SQL> RECOVER DATABASE UNTIL CANCEL;

SQL> RECOVER DATABASE UNTIL

2 TIME '2005-12-14:12:10:03';

Page 34: Les 06 Perform Rec

Copyright © 2007, Oracle. All rights reserved.6 - 40

Performing User-Managed Incomplete Recovery: Steps

To perform user-managed incomplete recovery, follow these steps:

1. Shut down the database.

2. Restore data files.

3. Mount the database.

4. Recover the database.

5. Open the database with the RESETLOGS option.

Page 35: Les 06 Perform Rec

Copyright © 2007, Oracle. All rights reserved.6 - 41

User-Managed Time-Based Recovery: Example

This is the scenario:

• A job ran in error, and its effects have to be undone.

• This happened 15 minutes ago, and there has been little database activity since then.

• You decide to perform incomplete recovery to restore the database back to its state as of 15 minutes ago.

SQL> SHUTDOWN IMMEDIATE$ cp /BACKUP/* /u01/db01/ORADATASQL> STARTUP MOUNTSQL> RECOVER DATABASE UNTIL TIME '2005-11-28:11:44:00';SQL> ALTER DATABASE OPEN RESETLOGS;

Page 36: Les 06 Perform Rec

Copyright © 2007, Oracle. All rights reserved.6 - 43

User-Managed Cancel-Based Recovery: Example

The scenario is the same as the one for the time-based example, except for these findings:

• Redo logs are not multiplexed.

• One of the online redo logs is missing.

• The missing redo log is not archived.

• The redo log contained information from 11:34 AM.

• Twenty-six minutes of data are lost.

• Users can reenter their data manually.

Page 37: Les 06 Perform Rec

Copyright © 2007, Oracle. All rights reserved.6 - 44

User-Managed Cancel-Based Recovery: Example

Recover the database as follows:

• Shut down the database.

• Restore all data files from the most recent backup.

• You already have a valid backup, so mount the database.

• Execute RECOVER DATABASE UNTIL CANCEL.

• Execute ALTER DATABASE OPEN RESETLOGS to open the database.

Page 38: Les 06 Perform Rec

Copyright © 2007, Oracle. All rights reserved.6 - 45

Recovering a Read-Only Tablespace

Special user-managed backup and recovery considerations for a read-only tablespace:

• You do not have to put it in backup mode in order to make a copy of its data files.

• You do not have to take the tablespace or data file offline before making a copy of it.

Page 39: Les 06 Perform Rec

Copyright © 2007, Oracle. All rights reserved.6 - 46

Recovering NOLOGGING Database Objects

Redo log

SQL> CREATE TABLE sales_copy NOLOGGING;SQL> INSERT /*+ APPEND */ INTO sales_copy 2 SELECT * FROM sales_history;

Page 40: Les 06 Perform Rec

Copyright © 2007, Oracle. All rights reserved.6 - 47

Recovering from the Loss ofAll Control File Copies: Overview

Current Backup

Available Restore backup control file, perform complete recovery, OPEN RESETLOGS

Restore backup control file, perform complete recovery, OPEN RESETLOGS

Unavailable Re-create control file, OPEN RESETLOGS

Restore backup control file, perform incomplete recovery, OPEN RESETLOGS

Online log status Data file status

Page 41: Les 06 Perform Rec

Copyright © 2007, Oracle. All rights reserved.6 - 48

Recovering the Control Fileto the Default Location

Repair hardware.

Yes

Databaseopen?

SHUTDOWN ABORT

Restore control file.

STARTUP MOUNT

Start database recovery.

Archivelogmissing?

No

Yes Specify online log.

Open databaseusing

RESETLOGS.

No

Page 42: Les 06 Perform Rec

Copyright © 2007, Oracle. All rights reserved.6 - 49

Summary

In this lesson, you should have learned how to:

• Describe the difference between user-managed and server-managed backup and recovery

• Manually back up a data file, tablespace, and database

• Back up and recover a control file

• Perform user-managed complete database recovery

• Perform user-managed incomplete database recovery

• Recover from a lost TEMP file

• Recover from a lost redo log group

Page 43: Les 06 Perform Rec

Copyright © 2007, Oracle. All rights reserved.6 - 50

Practice 6 Overview: Performing User-Managed Recovery

This practice covers the following topics:

• Backing up a NOARCHIVELOG database

• Recovering from lost control files

• Recovering from the loss of a redo log group