Recovery of accidentally deleted data files under Dameng database LINUX

In the Linux system, the Dameng database is stored in the operating system as a data file. When you delete a data file, as long as the operating system and the database are not restarted, the file handle will not be released. At this time , when you delete the data file, the database and the operating system have not been restarted, and there is no data backup , we still have the opportunity to restore the data file.

1. Use the data migration tool to migrate the data to a new normal database instance

After the data file is deleted, as long as the data file is not checked, the corresponding table data in the data file can still be accessed normally, so we can re-initialize a database with consistent parameters. Use a different port, then start the database. Then stop the application to prevent data from being written again, and then use the migration tool to completely migrate the data over.

Data migration will be fast if the amount of data is not large. However, if the amount of data is large, the downtime migration time is unacceptable, and there is no data backup, the second method can be used.

2. Retrieve the deleted data files for recovery

2.1 Create tablespaces, data files, users, and tables for testing.

SQL> create tablespace TEST_RM datafile 'TEST_RM01.DBF' size 128 autoextend on next 128 CACHE = NORMAL;
操作已执行
已用时间: 13.499(毫秒). 执行号:53300.
SQL>  create user TEST_RM identified by 123456789 default tablespace TEST_RM;
操作已执行
已用时间: 5.172(毫秒). 执行号:53301.
SQL>  grant "PUBLIC","RESOURCE","SOI","VTI" to TEST_RM;
操作已执行
已用时间: 3.186(毫秒). 执行号:53302.
SQL> create table TEST_RM.T1 as select * from v$sessions;
操作已执行
已用时间: 10.411(毫秒). 执行号:53303.
SQL> select tablespace_name, file_name, status from dba_data_files;
 
行号     TABLESPACE_NAME FILE_NAME                                STATUS   
---------- --------------- ---------------------------------------- ---------
1          SYSTEM          /home/dmdba/TEST_RM/DAMENG/SYSTEM.DBF    AVAILABLE
2          TEST_RM         /home/dmdba/TEST_RM/DAMENG/TEST_RM01.DBF AVAILABLE
3          MAIN            /home/dmdba/TEST_RM/DAMENG/MAIN.DBF      AVAILABLE
4          TEMP            /home/dmdba/TEST_RM/DAMENG/TEMP.DBF      AVAILABLE
5          ROLL            /home/dmdba/TEST_RM/DAMENG/ROLL.DBF      AVAILABLE
 
已用时间: 7.356(毫秒). 执行号:53304.
SQL> select * from TEST_RM.T1;
 
行号     SESS_ID              SESS_SEQ    SQL_TEXT                                             STATE  N_STMT      N_USED_STMT SEQ_NO      CURR_SCH USER_NAME TRX_ID               CREATE_TIME                CLNT_TYPE TIME_ZONE CHK_CONS CHK_IDENT RDONLY INS_NULL
---------- -------------------- ----------- ---------------------------------------------------- ------ ----------- ----------- ----------- -------- --------- -------------------- -------------------------- --------- --------- -------- --------- ------ --------
          COMPILE_FLAG AUTO_CMT DDL_AUTOCMT RS_FOR_QRY CHK_NET ISO_LEVEL   CLNT_HOST APPNAME CLNT_IP OSNAME CONN_TYPE    VPOOLADDR            RUN_STATUS MSG_STATUS LAST_RECV_TIME             LAST_SEND_TIME             DCP_FLAG THRD_ID     CONNECTED   PORT_TYPE  
          ------------ -------- ----------- ---------- ------- ----------- --------- ------- ------- ------ ------------ -------------------- ---------- ---------- -------------------------- -------------------------- -------- ----------- ----------- -----------
          SRC_SITE    MAL_ID               CONCURRENT_FLAG CUR_LINENO  CUR_MTDNAME CUR_SQLSTR CLNT_VER
          ----------- -------------------- --------------- ----------- ----------- ---------- --------
1          140217730275320      549         create table TEST_RM.T1 as select * from v$sessions; ACTIVE 64          1           8           SYSDBA   SYSDBA    3561                 2021-10-28 15:55:31.000000 SQL3      +08:00    N        N         N      Y
          N            N        Y           N          N       1           RH6       disql   ::1     Linux   HOMOGENEOUS 140217730207664      RUNNING    RECIEVE    2021-10-28 15:56:00.000000 2021-10-28 15:55:55.000000 N        27149       1           0
          65535       NULL                 0               55          NULL        NULL       7.1.8.46
 
 
已用时间: 1.607(毫秒). 执行号:53305.
SQL>

2.2 Delete the data file of the corresponding table space for simulation

[dmdba@RH6 bin]$ cd /home/dmdba/TEST_RM/DAMENG/
[dmdba@RH6 DAMENG]$ ls
bak  ctl_bak  DAMENG01.log  DAMENG02.log  dm.ctl  dm.ini  dminit20211028155433.log  dm_service.prikey  HMAIN  MAIN.DBF  rep_conflict.log  ROLL.DBF  sqllog.ini  SYSTEM.DBF  TEMP.DBF  TEST_RM01.DBF  trace
[dmdba@RH6 DAMENG]$ ls -ltr
total 1187948
drwxr-xr-x 2 dmdba dinstall      4096 Oct 28 15:54 bak
-rw-r--r-- 1 dmdba dinstall       481 Oct 28 15:54 sqllog.ini
-rw-r--r-- 1 dmdba dinstall     52092 Oct 28 15:54 dm.ini
-rw-r--r-- 1 dmdba dinstall       633 Oct 28 15:54 dm_service.prikey
-rw-r--r-- 1 dmdba dinstall 134217728 Oct 28 15:54 MAIN.DBF
drwxr-xr-x 2 dmdba dinstall      4096 Oct 28 15:54 HMAIN
-rw-r--r-- 1 dmdba dinstall      1000 Oct 28 15:54 dminit20211028155433.log
-rw-r--r-- 1 dmdba dinstall  77594624 Oct 28 15:55 TEMP.DBF
-rw-r--r-- 1 dmdba dinstall 268435456 Oct 28 15:55 DAMENG02.log
drwxr-xr-x 2 dmdba dinstall      4096 Oct 28 15:55 trace
-rw-r--r-- 1 dmdba dinstall        12 Oct 28 15:55 rep_conflict.log
-rw-r--r-- 1 dmdba dinstall      5632 Oct 28 15:55 dm.ctl
drwxr-xr-x 2 dmdba dinstall      4096 Oct 28 15:55 ctl_bak
-rw-r--r-- 1 dmdba dinstall 268435456 Oct 28 15:56 DAMENG01.log
-rw-r--r-- 1 dmdba dinstall 161480704 Oct 28 15:56 ROLL.DBF
-rw-r--r-- 1 dmdba dinstall 134217728 Oct 28 15:56 TEST_RM01.DBF
-rw-r--r-- 1 dmdba dinstall 171966464 Oct 28 15:56 SYSTEM.DBF
[dmdba@RH6 DAMENG]$
[dmdba@RH6 DAMENG]$ mv TEST_RM01.DBF /tmp

2.3 Query table space and table space status

Then query the table data and table space status, you will find that the data can still be queried (because the data file has not been verified, if the data file is verified, the data will not be queried), but the path of the data file in the system view is no longer exist.

SQL> select * from TEST_RM.T1;
 
行号     SESS_ID              SESS_SEQ    SQL_TEXT                                              STATE  N_STMT      N_USED_STMT SEQ_NO      CURR_SCH USER_NAME TRX_ID               CREATE_TIME                CLNT_TYPE TIME_ZONE CHK_CONS CHK_IDENT RDONLY INS_NULL
---------- -------------------- ----------- ----------------------------------------------------- ------ ----------- ----------- ----------- -------- --------- -------------------- -------------------------- --------- --------- -------- --------- ------ --------
           COMPILE_FLAG AUTO_CMT DDL_AUTOCMT RS_FOR_QRY CHK_NET ISO_LEVEL   CLNT_HOST APPNAME CLNT_IP OSNAME CONN_TYPE    VPOOLADDR            RUN_STATUS MSG_STATUS LAST_RECV_TIME             LAST_SEND_TIME             DCP_FLAG THRD_ID     CONNECTED   PORT_TYPE  
           ------------ -------- ----------- ---------- ------- ----------- --------- ------- ------- ------ ------------ -------------------- ---------- ---------- -------------------------- -------------------------- -------- ----------- ----------- -----------
           SRC_SITE    MAL_ID               CONCURRENT_FLAG CUR_LINENO  CUR_MTDNAME CUR_SQLSTR CLNT_VER
           ----------- -------------------- --------------- ----------- ----------- ---------- --------
1          140611457980408      549          create table TEST_RM.T1 as select * from v$sessions; ACTIVE 64          1           8           SYSDBA   SYSDBA    3561                 2021-10-28 16:02:53.000000 SQL3      +08:00    N        N         N      Y
           N            N        Y           N          N       1           RH6       disql   ::1     Linux   HOMOGENEOUS 140611457912752      RUNNING    RECIEVE    2021-10-28 16:03:10.000000 2021-10-28 16:03:05.000000 N        27377       1           0
           65535       NULL                 0               55          NULL        NULL       7.1.8.46
已用时间: 0.714(毫秒). 执行号:53306.
SQL>
SQL> select tablespace_name, file_name, status from dba_data_files;
select tablespace_name, file_name, status from dba_data_files;
[-2206]:无效的参数值.
已用时间: 9.580(毫秒). 执行号:0.
SQL> SELECT GROUP_ID,PATH,CLIENT_PATH FROM "V$DATAFILE";
 
行号     GROUP_ID    PATH                                                               CLIENT_PATH
---------- ----------- ------------------------------------------------------------------ -----------
1          0           /home/dmdba/TEST_RM/DAMENG/SYSTEM.DBF                              SYSTEM.DBF
2          1           /home/dmdba/TEST_RM/DAMENG/ROLL.DBF                                ROLL.DBF
3          3           /home/dmdba/TEST_RM/DAMENG/TEMP.DBF                                TEMP.DBF
4          4           /home/dmdba/TEST_RM/DAMENG/MAIN.DBF                                MAIN.DBF
5          NULL        文件或目录[/home/dmdba/TEST_RM/DAMENG/TEST_RM01.DBF]不存在 NULL
 
已用时间: 0.669(毫秒). 执行号:53308.
SQL>

2.4 Perform data file inspection

SQL> call SP_FILE_SYS_CHECK();
DMSQL 过程已成功完成
已用时间: 1.861(毫秒). 执行号:53309.
SQL>  select * from TEST_RM.T1;
 select * from TEST_RM.T1;
[-3430]:表空间[TEST_RM]中文件[/home/dmdba/TEST_RM/DAMENG/TEST_RM01.DBF]已被删除.
已用时间: 1.049(毫秒). 执行号:0.

It can be seen from the above that if the data file check is not done, the tables and objects on the deleted data file can be accessed normally, but they will not be able to be accessed normally after the data file check is done.

2.5 Restoring data files

2.5.1 Perform preparations before recovery first

SQL> call sp_tablespace_prepare_recover('TEST_RM');
DMSQL 过程已成功完成
已用时间: 1.018(毫秒). 执行号:53311.

2.5.2 Check the database process number, find the unreleased file handle number and the deleted data file (the file handle will disappear after restarting)

[dmdba@RH6 DAMENG]$ ps -ef|grep TEST_RM
dmdba    **27296**     1  0 16:01 pts/4    00:00:03 /home/dmdba/dm46/bin/dmserver path=/home/dmdba/TEST_RM/DAMENG/dm.ini -noconsole
dmdba    27444 26219  0 16:20 pts/5    00:00:00 grep TEST_RM
[dmdba@RH6 DAMENG]$ cd /proc/27296/fd
[dmdba@RH6 fd]$ ls -ltr
total 0
l-wx------ 1 dmdba dinstall 64 Oct 28 16:20 2 -> /home/dmdba/dm46/log/DmServiceTESTRM.log
lrwx------ 1 dmdba dinstall 64 Oct 28 16:20 9 -> /home/dmdba/TEST_RM/DAMENG/DAMENG02.log
lrwx------ 1 dmdba dinstall 64 Oct 28 16:20 8 -> /home/dmdba/TEST_RM/DAMENG/DAMENG01.log
lrwx------ 1 dmdba dinstall 64 Oct 28 16:20 7 -> /home/dmdba/TEST_RM/DAMENG/TEMP.DBF
lrwx------ 1 dmdba dinstall 64 Oct 28 16:20 6 -> /home/dmdba/TEST_RM/DAMENG/SYSTEM.DBF
lrwx------ 1 dmdba dinstall 64 Oct 28 16:20 5 -> socket:[225406]
lrwx------ 1 dmdba dinstall 64 Oct 28 16:20 48 -> socket:[226078]
lrwx------ 1 dmdba dinstall 64 Oct 28 16:20 47 -> socket:[226077]
lrwx------ 1 dmdba dinstall 64 Oct 28 16:20 46 -> socket:[225766]
**lrwx------ 1 dmdba dinstall 64 Oct 28 16:20 45 -> /home/dmdba/TEST_RM/DAMENG/TEST_RM01.DBF (deleted)**
lrwx------ 1 dmdba dinstall 64 Oct 28 16:20 44 -> socket:[225508]
......
lrwx------ 1 dmdba dinstall 64 Oct 28 16:20 11 -> /home/dmdba/TEST_RM/DAMENG/MAIN.DBF
lrwx------ 1 dmdba dinstall 64 Oct 28 16:20 10 -> /home/dmdba/TEST_RM/DAMENG/ROLL.DBF
l-wx------ 1 dmdba dinstall 64 Oct 28 16:20 1 -> /home/dmdba/dm46/log/DmServiceTESTRM.log
lr-x------ 1 dmdba dinstall 64 Oct 28 16:20 0 -> /dev/null
[dmdba@RH6 fd]$

2.5.3 Copy data files back

Note: copy files with dmdba user

[dmdba@RH6 fd]$ cp 45 /home/dmdba/TEST_RM/DAMENG/TEST_RM01.DBF

2.5.4 Executing the data file recovery process

SQL> call sp_tablespace_recover('TEST_RM');
DMSQL 过程已成功完成
已用时间: 6.302(毫秒). 执行号:53312.
SQL> call SP_FILE_SYS_CHECK();
DMSQL 过程已成功完成
已用时间: 0.254(毫秒). 执行号:53313.

2.5.5 Query data and table space for data verification

SQL> select * from TEST_RM.T1;
 
行号     SESS_ID              SESS_SEQ    SQL_TEXT                                              STATE  N_STMT      N_USED_STMT SEQ_NO      CURR_SCH USER_NAME TRX_ID               CREATE_TIME                CLNT_TYPE TIME_ZONE CHK_CONS CHK_IDENT RDONLY INS_NULL
---------- -------------------- ----------- ----------------------------------------------------- ------ ----------- ----------- ----------- -------- --------- -------------------- -------------------------- --------- --------- -------- --------- ------ --------
           COMPILE_FLAG AUTO_CMT DDL_AUTOCMT RS_FOR_QRY CHK_NET ISO_LEVEL   CLNT_HOST APPNAME CLNT_IP OSNAME CONN_TYPE    VPOOLADDR            RUN_STATUS MSG_STATUS LAST_RECV_TIME             LAST_SEND_TIME             DCP_FLAG THRD_ID     CONNECTED   PORT_TYPE  
           ------------ -------- ----------- ---------- ------- ----------- --------- ------- ------- ------ ------------ -------------------- ---------- ---------- -------------------------- -------------------------- -------- ----------- ----------- -----------
           SRC_SITE    MAL_ID               CONCURRENT_FLAG CUR_LINENO  CUR_MTDNAME CUR_SQLSTR CLNT_VER
           ----------- -------------------- --------------- ----------- ----------- ---------- --------
1          140611457980408      549          create table TEST_RM.T1 as select * from v$sessions; ACTIVE 64          1           8           SYSDBA   SYSDBA    3561                 2021-10-28 16:02:53.000000 SQL3      +08:00    N        N         N      Y
           N            N        Y           N          N       1           RH6       disql   ::1     Linux   HOMOGENEOUS 140611457912752      RUNNING    RECIEVE    2021-10-28 16:03:10.000000 2021-10-28 16:03:05.000000 N        27377       1           0
           65535       NULL                 0               55          NULL        NULL       7.1.8.46
 
 
已用时间: 1.916(毫秒). 执行号:53314.
SQL> SELECT GROUP_ID,PATH,CLIENT_PATH FROM "V$DATAFILE";
 
行号     GROUP_ID    PATH                                     CLIENT_PATH  
---------- ----------- ---------------------------------------- -------------
1          0           /home/dmdba/TEST_RM/DAMENG/SYSTEM.DBF    SYSTEM.DBF
2          1           /home/dmdba/TEST_RM/DAMENG/ROLL.DBF      ROLL.DBF
3          3           /home/dmdba/TEST_RM/DAMENG/TEMP.DBF      TEMP.DBF
4          4           /home/dmdba/TEST_RM/DAMENG/MAIN.DBF      MAIN.DBF
5          5           /home/dmdba/TEST_RM/DAMENG/TEST_RM01.DBF TEST_RM01.DBF
 
已用时间: 0.450(毫秒). 执行号:53315.
SQL> select tablespace_name, file_name, status from dba_data_files;
 
行号     TABLESPACE_NAME FILE_NAME                                STATUS   
---------- --------------- ---------------------------------------- ---------
1          SYSTEM          /home/dmdba/TEST_RM/DAMENG/SYSTEM.DBF    AVAILABLE
2          TEST_RM         /home/dmdba/TEST_RM/DAMENG/TEST_RM01.DBF AVAILABLE
3          MAIN            /home/dmdba/TEST_RM/DAMENG/MAIN.DBF      AVAILABLE
4          TEMP            /home/dmdba/TEST_RM/DAMENG/TEMP.DBF      AVAILABLE
5          ROLL            /home/dmdba/TEST_RM/DAMENG/ROLL.DBF      AVAILABLE
已用时间: 4.885(毫秒). 执行号:53316.

At this point, the recovery is complete, and immediately after the recovery is complete, make a backup of the entire database; if you have not done a scheduled data backup, do the task quickly. Points to note during recovery: 1. The database and operating system cannot be restarted; 2. When copying data files, you need to use the dmdba user to copy, and other accounts need to set the file user and group permissions to dmdba and dinstall;

Summary: The above method is used when the database is not archived or backed up. If there is a backup, you can directly use the backup and archive to restore the data completely, and at the same time, the speed of recovery and the integrity of the data are guaranteed. When you encounter an unfamiliar environment and need to upgrade the data software or shut down and restart, it is best to check the status of the data file.

Guess you like

Origin blog.csdn.net/qq_35273918/article/details/129738162
Recommended