Difference between revisions of "Auto Reset Oracle Database to Actual"

From SmartHCM Wiki
Jump to navigation Jump to search
(Created page with "Create a script to reset a demo database; <pre> vim /home/oracle/resetdemodatabase.sh </pre> Write following code in resetdemodatabase.sh file; <pre> sqlplus system/passwor...")
 
Line 9: Line 9:
 
<pre>
 
<pre>
 
sqlplus system/password@smarthcmdemo  @drop_user.sql
 
sqlplus system/password@smarthcmdemo  @drop_user.sql
impdp system/password@smarthcmdemo remap_schema=SMARTHCM_DEMO:SMARTHCM_DEMO remap_tablespace=SMARTHCM:SMARTHCM directory=data_demo dumpfile=smarthcmdemo.dmp logfile=smarthcmdemo.log Transform=oid:n
+
impdp system/password@smarthcmdemo remap_schema=SMARTHCM_DEMO:SMARTHCM_DEMO remap_tablespace=SMARTHCM:SMARTHCM directory=data_demo
 +
dumpfile=smarthcmdemo.dmp logfile=smarthcmdemo.log Transform=oid:n
 
sqlplus system/password@smarthcmdemo  @recompile_objects.sql
 
sqlplus system/password@smarthcmdemo  @recompile_objects.sql
 
</pre>
 
</pre>

Revision as of 13:02, 22 December 2016

Create a script to reset a demo database;

vim /home/oracle/resetdemodatabase.sh

Write following code in resetdemodatabase.sh file;

sqlplus system/password@smarthcmdemo  @drop_user.sql
impdp system/password@smarthcmdemo remap_schema=SMARTHCM_DEMO:SMARTHCM_DEMO remap_tablespace=SMARTHCM:SMARTHCM directory=data_demo
dumpfile=smarthcmdemo.dmp logfile=smarthcmdemo.log Transform=oid:n
sqlplus system/password@smarthcmdemo  @recompile_objects.sql

Create a script to drop user;

vim /home/oracle/drop_user.sql

Write following code in drop_user.sql file;

SET SERVEROUTPUT ON
DECLARE
V_CHCK_USER NUMBER;
BEGIN

  FOR CUR IN (SELECT '''' || S.SID || ', ' || S.SERIAL# || '''' SNO
                FROM V$SESSION S
               WHERE S.USERNAME = 'SMARTHCM_DEMO') LOOP
    DBMS_OUTPUT.PUT_LINE('Disconnecting all users from SmartHCM_DEMO user');
    EXECUTE IMMEDIATE 'ALTER SYSTEM DISCONNECT SESSION ' || CUR.SNO ||
                      ' IMMEDIATE';

  END LOOP;

SELECT COUNT(1) INTO V_CHCK_USER
FROM dba_users WHERE username like 'SMARTHCM_DEMO';

IF V_CHCK_USER = 1 THEN
  DBMS_OUTPUT.PUT_LINE('Droping the SmartHCM_DEMO please wait for a while.....');
  EXECUTE IMMEDIATE 'DROP USER SMARTHCM_DEMO CASCADE';
END IF;

END;
/
exit

Create a script to compile invalid objects;

vim /home/oracle/recompile_objects.sql

Write following code in recompile_objects.sql file;

SET SERVEROUTPUT ON
BEGIN dbms_output.put_line('Compiling invalid objects. Please wait...'); END;
EXEC DBMS_UTILITY.compile_schema(schema => 'SMARTHCM_DEMO');
exit

Create a cron job;

crontab -e

Insert following schedule in cron job;

0 3 * * * /home/oracle/resetdemodatabase.sh

Understanding Cron Job scheduler;

Crontab Format:
Minute | Hour | Day | Month | DayOfWeek | user | ThingToDo

'user' is the account name for the 'ThingToDo' to be run under. Exceptionally useful. You don't want every job running as root do you?
'ThingToDo' is as per any command line syntax. Typically, it invokes a script.
'Day' and 'Date' are easy to understand, but very difficult to read in their raw form

Thus, our cron job will execute the script oraclebackup.sh on 03:00 hours daily.