RMAN stuff
Restore a tablespace
Accidentally dropped a tablespace? Already the first grey hair starts spreading, thinking about all the work with creating some temporary database to restore your backup to in order to regain your tablespace … Stop! You're running Oracle 11g or higer? You do your backups with RMAN? So what?
RMAN> RECOVER TABLESPACE dropped_ts
UNTIL TIME ( to_date('2012-04-20 22:31:54','YYYY-MM-DD HH24:MI:SS') )
AUXILIARY DESTINATION '/opt/freespace';
dropped_ts is the name of the tablespace to restore. The timestamp was taken
from the alert_log (minus one second, to be before the drop), and the
AUXILIARY DESTINATION is where RMAN shall automatically do all the "temporary
database" stuff.
Recovery Advisor
Another new feature coming with Oracle 11g: Let RMAN look for failures, suggest strategies, automatically generate required scripts, and do the job! Say "something's wrong" with your database. First time this happens to you, so you are a bit nervous. Or, even worse: you are not even there, but only some "assistant" who called you on the phone … OK. So now we get it step by step: First thing of course is to start RMAN and connect to the database in question:
$ rman target=/(start RMAN and connect to the problematic database)RMAN> list failure(presents a list of failures (if any), or simply the message "no failure found")RMAN> list failure <id> detail(gives details on the specified error)RMAN> ADVISE FAILURE- Here we go! What shall we do to repair the damage? What possibilities exist, and which seems the best approach?
- Lists up …
- Mandatory manual actions (if any)
- optional manual actions (if any)
- automatic repair options
RMAN> repair failure preview- automatic repair options come as pre-generated script which must simply be executed
- this displays the script, so one can investigate what would be done
RMAN> repair failure [noprompt]- again displays the script shown with repair failure preview, and then asks for confirmation
- if confirmed, script will be executed automatically
- if you do not want to be prompted for anything, append the optional
nopromptparameter
Create a catalog DB
First connect to the database which shall hold the catalog and create the RMAN user:
create user rman identified by topfSecret default tablespace rman quota unlimited on rman;
grant recovery_catalog_owner to rman;
Now use rman to create the catalog:
$ rman
RMAN> connect catalog rman/topfSecret
RMAN> create catalog;
Create Standby (using catalog DB)
On the primary:
- set standby_file_management to „auto“:
alter system set standby_file_management='AUTO'; - standby log files created
- you need n+1 standby logfile groups, where n is the number of your „normal“ redo log groups
- these must be explicitly created with
thread#andunique_group#which do not match any of the existing redo log groups. If you e.g. already have redo log groups 1 to 5, you need 6 standby log groups, starting with
alter database add standby logfile thread 1 group 6 size 500M;
alter database add standby logfile thread 1 group 7 size 500M;…
alter database add standby logfile thread 1 group 11 size 500M;
- Flashback must be enabled (
alter database flashback on;) - enable the Data Guard Broker:
alter system set dg_broker_start=TRUE; - force logging:
alter database force logging;
Now transfer the password file to the standby machine, then start the new
standby instance with a simple parameter file containing nothing but the
db_name – which must be the same as the on primary (e.g. *.db_name='MYDB')
$ export ORACLE_SID=MYDB
$ sqlplus /nolog
SQL> connect / as sysdba
SQL> startup nomount pfile=/path/to/mydb.ora
Prepare a parameter file for RMAN, e.g. duplicate_mydb.rman. <password>
must be your sys password, the connection names must be in your tnsnames.ora,
adjust the service_names as you see fit:
connect target sys/<password>@MYDB_PRIM
connect auxiliary sys/<password>@MYDB_STDBY
duplicate target database for standby
from active database
dorecover
spfile
set db_unique_name='MYDB_STDBY'
set service_names='MYDB,MYDB_STDBY'
set global_names='TRUE'
nofilenamecheck;
Ready to let RMAN do the job:
$ rman cmdfile=duplicate_mydb.rmn
Lean back and watch! Then, do not forget to setup Data Guard ;)
Regular backups via Cron
A simple but effective backup strategy based on the following assumptions:
- you want to be able to quickly recover to any point-in-time within the past 7 days
- your backup location is on some disk (e.g. an NFS export)
- a „second layer“ takes care for „long time archival“ (i.e. picking your files from your backup location and copying them e.g. to tape or another disk), so recovering to an earlier date is possibly by getting files back from there and „re-cataloging“ them with RMAN
- you are using an RMAN catalog database
Initially configure RMAN accordingly:
CONFIGURE RETENTION POLICY TO RECOVERY WINDOW OF 7 DAYS;
CONFIGURE CONTROLFILE AUTOBACKUP ON;
Once a week, you will create a Level-0 backup (full backup to be used with
subsequent incremental Level-1 backups). Your RMAN script for that (e.g. called
level0.rmn will look like this:
run {
allocate channel disk1 type disk format '/mnt/db_backups/%d/%d_full%t.bkp';
backup incremental level 0 database tag "backup_level0";
release channel disk1;
allocate channel disk1 type disk format '/mnt/db_backups/%d/%d_archivelog_%e.bkp';
backup archivelog all not backed up 2 times tag "backup_archivelog";
}
Every other day, you will create a Level-1 backup using level1.rmn:
run {
allocate channel disk1 type disk format '/mnt/db_backups/%d/%d_incremental_%t.bkp';
configure controlfile autobackup on;
backup incremental level 1 database tag "backup_level1";
release channel disk1;
allocate channel disk1 type disk format '/mnt/db_backups/%d/%d_archivelog_%e.bkp';
backup archivelog all not backed up 2 times tag "backup_archivelog";
}
Hourly (except when your Level-0/Level-1 backups are running), you will backup your archive logs with archivelog.rmn:
run {
allocate channel disk1 type disk format '/mnt/db_backups/%d/%d_archivelog_%e.bkp';
configure controlfile autobackup on;
backup archivelog all not backed up 2 times tag "backup_archivelog";
}
And nightly (probably after your Level-0/Level-1 backups have been run), you will do some housekeeping.rmn:
crosscheck backup;
crosscheck archivelog all;
delete noprompt obsolete;
delete noprompt archivelog all backed up 2 times to disk;
Now, to run them via Cron, you will need a shell script running them. Make sure
it sets your Oracle environment properly ($ORACLE_HOME, $ORACLE_SID,
$PATH etc), then have it call rman target / cmdfile=$rmnfile (where
$rmnfile is the corresponding file from above), followed by a echo -e
"resync catalog;\n" | rman target / catalog /@CAT (where CAT is the
TNS-Alias of your catalog database, and has a corresponding Oracle Wallet entry
for password-less login).
Why two steps – and not connecting „target“ and „catalog“ right to start with? This ensures that backups are created even if the catalog database should be temporarily unreachable. In such a case, the sync would simply take place at the next run, nothing lost – the control file will take care for that. As long as you get the catalog database up and running before the control file runs out of space ;)
Register/Unregister a database from catalog
Check if it's already there:
$ sqlplus /@CAT
SQL> select * from rc_database where name='MYDB';
It is there but should be gone? Let's get rid of it:
$ rman
RMAN> connect catalog /@repo
RMAN> list incarnation of database MYDB;
# just one DB ID for this? Easy:
RMAN> unregister database MYDB;
# ouch, multiple already? Time for a cleanup:
RMAN> run
{
set DBID 1567892184;
unregister database mydb;
}
Now let's register our shiny new database:
$ rman target=/ catalog=/@CAT
RMAN> register database;
CONFIGURE RETENTION POLICY TO RECOVERY WINDOW OF 7 DAYS;
CONFIGURE CONTROLFILE AUTOBACKUP ON;
Upgrade Catalog
If you've updated your database (that includes regular quarterly patches), connecting to the catalog might present you with a warning your database is newer than the catalog. Then you know it cannot hurt to upgrade the catalog as well. Make sure to do this while you're not running a backup (or the backup will fail):
$ rman target=/ catalog=/@CAT
RMAN> upgrade catalog;
RMAN> upgrade catalog;
In case you wonder: yes, twice. You will see why when you're doing it: RMAN will ask you to „say again, please“ for confirmation.
For more details and some background, see e.g. RMAN catalog upgrade, why, when and how.
Snippets to deal with Archive Logs
list archivelog until time "to_date('20191201','YYYYMMDD')";list backup completed before "to_date('20191201','YYYYMMDD')";delete archivelog all completed before "to_date('20200301','YYYYMMDD')";
Further Readings
- Rolling Forward Image Copies Using RMAN
- RMAN Recovery using the SWITCH DATABASE TO COPY command
- Oracle RMAN Backup, Recover, and Restore Demos (lots of examples)
- Oracle suggested Backup Strategy (incremental backups with rolling-forward image copies)
- Performing Oracle Advised Recovery
- Advanced RMAN Recovery Techniques
