[20180228]11g删除表空间的恢复2.txt

[20180228]11g删除表空间的恢复2.txt

--//以前曾经做过利用传输表空间恢复数据库,连接如下:
http://blog.itpub.net/267265/viewspace-2141174/
http://blog.itpub.net/267265/viewspace-2141174/

--//在rman下执行如下:
RMAN> transport tablespace tea tablespace destination '/home/oracle/aux' auxiliary destination '/home/oracle/aux' until scn 13276962316;

--//利用建立auxiliary实例,生成特定scn号的scn数据文件(这里只能恢复表空间文件).然后impdp导入回去.
--//为了规避同名问题,建立新的用户,使用参数REMAP_TABLESPACE,REMAP_SCHEMA.

SCOTT@book> grant dba to ttt IDENTIFIED BY ttt;
Grant succeeded.

$ cp /home/oracle/aux1/dmpfile.dmp /u01/app/oracle/admin/book/dpdump/

$ impdp system/oracle dumpfile=dmpfile.dmp transport_datafiles=/home/oracle/aux1/tea01.dbf REMAP_TABLESPACE=TEA:MILK
REMAP_SCHEMA=scott:ttt logfile=impdp.log

--//今天补充测试是否可以不用删除做恢复特定时间点的操作:

1.环境:
SCOTT@book> @ &r/ver1
PORT_STRING                    VERSION        BANNER
------------------------------ -------------- --------------------------------------------------------------------------------
x86_64/Linux 2.4.xx            11.2.0.4.0     Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production

CREATE TABLESPACE TEA DATAFILE
  '/mnt/ramdisk/book/tea01.dbf' SIZE 40M AUTOEXTEND ON NEXT 1M MAXSIZE UNLIMITED
LOGGING
ONLINE
EXTENT MANAGEMENT LOCAL AUTOALLOCATE
BLOCKSIZE 8K
SEGMENT SPACE MANAGEMENT MANUAL
FLASHBACK ON;

SCOTT@book> create table t tablespace tea as select * from all_objects;
Table created.

SCOTT@book> alter system archive log current ;
System altered.

--//使用rman做一个全备.

RMAN> backup database format '/home/oracle/backup/full_20180228_%U.bak';

Starting backup at 2018-02-28 10:19:01
using target database control file instead of recovery catalog
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=3 device type=DISK
allocated channel: ORA_DISK_2
channel ORA_DISK_2: SID=14 device type=DISK
allocated channel: ORA_DISK_3
channel ORA_DISK_3: SID=28 device type=DISK
channel ORA_DISK_1: starting full datafile backup set
channel ORA_DISK_1: specifying datafile(s) in backup set
input datafile file number=00002 name=/mnt/ramdisk/book/sysaux01.dbf
input datafile file number=00005 name=/mnt/ramdisk/book/example01.dbf
channel ORA_DISK_1: starting piece 1 at 2018-02-28 10:19:02
channel ORA_DISK_2: starting full datafile backup set
channel ORA_DISK_2: specifying datafile(s) in backup set
input datafile file number=00003 name=/mnt/ramdisk/book/undotbs01.dbf
input datafile file number=00006 name=/mnt/ramdisk/book/tea01.dbf
channel ORA_DISK_2: starting piece 1 at 2018-02-28 10:19:02
channel ORA_DISK_3: starting full datafile backup set
channel ORA_DISK_3: specifying datafile(s) in backup set
input datafile file number=00001 name=/mnt/ramdisk/book/system01.dbf
input datafile file number=00004 name=/mnt/ramdisk/book/users01.dbf
channel ORA_DISK_3: starting piece 1 at 2018-02-28 10:19:02
channel ORA_DISK_2: finished piece 1 at 2018-02-28 10:19:03
piece handle=/home/oracle/backup/full_20180228_f8ssbr0m_1_1.bak tag=TAG20180228T101902 comment=NONE
channel ORA_DISK_2: backup set complete, elapsed time: 00:00:01
channel ORA_DISK_1: finished piece 1 at 2018-02-28 10:19:09
piece handle=/home/oracle/backup/full_20180228_f7ssbr0m_1_1.bak tag=TAG20180228T101902 comment=NONE
channel ORA_DISK_1: backup set complete, elapsed time: 00:00:07
channel ORA_DISK_3: finished piece 1 at 2018-02-28 10:19:09
piece handle=/home/oracle/backup/full_20180228_f9ssbr0m_1_1.bak tag=TAG20180228T101902 comment=NONE
channel ORA_DISK_3: backup set complete, elapsed time: 00:00:07
Finished backup at 2018-02-28 10:19:09

Starting Control File and SPFILE Autobackup at 2018-02-28 10:19:10
piece handle=/u01/app/oracle/fast_recovery_area/BOOK/autobackup/2018_02_28/o1_mf_s_969272350_f9d4dy2m_.bkp comment=NONE
Finished Control File and SPFILE Autobackup at 2018-02-28 10:19:11

2.测试环境建立:

SCOTT@book> select count(*) from t ;
  COUNT(*)
----------
     84760

--//假设做了一次误操作.
SCOTT@book> set numw 12
SCOTT@book> select current_scn,sysdate from v$database ;
CURRENT_SCN SYSDATE
------------ -------------------
13276934559 2018-02-28 10:19:59

SCOTT@book> delete from t where owner='SYS';
37354 rows deleted.

SCOTT@book> commit ;
Commit complete.

SCOTT@book> alter system archive log current ;
System altered.

SCOTT@book> select count(*) from t ;
    COUNT(*)
------------
       47406


SCOTT@book> select current_scn,sysdate from v$database ;
CURRENT_SCN SYSDATE
------------ -------------------
13276934659 2018-02-28 10:21:13
      
--//SCOTT@book> drop tablespace tea including contents and datafiles;
--//Tablespace dropped.
 
2.恢复表空间看看:
$ mkdir /homne/oracle/aux

--//recover tablespace tea until scn 13276934559 auxiliary destination '/home/oracle/aux';

RMAN> recover tablespace tea until scn 13276934559 auxiliary destination '/home/oracle/aux';
Starting recover at 2018-02-28 10:22:46
using channel ORA_DISK_1
using channel ORA_DISK_2
using channel ORA_DISK_3
RMAN-05026: WARNING: presuming following set of tablespaces applies to specified point-in-time

List of tablespaces expected to have UNDO segments
Tablespace SYSTEM
Tablespace UNDOTBS1

Creating automatic instance, with SID='Badu'

initialization parameters used for automatic instance:
db_name=BOOK
db_unique_name=Badu_tspitr_BOOK
compatible=11.2.0.4.0
db_block_size=8192
db_files=200
sga_target=1G
processes=80
db_create_file_dest=/home/oracle/aux
log_archive_dest_1='location=/home/oracle/aux'
#No auxiliary parameter file used


starting up automatic instance BOOK

Oracle instance started

Total System Global Area    1068937216 bytes

Fixed Size                     2260088 bytes
Variable Size                285213576 bytes
Database Buffers             771751936 bytes
Redo Buffers                   9711616 bytes
Automatic instance created
Running TRANSPORT_SET_CHECK on recovery set tablespaces
TRANSPORT_SET_CHECK completed successfully

contents of Memory Script:
{
# set requested point in time
set until  scn 13276934559;
# restore the controlfile
restore clone controlfile;
# mount the controlfile
sql clone 'alter database mount clone database';
# archive current online log
sql 'alter system archive log current';
# avoid unnecessary autobackups for structural changes during TSPITR
sql 'begin dbms_backup_restore.AutoBackupFlag(FALSE); end;';
}
executing Memory Script

executing command: SET until clause
Starting restore at 2018-02-28 10:22:52
allocated channel: ORA_AUX_DISK_1
channel ORA_AUX_DISK_1: SID=127 device type=DISK
allocated channel: ORA_AUX_DISK_2
channel ORA_AUX_DISK_2: SID=133 device type=DISK
allocated channel: ORA_AUX_DISK_3
channel ORA_AUX_DISK_3: SID=139 device type=DISK

channel ORA_AUX_DISK_1: starting datafile backup set restore
channel ORA_AUX_DISK_1: restoring control file
channel ORA_AUX_DISK_1: reading from backup piece /u01/app/oracle/fast_recovery_area/BOOK/autobackup/2018_02_28/o1_mf_s_969272350_f9d4dy2m_.bkp
channel ORA_AUX_DISK_1: piece handle=/u01/app/oracle/fast_recovery_area/BOOK/autobackup/2018_02_28/o1_mf_s_969272350_f9d4dy2m_.bkp tag=TAG20180228T101910
channel ORA_AUX_DISK_1: restored backup piece 1
channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:00:01
output file name=/home/oracle/aux/BOOK/controlfile/o1_mf_f9d4mxxn_.ctl
Finished restore at 2018-02-28 10:22:54

sql statement: alter database mount clone database

sql statement: alter system archive log current

sql statement: begin dbms_backup_restore.AutoBackupFlag(FALSE); end;

contents of Memory Script:
{
# set requested point in time
set until  scn 13276934559;
plsql <<<-- tspitr_2
declare
  sqlstatement       varchar2(512);
  offline_not_needed exception;
  pragma exception_init(offline_not_needed, -01539);
begin
  sqlstatement := 'alter tablespace '||  'TEA' ||' offline immediate';
--//这里offline表空间,采用immediate方式,我感觉应该由dba干预为佳.
  krmicd.writeMsg(6162, sqlstatement);
  krmicd.execSql(sqlstatement);
exception
  when offline_not_needed then
    null;
end; >>>;
# set destinations for recovery set and auxiliary set datafiles
set newname for clone datafile  1 to new;
set newname for clone datafile  3 to new;
set newname for clone datafile  2 to new;
set newname for clone tempfile  1 to new;
set newname for datafile  6 to
"/mnt/ramdisk/book/tea01.dbf";
# switch all tempfiles
switch clone tempfile all;
# restore the tablespaces in the recovery set and the auxiliary set
restore clone datafile  1, 3, 2, 6;
switch clone datafile all;
}
executing Memory Script

executing command: SET until clause

sql statement: alter tablespace TEA offline immediate

executing command: SET NEWNAME

executing command: SET NEWNAME

executing command: SET NEWNAME

executing command: SET NEWNAME

executing command: SET NEWNAME

renamed tempfile 1 to /home/oracle/aux/BOOK/datafile/o1_mf_temp_%u_.tmp in control file

Starting restore at 2018-02-28 10:22:59
using channel ORA_AUX_DISK_1
using channel ORA_AUX_DISK_2
using channel ORA_AUX_DISK_3

channel ORA_AUX_DISK_1: starting datafile backup set restore
channel ORA_AUX_DISK_1: specifying datafile(s) to restore from backup set
channel ORA_AUX_DISK_1: restoring datafile 00003 to /home/oracle/aux/BOOK/datafile/o1_mf_undotbs1_%u_.dbf
channel ORA_AUX_DISK_1: restoring datafile 00006 to /mnt/ramdisk/book/tea01.dbf
channel ORA_AUX_DISK_1: reading from backup piece /home/oracle/backup/full_20180228_f8ssbr0m_1_1.bak
channel ORA_AUX_DISK_2: starting datafile backup set restore
channel ORA_AUX_DISK_2: specifying datafile(s) to restore from backup set
channel ORA_AUX_DISK_2: restoring datafile 00001 to /home/oracle/aux/BOOK/datafile/o1_mf_system_%u_.dbf
channel ORA_AUX_DISK_2: reading from backup piece /home/oracle/backup/full_20180228_f9ssbr0m_1_1.bak
channel ORA_AUX_DISK_3: starting datafile backup set restore
channel ORA_AUX_DISK_3: specifying datafile(s) to restore from backup set
channel ORA_AUX_DISK_3: restoring datafile 00002 to /home/oracle/aux/BOOK/datafile/o1_mf_sysaux_%u_.dbf
channel ORA_AUX_DISK_3: reading from backup piece /home/oracle/backup/full_20180228_f7ssbr0m_1_1.bak
channel ORA_AUX_DISK_1: piece handle=/home/oracle/backup/full_20180228_f8ssbr0m_1_1.bak tag=TAG20180228T101902
channel ORA_AUX_DISK_1: restored backup piece 1
channel ORA_AUX_DISK_1: restore complete, elapsed time: 00:00:15
channel ORA_AUX_DISK_2: piece handle=/home/oracle/backup/full_20180228_f9ssbr0m_1_1.bak tag=TAG20180228T101902
channel ORA_AUX_DISK_2: restored backup piece 1
channel ORA_AUX_DISK_2: restore complete, elapsed time: 00:00:15
channel ORA_AUX_DISK_3: piece handle=/home/oracle/backup/full_20180228_f7ssbr0m_1_1.bak tag=TAG20180228T101902
channel ORA_AUX_DISK_3: restored backup piece 1
channel ORA_AUX_DISK_3: restore complete, elapsed time: 00:00:15
Finished restore at 2018-02-28 10:23:14

datafile 1 switched to datafile copy
input datafile copy RECID=16 STAMP=969272595 file name=/home/oracle/aux/BOOK/datafile/o1_mf_system_f9d4n3xp_.dbf
datafile 3 switched to datafile copy
input datafile copy RECID=17 STAMP=969272595 file name=/home/oracle/aux/BOOK/datafile/o1_mf_undotbs1_f9d4n3xh_.dbf
datafile 2 switched to datafile copy
input datafile copy RECID=18 STAMP=969272595 file name=/home/oracle/aux/BOOK/datafile/o1_mf_sysaux_f9d4n3xv_.dbf

contents of Memory Script:
{
# set requested point in time
set until  scn 13276934559;
# online the datafiles restored or switched
sql clone "alter database datafile  1 online";
sql clone "alter database datafile  3 online";
sql clone "alter database datafile  2 online";
sql clone "alter database datafile  6 online";
# recover and open resetlogs
recover clone database tablespace  "TEA", "SYSTEM", "UNDOTBS1", "SYSAUX" delete archivelog;
alter clone database open resetlogs;
}
executing Memory Script

executing command: SET until clause

sql statement: alter database datafile  1 online

sql statement: alter database datafile  3 online

sql statement: alter database datafile  2 online

sql statement: alter database datafile  6 online

Starting recover at 2018-02-28 10:23:15
using channel ORA_AUX_DISK_1
using channel ORA_AUX_DISK_2
using channel ORA_AUX_DISK_3

starting media recovery
archived log for thread 1 with sequence 698 is already on disk as file /u01/app/oracle/archivelog/book/1_698_896605872.dbf
archived log file name=/u01/app/oracle/archivelog/book/1_698_896605872.dbf thread=1 sequence=698
media recovery complete, elapsed time: 00:00:00
Finished recover at 2018-02-28 10:23:16

database opened

contents of Memory Script:
{
# make read only the tablespace that will be exported
sql clone 'alter tablespace  TEA read only';
# create directory for datapump import
sql "create or replace directory TSPITR_DIROBJ_DPDIR as ''
/home/oracle/aux''";
# create directory for datapump export
sql clone "create or replace directory TSPITR_DIROBJ_DPDIR as ''
/home/oracle/aux''";
}
executing Memory Script

sql statement: alter tablespace  TEA read only

sql statement: create or replace directory TSPITR_DIROBJ_DPDIR as ''/home/oracle/aux''

sql statement: create or replace directory TSPITR_DIROBJ_DPDIR as ''/home/oracle/aux''

Performing export of metadata...
   EXPDP> Starting "SYS"."TSPITR_EXP_Badu":
   EXPDP> Processing object type TRANSPORTABLE_EXPORT/PLUGTS_BLK
   EXPDP> Processing object type TRANSPORTABLE_EXPORT/TABLE
   EXPDP> Processing object type TRANSPORTABLE_EXPORT/POST_INSTANCE/PLUGTS_BLK
   EXPDP> Master table "SYS"."TSPITR_EXP_Badu" successfully loaded/unloaded
   EXPDP> ******************************************************************************
   EXPDP> Dump file set for SYS.TSPITR_EXP_Badu is:
   EXPDP>   /home/oracle/aux/tspitr_Badu_79616.dmp
   EXPDP> ******************************************************************************
   EXPDP> Datafiles required for transportable tablespace TEA:
   EXPDP>   /mnt/ramdisk/book/tea01.dbf
   EXPDP> Job "SYS"."TSPITR_EXP_Badu" successfully completed at Wed Feb 28 10:23:59 2018 elapsed 0 00:00:36
Export completed


contents of Memory Script:
{
# shutdown clone before import
shutdown clone immediate
# drop target tablespaces before importing them back
sql 'drop tablespace  TEA including contents keep datafiles cascade constraints';
}
executing Memory Script

database closed
database dismounted
Oracle instance shut down

sql statement: drop tablespace  TEA including contents keep datafiles cascade constraints

Performing import of metadata...
   IMPDP> Master table "SYS"."TSPITR_IMP_Badu" successfully loaded/unloaded
   IMPDP> Starting "SYS"."TSPITR_IMP_Badu":
   IMPDP> Processing object type TRANSPORTABLE_EXPORT/PLUGTS_BLK
   IMPDP> Processing object type TRANSPORTABLE_EXPORT/TABLE
   IMPDP> Processing object type TRANSPORTABLE_EXPORT/POST_INSTANCE/PLUGTS_BLK
   IMPDP> Job "SYS"."TSPITR_IMP_Badu" successfully completed at Wed Feb 28 10:24:17 2018 elapsed 0 00:00:03
Import completed

contents of Memory Script:
{
# make read write and offline the imported tablespaces
sql 'alter tablespace  TEA read write';
sql 'alter tablespace  TEA offline';
# enable autobackups after TSPITR is finished
sql 'begin dbms_backup_restore.AutoBackupFlag(TRUE); end;';
}
executing Memory Script

sql statement: alter tablespace  TEA read write

sql statement: alter tablespace  TEA offline
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

sql statement: begin dbms_backup_restore.AutoBackupFlag(TRUE); end;

Removing automatic instance
Automatic instance removed
auxiliary instance file /home/oracle/aux/BOOK/datafile/o1_mf_temp_f9d4nof6_.tmp deleted
auxiliary instance file /home/oracle/aux/BOOK/onlinelog/o1_mf_3_f9d4no5q_.log deleted
auxiliary instance file /home/oracle/aux/BOOK/onlinelog/o1_mf_2_f9d4no08_.log deleted
auxiliary instance file /home/oracle/aux/BOOK/onlinelog/o1_mf_1_f9d4nnrr_.log deleted
auxiliary instance file /home/oracle/aux/BOOK/datafile/o1_mf_sysaux_f9d4n3xv_.dbf deleted
auxiliary instance file /home/oracle/aux/BOOK/datafile/o1_mf_undotbs1_f9d4n3xh_.dbf deleted
auxiliary instance file /home/oracle/aux/BOOK/datafile/o1_mf_system_f9d4n3xp_.dbf deleted
auxiliary instance file /home/oracle/aux/BOOK/controlfile/o1_mf_f9d4mxxn_.ctl deleted
Finished recover at 2018-02-28 10:24:20

RMAN>

--//注:我感觉不是很好,应该由dba干预offline表空间,而不是直接恢复.
--//恢复完成后,表空间状态是offline;

RMAN> report schema ;

Report of database schema for database with db_unique_name BOOK

List of Permanent Datafiles
===========================
File Size(MB) Tablespace           RB segs Datafile Name
---- -------- -------------------- ------- ------------------------
1    760      SYSTEM               ***     /mnt/ramdisk/book/system01.dbf
2    940      SYSAUX               ***     /mnt/ramdisk/book/sysaux01.dbf
3    865      UNDOTBS1             ***     /mnt/ramdisk/book/undotbs01.dbf
4    128      USERS                ***     /mnt/ramdisk/book/users01.dbf
5    346      EXAMPLE              ***     /mnt/ramdisk/book/example01.dbf
6    0        TEA                  ***     /mnt/ramdisk/book/tea01.dbf
--//恢复到原来的位置.

List of Temporary Files
=======================
File Size(MB) Tablespace           Maxsize(MB) Tempfile Name
---- -------- -------------------- ----------- --------------------
1    414      TEMP                 32767       /mnt/ramdisk/book/temp01.dbf


$ cat e.txt
alter tablespace tea online;
alter tablespace tea read only ;

SYS@book> @ /home/oracle/hrp430/testb/e.txt
Tablespace altered.
Tablespace altered.

SCOTT@book> select count(*) from t ;
  COUNT(*)
----------
     84760

--//恢复到删除前的记录数,对比前面的结果.可以这样恢复的简单,完全把复杂的rman命令封装起来,是否实用不是很清楚.
--//看来我学的不是很扎实,我一直以为要选择drop 表空间,然后在恢复.^_^.

3.恢复备库:
--//因为使用传输表空间,备库会存在问题,必须重新拷贝文件到备库.生成备库控制文件.
SYS@bookdg> shutdown immediate ;
Database closed.
Database dismounted.
ORACLE instance shut down.

--//主库执行:
$ scp /mnt/ramdisk/book/tea01.dbf oracle@192.168.100.40:/mnt/ramdisk/book/

--//备库
SYS@bookdg> startup
ORACLE instance started.
Total System Global Area  634732544 bytes
Fixed Size                  2255792 bytes
Variable Size             197133392 bytes
Database Buffers          427819008 bytes
Redo Buffers                7524352 bytes
Database mounted.
ORA-10458: standby database requires recovery
ORA-01157: cannot identify/lock data file 6 - see DBWR trace file
ORA-01110: data file 6: '/mnt/ramdisk/book/tea01.dbf'

SYS@bookdg> shutdown immediate ;
ORA-01109: database not open
Database dismounted.
ORACLE instance shut down.


--//主库执行:
SYS@book> host rm /tmp/aa.ctl3
SYS@book> alter database create standby controlfile as '/tmp/aa.ctl3';
Database altered.

$ scp /tmp/aa.ctl3 oracle@192.168.100.40:/mnt/ramdisk/book/control01.ctl
$ scp /tmp/aa.ctl3 oracle@192.168.100.40:/mnt/ramdisk/book/control02.ctl

--//备库
SYS@bookdg> startup
ORACLE instance started.
Total System Global Area  634732544 bytes
Fixed Size                  2255792 bytes
Variable Size             197133392 bytes
Database Buffers          427819008 bytes
Redo Buffers                7524352 bytes
Database mounted.
Database opened.

SYS@bookdg> alter database recover managed standby database using current logfile disconnect ;
Database altered.

SYS@bookdg> select count(*) from scott.t;
  COUNT(*)
----------
     84760

SYS@bookdg> @ &r/dg/dg
PROCESS       PID STATUS       CLIENT_P GROUP# THREAD#  SEQUENCE#     BLOCK#     BLOCKS DELAY_MINS
--------- ------- ------------ -------- ------ ------- ---------- ---------- ---------- ----------
RFS         23633 IDLE         ARCH     N/A          0          0          0          0          0
ARCH        23616 CONNECTED    ARCH     N/A          0          0          0          0          0
RFS         23629 IDLE         LGWR     2            1        705      51152          1          0
ARCH        23612 CLOSING      ARCH     6            1        703      96256       1755          0
ARCH        23614 CLOSING      ARCH     7            1        704      90112       1077          0
ARCH        23618 CLOSING      ARCH     7            1        702      96256       1755          0
MRP0        23635 APPLYING_LOG N/A      N/A          1        705      51152     102400          0
7 rows selected.

上一篇:《PHP和MySQL Web开发从新手到高手(第5版)》一导读


下一篇:phpcms本地包含漏洞导致的写shell漏洞和删除任意文件漏洞