Skip to content

43. Incomplete Recovery, Backup Script

Agnivo102 edited this page Jun 23, 2026 · 1 revision

Incomplete Recovery:

When the recovery of a database remains incomple not complete this is called incomplete recovery.

When to use this:

Like for example we have created a table some hours back and it got deleted by mistake. We can then restore that deleted table by recovering our database to the state as it was before the time of deletion. There are 2 ways of doing it.

i. Using timestamp of deletion:

We need to know when the table got deleted and we can use that time to restore our database as it was 1 second before.

ii. Using SCN Number

We can also use SCN number to do the incomplete recovery. When we commit the background process CKPT takes the SCN number of the time of commit and put it in the header of datafile. We can use that for the recovery.

For this recovery to work there needs to be an active backup of the database. The restoration will use the archive logs to do the recovery.

How this is different from Complete Recovery:

As for why this is called incomplete restoration. In case of complete restoration or recovery we recover the whole thing, the things happened between the creation of the database uoto current time everything is recovered. But this will complete our objective of recovering an important table if its deleted by mistakenly. This is possible with the incomplete recovery which recovers the database to the state it was before the table got deleted. So this is called incomplete recovery as the whole thing (the deletion of the table) doesn't get recovered.

1st Way: By using timestamp:

We can do this incomplete recovery by 2 ways. First is by using the timestamp before deletion.


Screenshot

Now don't forget to commit the table.


Screenshot

We have created an user and a table with it. Now lets see what is the current timestamp so that we can use it for our incomplete recovery.


Screenshot

Now lets delete the table.


Screenshot

Now we have to take the database to mount state and go to the rman tool.


Screenshot

Script:

run
{
set until time "to_date('22-06-26 22:40:55','dd-mm-yy hh24:mi:ss')";
restore database;
recover database;
alter database open resetlogs;
}

This line set until time tells the process to do the recovery until this timestamp.

Screenshot
Screenshot

Also we need to reset the logs after recovery. This reset logs is also required after incomplete recovery just like after recovering comtrol file.


Screenshot

See the table is back again.

2nd Way: By using SCN number

2nd is by using the SCN number of the last commit before the deletion of the deletion of the table.


Screenshot

We will use this SCN number to recover. This time lets delete the whole user itself.


Screenshot
Screenshot

run
{
set until scn 2401342;
restore database;
recover database;
alter database open resetlogs;
}

This line set until scn tells the process to do the recovery until the time this SCN number was in the header of the datafile.

Screenshot
Screenshot

See the user and its tables are back.

Backup script:

Now in industry they don't run backup by typing code. There are scripts for that. And there is a scheduler crontab in linux. This scheduler is used to schedule backup scripts to run a specific time in a regular basis. This scheduler is not oracle's application but its Linux's tool. And for this scheduler to work the backup script will have to be a shell script.

Screenshot

Backup.sh

export ORACLE_SID=prod
export ORACLE_HOME=/u01/app/oracle/product/19.0.0/dbhome_1
export PATH=$ORACLE_HOME/bin:$PATH
export LOG_DIR=/u01/backup/

rman target / log=$LOG_DIR/full_backup_$(date +%d%m%Y).log <<EOF

run
{
delete noprompt expired backup;
backup database plus archivelog;
}
exit;
EOF
~


Explanation:

This backup script will store a log. Like how to we understand what backups were taken and what happened. Through the logs.

export ORACLE_SID=prod
export ORACLE_HOME=/u01/app/oracle/product/19.0.0/dbhome_1
export PATH=$ORACLE_HOME/bin:$PATH
export LOG_DIR=/u01/backup/
This 4 lines to set the paths the script will need in linux environment variables. Oracle sid and home is already known. Path variable is for getting the sql and rman prompt. Log directory is where the log will be stored.

rman target / log=$LOG_DIR/full_backup_$(date +%d%m%Y).log <<EOF
This line is so that what happens in the rman tool will be stored in the log. Everytime the script runs it will create a new log file with current date and store the logs in it.

run
{
delete noprompt expired backup;
This line is for like when we try to delete expired backup it always asks our permission right? To enyer yes or no. This line forces to not ask that and to delete the expired backup without asking anything.
backup database plus archivelog;
}
exit;
EOF

Deleted previous backups.


Screenshot

Run the script:


Screenshot

See the backups are back. Lets check the log.


Screenshot
Screenshot
Screenshot

Clone this wiki locally