Bài viết này sẽ hướng dẫn bạn cách khôi phục lại datafile, tablespace bị mất datafile do lỗi OS hoặc vì lý do nào đó, trong khi chúng ta chưa có backup datafile mất đó.
Các công việc thực hiện:
- Tạo tablespace
- Tạo backup ban đầu
- Thêm mới datafile vào tbs nhưng không thực hiện backup
- Tạo table và insert data
- Xoá 2 datafiles trong tbs vừa tao bên trên trong OS level (lúc này sẽ lỗi vì tbs ko có datafile để đọc)
- Offline tbs vừa tạo
- Check thông tin trong rman datafile lỗi
- Restore và Recover TBS
- TBS online và check dữ liệu
Tạo tablespace:
create tablespace duongtbs datafile '/u01/app/oracle/oradata/DUODB/datafile/duongtbs01.dbf' size 100M;
SQL> create tablespace duongtbs datafile ‘/u01/app/oracle/oradata/DUODB/datafile/duongtbs01.dbf’ size 100M;
Tablespace created.
Tạo bản backup:
exit rman target / backup database plus archivelog;
[oracle@srv1 ~]$ rman target /
Recovery Manager: Release 19.0.0.0.0 – Production on Fri May 10 22:44:50 2024
Version 19.3.0.0.0
Copyright (c) 1982, 2019, Oracle and/or its affiliates. All rights reserved.
connected to target database: DUODB (DBID=381617332)
RMAN> backup database plus archivelog;
Starting backup at 10-MAY-24
current log archived
using target database control file instead of recovery catalog
Thêm datafile mới cho TBS:
exit sqlplus / as sysdba alter tablespace duongtbs add datafile '/u01/app/oracle/oradata/DUODB/datafile/duongtbs02.dbf' size 100m;
SQL> alter tablespace duongtbs add datafile ‘/u01/app/oracle/oradata/DUODB/datafile/duongtbs02.dbf’ size 100m;
Tablespace altered.
SQL> select file_name from dba_data_files where tablespace_name like ‘DUONGTBS’;
FILE_NAME
——————————————————————————–
/u01/app/oracle/oradata/DUODB/datafile/duongtbs01.dbf
/u01/app/oracle/oradata/DUODB/datafile/duongtbs02.dbf
Kiểm tra datafile trong TBS và tiến hành switch logfile:
select file_name from dba_data_files where tablespace_name like 'DUONGTBS'; alter system switch logfile; alter system switch logfile;
SQL> alter system switch logfile;
alter system switch logfile;
System altered.
Tạo table và insert data:
create table tab1(col1 number) tablespace DUONGTBS; insert into tab1 values (1); insert into tab1 values (2); commit;
SQL> create table tab1(col1 number) tablespace DUONGTBS;
Table created.
SQL> insert into tab1 values (1);
1 row created.
SQL> insert into tab1 values (2);
1 row created.
SQL> commit;
Commit complete.
Xoá datafile của TBS bên trên:
exit cd /u01/app/oracle/oradata/DUODB/datafile ls -lha rm duongtbs*
[oracle@srv1 ~]$ cd /u01/app/oracle/oradata/DUODB/datafile
[oracle@srv1 datafile]$ ls -lha
total 2.1G
drwxr-x—. 2 oracle oinstall 4.0K May 10 22:47 .
drwxr-x—. 5 oracle oinstall 58 May 4 06:13 ..
-rw-r—–. 1 oracle oinstall 101M May 10 22:45 duongtbs01.dbf
-rw-r—–. 1 oracle oinstall 101M May 10 22:47 duongtbs02.dbf
-rw-r—–. 1 oracle oinstall 601M May 10 22:48 o1_mf_sysaux_m3bvvyk8_.dbf
-rw-r—–. 1 oracle oinstall 911M May 10 22:45 o1_mf_system_m3bvtvd1_.dbf
-rw-r—–. 1 oracle oinstall 33M May 10 22:00 o1_mf_temp_m3bvyjgk_.tmp
-rw-r—–. 1 oracle oinstall 346M May 10 22:45 o1_mf_undotbs1_m3bvwqpc_.dbf
-rw-r—–. 1 oracle oinstall 5.1M May 10 22:45 o1_mf_users_m3bvwrsm_.dbf
[oracle@srv1 datafile]$ rm duongtbs*
[oracle@srv1 datafile]$ ls -lha
total 1.9G
drwxr-x—. 2 oracle oinstall 4.0K May 10 22:48 .
drwxr-x—. 5 oracle oinstall 58 May 4 06:13 ..
-rw-r—–. 1 oracle oinstall 601M May 10 22:48 o1_mf_sysaux_m3bvvyk8_.dbf
-rw-r—–. 1 oracle oinstall 911M May 10 22:45 o1_mf_system_m3bvtvd1_.dbf
-rw-r—–. 1 oracle oinstall 33M May 10 22:00 o1_mf_temp_m3bvyjgk_.tmp
-rw-r—–. 1 oracle oinstall 346M May 10 22:45 o1_mf_undotbs1_m3bvwqpc_.dbf
-rw-r—–. 1 oracle oinstall 5.1M May 10 22:45 o1_mf_users_m3bvwrsm_.dbf
Offline TBS:
sqlplus / as sysdba alter tablespace duongtbs offline; alter tablespace duongtbs offline immediate; select file_id,file_name from dba_data_files where tablespace_name like 'DUONGTBS';
SQL> alter tablespace duongtbs offline;
alter tablespace duongtbs offline
*
ERROR at line 1:
ORA-01116: error in opening database file 5
ORA-01110: data file 5: ‘/u01/app/oracle/oradata/DUODB/datafile/duongtbs01.dbf’
ORA-27041: unable to open file
Linux-x86_64 Error: 2: No such file or directory
Additional information: 3
SQL> alter tablespace duongtbs offline immediate;
Tablespace altered.
SQL> set lines 300
SQL> /
FILE_ID FILE_NAME
———- ———————————————————————-
5 /u01/app/oracle/oradata/DUODB/datafile/duongtbs01.dbf
2 /u01/app/oracle/oradata/DUODB/datafile/duongtbs02.dbf
Kiểm tra trạng thái datafile của TBS bên trên (chúng ta thấy có 2 datafile có ID là 2 và 5):
exit rman target / list backup of datafile 2; list backup of datafile 5;
RMAN> list backup of datafile 2;
using target database control file instead of recovery catalog
specification does not match any backup in the repository
RMAN> list backup of datafile 5;
List of Backup Sets
===================
BS Key Type LV Size Device Type Elapsed Time Completion Time
——- —- — ———- ———– ———— —————
54 Full 1.21G DISK 00:00:06 10-MAY-24
BP Key: 54 Status: AVAILABLE Compressed: NO Tag: TAG20240510T224520
Piece Name: /u01/app/oracle/fast_recovery_area/DUODB/backupset/2024_05_10/o1_mf_nnndf_TAG20240510T224520_m3wj8k05_.bkp
List of Datafiles in backup set 54
File LV Type Ckp SCN Ckp Time Abs Fuz SCN Sparse Name
—- — —- ———- ——— ———– —— —-
5 Full 2379035 10-MAY-24 NO /u01/app/oracle/oradata/DUODB/datafile/duongtbs01.dbf
=> trong backup piece ban đầu thì chúng ta có datafile 5, còn file 2 thì tạo sau khi backup nên không có trong backup piece.
Restore và Recover TBS:
restore tablespace DUONGTBS; recover tablespace DUONGTBS;
RMAN> restore tablespace DUONGTBS;
Starting restore at 10-MAY-24
allocated channel: ORA_DISK_1
channel ORA_DISK_1: SID=275 device type=DISK
allocated channel: ORA_DISK_2
channel ORA_DISK_2: SID=278 device type=DISK
allocated channel: ORA_DISK_3
channel ORA_DISK_3: SID=44 device type=DISK
allocated channel: ORA_DISK_4
channel ORA_DISK_4: SID=279 device type=DISK
allocated channel: ORA_DISK_5
channel ORA_DISK_5: SID=45 device type=DISK
RMAN> recover tablespace DUONGTBS;
Starting recover at 10-MAY-24
using channel ORA_DISK_1
using channel ORA_DISK_2
using channel ORA_DISK_3
using channel ORA_DISK_4
using channel ORA_DISK_5
using channel ORA_DISK_6
using channel ORA_DISK_7
using channel ORA_DISK_8
using channel ORA_DISK_9
using channel ORA_DISK_10
starting media recovery
archived log for thread 1 with sequence 2 is already on disk as file /u01/app/oracle/fast_recovery_area/DUODB/archivelog/2024_05_10/o1_mf_1_2_m3wj8r1l_.arc
archived log for thread 1 with sequence 3 is already on disk as file /u01/app/oracle/fast_recovery_area/DUODB/archivelog/2024_05_10/o1_mf_1_3_m3wjfbl2_.arc
archived log for thread 1 with sequence 4 is already on disk as file /u01/app/oracle/fast_recovery_area/DUODB/archivelog/2024_05_10/o1_mf_1_4_m3wjfbln_.arc
archived log file name=/u01/app/oracle/fast_recovery_area/DUODB/archivelog/2024_05_10/o1_mf_1_2_m3wj8r1l_.arc thread=1 sequence=2
media recovery complete, elapsed time: 00:00:00
Finished recover at 10-MAY-24
Online TBS và check status:
select tablespace_name,status from dba_tablespaces; alter tablespace duongtbs online; select tablespace_name,status from dba_tablespaces; select * from tab1;
RMAN> select tablespace_name,status from dba_tablespaces;
TABLESPACE_NAME STATUS
—————————— ———
SYSTEM ONLINE
SYSAUX ONLINE
UNDOTBS1 ONLINE
TEMP ONLINE
USERS ONLINE
DUONGTBS OFFLINE
RMAN> alter tablespace duongtbs online;
Statement processed
RMAN> select tablespace_name,status from dba_tablespaces;
TABLESPACE_NAME STATUS
—————————— ———
SYSTEM ONLINE
SYSAUX ONLINE
UNDOTBS1 ONLINE
TEMP ONLINE
USERS ONLINE
DUONGTBS ONLINE
RMAN> select * from tab1;
COL1
———-
1
2
DataFiles đã xuất thiện sau khi restore => hệ thống tự động tạo lại datafile từ backup piece và archivelog:
[oracle@srv1 log]$ cd /u01/app/oracle/oradata/DUODB/datafile
[oracle@srv1 datafile]$ ls -lha
total 2.1G
drwxr-x—. 2 oracle oinstall 4.0K May 10 22:53 .
drwxr-x—. 5 oracle oinstall 58 May 4 06:13 ..
-rw-r—–. 1 oracle oinstall 101M May 10 22:54 duongtbs01.dbf
-rw-r—–. 1 oracle oinstall 101M May 10 22:54 duongtbs02.dbf
-rw-r—–. 1 oracle oinstall 601M May 10 22:54 o1_mf_sysaux_m3bvvyk8_.dbf
-rw-r—–. 1 oracle oinstall 911M May 10 22:54 o1_mf_system_m3bvtvd1_.dbf
-rw-r—–. 1 oracle oinstall 33M May 10 22:00 o1_mf_temp_m3bvyjgk_.tmp
-rw-r—–. 1 oracle oinstall 346M May 10 22:54 o1_mf_undotbs1_m3bvwqpc_.dbf
-rw-r—–. 1 oracle oinstall 5.1M May 10 22:53 o1_mf_users_m3bvwrsm_.dbf
Chúc các bạn thành công.