Monday, 11 November 2024

How to enable Flashback in Oracle database

 To enable Flashback in an Oracle Database, you need to perform several key steps. Flashback allows you to retrieve data that was modified or deleted and to perform time-based queries, which can be helpful for recovery, auditing, and historical analysis.


Here’s a step-by-step guide to enable Flashback in Oracle:


### 1. **Check Database Compatibility**

Flashback features require your database to be running in a compatible mode, typically Oracle 9i or later. Ensure your database is using **ARCHIVELOG** mode (Flashback won't work if the database is running in **NOARCHIVELOG** mode).


SELECT LOG_MODE FROM V$DATABASE;



If the database is not in ARCHIVELOG mode, you need to enable it.


### 2. **Enable ARCHIVELOG Mode (if not already enabled)**

If the database is not in ARCHIVELOG mode, follow these steps to enable it:


1. **Shutdown the database**:


   SHUTDOWN IMMEDIATE;



2. **Mount the database**:


   STARTUP MOUNT;


3. **Enable ARCHIVELOG mode**:

   ALTER DATABASE ARCHIVELOG;



4. **Open the database**:


   ALTER DATABASE OPEN;



5. **Check ARCHIVELOG mode**:


   SELECT LOG_MODE FROM V$DATABASE;



Now, the database should be in ARCHIVELOG mode.


### 3. **Enable Flashback Logging**

Flashback features require Flashback Logging to be enabled in the database. To enable Flashback, ensure that the database has sufficient undo tablespace and the **flashback retention target** is set appropriately.


#### a. **Enable Flashback Logging**

Flashback logging is enabled by default in recent Oracle versions, but you can manually ensure it's enabled by running:


ALTER SYSTEM SET UNDO_RETENTION = <time_in_seconds>;

ALTER SYSTEM SET FLASHBACK_RETENTION_TARGET = <time_in_minutes>;



For example, to set the Flashback retention to 24 hours:


ALTER SYSTEM SET FLASHBACK_RETENTION_TARGET = 1440;



#### b. **Check Flashback Status**

You can check whether Flashback is enabled using the following query:


SELECT FLASHBACK_ON FROM V$DATABASE;



If the result is `YES`, Flashback is enabled.


### 4. **Create a Flash Recovery Area (FRA)**

To enable Flashback, you typically need a Flash Recovery Area (FRA) where Oracle stores archived logs, backups, and Flashback logs. Set up the FRA by running the following command (adjust the path and size according to your requirements):


ALTER SYSTEM SET DB_RECOVERY_FILE_DEST='/path/to/flash_recovery_area';

ALTER SYSTEM SET DB_RECOVERY_FILE_DEST_SIZE=10G;


To check the FRA status:


SELECT * FROM V$RECOVERY_FILE_DEST;



### 5. **Enable Flashback on Tablespaces (Optional)**

Flashback operations may require certain tablespaces to be Flashback-enabled. You can enable Flashback on a tablespace using the following command:




If you want to disable Flashback on a tablespace, use:


ALTER TABLESPACE <tablespace_name> FLASHBACK OFF;



### 6. **Perform Flashback Operations**

Once Flashback is enabled, you can perform various Flashback operations, such as:


- **Flashback Query** to see data as of a specific time or SCN:


  SELECT * FROM my_table AS OF TIMESTAMP TO_TIMESTAMP('2024-11-01 12:00:00', 'YYYY-MM-DD HH24:MI:SS');



- **Flashback Table** to restore a table to a previous state:


  FLASHBACK TABLE my_table TO TIMESTAMP TO_TIMESTAMP('2024-11-01 12:00:00', 'YYYY-MM-DD HH24:MI:SS');



- **Flashback Database** to restore the entire database to a previous point in time (requires the database to be in ARCHIVELOG mode and Flashback logging enabled):


  FLASHBACK DATABASE TO TIMESTAMP TO_TIMESTAMP('2024-11-01 12:00:00', 'YYYY-MM-DD HH24:MI:SS');


- Flashback operations require undo tablespace and the **Flashback logs** to be retained for the duration of your retention period.

- Ensure your FRA has sufficient space to store Flashback logs; otherwise, Oracle will automatically purge them as needed.

  

By following these steps, Flashback will be enabled, and you’ll be able to take advantage of time-based recovery and auditing features in Oracle.

No comments:

Post a Comment

Was this Post Helpful?

Feel free to suggest your opinions in the comments section!