Scripts to start the Dataguard after system reboot in Windows
Scripts to start the Dataguard Standby Server after Windows system reboot occurred.
Problem:
When the window reboot occurred, the DR site of the data guard environment did not come up in recovery mode.
We get a gap between the physical and DR site.
Solution: Script OR TRIGGER to make the data guard standby site start in recovery mode on every window restart.
1. Placed the given below scripts on the folder as shown below:
Save the two files shown below in a folder script.
-- Save file as DGSTART.BAT SQLPLUS SYS/PASSWORD AS SYSDBA @C:\SCRIPT\DGSTART.SQL > C:\SCRIPT\DBSTART.LOG--Save file as DGSTART.SQL STARTUP NOMOUNT; ALTER DATABASE MOUNT; ALTER DATABASE RECOVER MANAGED STANDBY DATABASE DISCONNECT FROM SESSION; alter session set nls_date_format = 'DD-MON-YYYY HH24:MI:SS'; select sysdate from dual; exit;
OR
We can use this trigger directly on Standby Database to verify its primary or standby and close the instance and start the recovery.
CREATE OR REPLACE TRIGGER startup_mount
AFTER STARTUP ON DATABASE
DECLARE
role VARCHAR(30);
BEGIN
SELECT DATABASE_ROLE INTO role FROM V$DATABASE;
IF role != ‘PRIMARY’ THEN
execute IMMEDIATE ‘alter database close’;
execute IMMEDIATE ‘alter database recover managed standby database using current logfile disconnect from session’;
END IF;
END startup_mount;
/
2. Schedules this script in the Window Task Scheduler event “after startup”. Detailed steps are as follows:
a. Open the task scheduler in Windows and create essential task on it.

b. Select the Task Trigger –> When the Computer starts

c. Select the start a program.

d. Select your script by browser button and put the location of the script also in the START IN(OPTIONAL) field.

e. Click on properties and press the finish button.

f. Change the setting to RUN WHETHER USER IS LOGGED ON OR NOT.

Thanks for sharing the script.
We need to know first if data server is with database in standup mode or not. Maybe is in primary mode after switch…
Am I right?
Thanks
This script is only to start standby database.
Because primary database is start with window seevices but standby recovery is not started
Dear SandeepSingh DBA
Thanks for your answer but you need to know where standby is to start that script… imagine that you implement that start script via Window Task Scheduler in a server that became primary?
Thanks
Paulo
Dear SandeepSingh DBA
Sorry, the last post was from me
Thanks
Yes thanks for update, you can put one check in between this script which let you check before firing command that database server is Standby or Primary. By using this statement:
select database_role from v$database;
Before open the databases, you need to know which is PRIMARY and STANDBY to run the STARTUP or STARTUP MOUNT command.
So, you can’t run any command…
Thanks
Paulo
you can run this command at mount stage.
SQL> shutdown abort
ORACLE instance shut down.
SQL> startup mount
ORACLE instance started.
Total System Global Area 1610609744 bytes
Fixed Size 9028688 bytes
Variable Size 1258291200 bytes
Database Buffers 335544320 bytes
Redo Buffers 7745536 bytes
Database mounted.
SQL> select database_role from v$database;
DATABASE_ROLE
—————-
PRIMARY
Dear SandeepSingh DBA,
On primary is not a problem, you check witch is primary after mount mode and then “alter database open;”
The problem is on standby. You mount standby and then, what you will do to set as standby?
> ALTER DATABASE MOUNT STANDBY DATABASE; (you need this to knowledge by observer)
> ALTER DATABASE RECOVER MANAGED STANDBY DATABASE DISCONNECT FROM SESSION;
Thanks
You can also simply mount standy by with ALTER DATABASE MOUNT command and Check..thanks
As oracle database and there by database starts automatically. create this trigger in primary ;it should reflect in standby and do the job for you:
CREATE OR REPLACE TRIGGER startup_mount
AFTER STARTUP ON DATABASE
DECLARE
role VARCHAR(30);
BEGIN
SELECT DATABASE_ROLE INTO role FROM V$DATABASE;
IF role ‘PRIMARY’ THEN
alter database close;
alter database recover managed standby database using current logfile disconnect from session;
END IF;
END;
/
Please read as “As oracle database windows service and …”
my bad the line should
IF role ‘PRIMARY’ THEN
my bad the line should
IF role !=‘PRIMARY’ THEN
Final version :
CREATE OR REPLACE TRIGGER startup_mount
AFTER STARTUP ON DATABASE
DECLARE
role VARCHAR(30);
BEGIN
SELECT DATABASE_ROLE INTO role FROM V$DATABASE;
IF role != ‘PRIMARY’ THEN
execute IMMEDIATE ‘alter database close’;
execute IMMEDIATE ‘alter database recover managed standby database using current logfile disconnect from session’;
END IF;
END startup_mount;
/