Wednesday, March 18, 2015

ALTER DATABASE BEGIN BACKUP and ALTER DATABASE END BACKUP

I logged into an Oracle 12.1 database, checked it was in ARCHIVELOG mode then ran the ALTER DATABASE BEGIN BACKUP command. This told Oracle I was about to do a hot backup:

C:\Users\Andrew>sqlplus / as sysdba
 
SQL*Plus: Release 12.1.0.1.0 Production on Wed Mar 18 14:45:56 2015
 
Copyright (c) 1982, 2013, Oracle.  All rights reserved.
 
Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 - 64bit Production
With the Partitioning, OLAP, Advanced Analytics and Real Application Testing options
 
SQL> select log_mode from v$database
  2  /
 
LOG_MODE
------------
ARCHIVELOG
 
SQL> alter database begin backup
  2  /
 
Database altered.
 
SQL>

I copied the database’s datafiles to somewhere safe. I didn’t bother about the control files or the redo log files as I didn’t intend to do anything to them in this example. Clearly, if you try this yourself, you need to work out where the datafiles are beforehand:

C:\app\Administrator\oradata\ORCL1\DATAFILE>copy * Z:\Data\Oracle_Backup
O1_MF_SYSAUX_BDGVW9OT_.DBF
O1_MF_SYSTEM_BDGVZ93W_.DBF
O1_MF_TEMP_BDGW88KG_.TMP
O1_MF_UNDOTBS1_BDGW2MY6_.DBF
O1_MF_USERS_BDGW2LNZ_.DBF
        5 file(s) copied.
 
C:\app\Administrator\oradata\ORCL1\DATAFILE>

I created a MARKER table to prove that the backup and restore had worked as planned:

SQL> create table marker as
  2  select 'Andrew was here' message
  3  from dual
  4  /
 
Table created.
 
SQL>

Then I used the ALTER DATABASE END BACKUP command to tell Oracle the hot backup was finished and closed the database:

SQL> alter database end backup;
 
Database altered.
 
SQL> shutdown
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL>

I deleted the database’s datafiles:

C:\app\Administrator\oradata\ORCL1\DATAFILE>dir
Volume in drive C has no label.
Volume Serial Number is 269C-9AD9
 
Directory of C:\app\Administrator\oradata\ORCL1\DATAFILE
 
27/01/2015  11:05    <DIR>          .
27/01/2015  11:05    <DIR>          ..
18/03/2015  15:09     3,722,452,992 O1_MF_SYSAUX_BDGVW9OT_.DBF
18/03/2015  15:09       964,698,112 O1_MF_SYSTEM_BDGVZ93W_.DBF
18/03/2015  14:27     1,906,319,360 O1_MF_TEMP_BDGW88KG_.TMP
18/03/2015  15:09     1,378,885,632 O1_MF_UNDOTBS1_BDGW2MY6_.DBF
18/03/2015  15:09     4,650,442,752 O1_MF_USERS_BDGW2LNZ_.DBF
               5 File(s) 12,622,798,848 bytes
               2 Dir(s)   6,003,765,248 bytes free
 
C:\app\Administrator\oradata\ORCL1\DATAFILE>del *.*
C:\app\Administrator\oradata\ORCL1\DATAFILE\*.*, Are you sure (Y/N)? y
 
C:\app\Administrator\oradata\ORCL1\DATAFILE>

Then I restored them from the backup which I made earlier. Again, if you are doing this yourself, you need to work out which files have been lost before you can restore them:

Z:\Data\Oracle_Backup>copy *.* C:\app\Administrator\oradata\ORCL1\DATAFILE
O1_MF_SYSAUX_BDGVW9OT_.DBF
O1_MF_SYSTEM_BDGVZ93W_.DBF
O1_MF_TEMP_BDGW88KG_.TMP
O1_MF_UNDOTBS1_BDGW2MY6_.DBF
O1_MF_USERS_BDGW2LNZ_.DBF
        5 file(s) copied.
 
Z:\Data\Oracle_Backup>

Finally, I mounted the database, recovered it, opened it and confirmed that the MARKER table was still there:

Z:\Data\Oracle_Backup>sqlplus / as sysdba
 
SQL*Plus: Release 12.1.0.1.0 Production on Wed Mar 18 15:50:55 2015
 
Copyright (c) 1982, 2013, Oracle.  All rights reserved.
 
Connected to an idle instance.
 
SQL> startup mount
ORACLE instance started.
 
Total System Global Area 1720328192 bytes
Fixed Size                  2403496 bytes
Variable Size            1023411032 bytes
Database Buffers          687865856 bytes
Redo Buffers                6647808 bytes
Database mounted.
SQL> recover database
Media recovery complete.
SQL> alter database open
  2  /
 
Database altered.
 
SQL> select * from marker
  2  /
 
MESSAGE
---------------
Andrew was here
 
SQL>

No comments:

Post a Comment