SCHEMA REFRESH FROM PROD TO DEV DATABASE - DIFFERENT SERVERS.pdf
-
Upload
grthiyagu-oracle-dba -
Category
Documents
-
view
64 -
download
1
Transcript of SCHEMA REFRESH FROM PROD TO DEV DATABASE - DIFFERENT SERVERS.pdf
-
SCHEMA REFRESH IN ORACLE USING EXP/IMP
Exploring the Oracle DBA Technology by Gunasekaran ,Thiyagu
SCHEMA REFRESH
SCHEMA REFRESH is a regular job for DBAs. So I decide to prepare an informative document for
schema refresh. We can achieve schema refresh in two ways, they are
1) Refresh the database schema with Production database.
2) Refresh the database schema from Production database to some other databases like DEV/TEST/...
SOURCE DATABASE : CRMS
TARGET DATABASE : HRMS
SOURCE SERVER 192.168.1.130 (SERVER1.ORACLE.COM) PRODUCTION
TARGET SERVER 192.168.1.131 (SERVER2.ORACLE.COM) DEVELOPMENT
REFRESH SCHEMA FROM PRODUCTION TO DEVELOPMENT
Find the roles & privileges that are assigned to the schema.
Capture source database schema objects count.
Export database schema using EXP/EXPDP utility.
Send the full export backup .dmp file to target server.
Create the schema with default tablespace and allocate quota.
Make required roles & privileges for the schema.
Connect to user and ensure default tablespace, roles and privileges.
Import the dump file using IMP/IMPDP into the target database.
Recompile invalid objects.
Gather schema statistics after the refresh.
If the schema is already existing on the target database server, then take export backup of existing
schema from the target database. After that you can drop it and recreate the schema.
NETWORK AVAILABILITY BETWEEN TWO SERVERS
$ hostname -i
192.168.1.130
$ ping 192.168.1.131
PING 192.168.1.131 (192.168.1.131) 56(84) bytes of data.
64 bytes from 192.168.1.131: icmp_seq=0 ttl=64 time=4.10 ms
64 bytes from 192.168.1.131: icmp_seq=1 ttl=64 time=0.156 ms
64 bytes from 192.168.1.131: icmp_seq=2 ttl=64 time=0.216 ms
...
$ hostname i
192.168.1.131
$ ping 192.168.1.130
PING 192.168.1.130 (192.168.1.130) 56(84) bytes of data.
64 bytes from 192.168.1.130: icmp_seq=0 ttl=64 time=15.2 ms
64 bytes from 192.168.1.130: icmp_seq=1 ttl=64 time=0.263 ms
64 bytes from 192.168.1.130: icmp_seq=2 ttl=64 time=0.134 ms
...
-
SCHEMA REFRESH IN ORACLE USING EXP/IMP
Exploring the Oracle DBA Technology by Gunasekaran ,Thiyagu
PRE - REFRESH ACTIVITIES
SYS> select username, account_status, default_tablespace, temporary_tablespace
FROM dba_users WHERE username=upper('&username');
Enter value for username: SHAM
old 2: where username=upper('&username')
new 2: where username=upper('SHAM')
USERNAME ACCOUNT_STATUS DEFAULT_TABLESPACE TEMPORARY_TABLESPACE
--------- ----------------- -------------------- ---------------------
SHAM OPEN TBS1 TEMP
SYS> select owner, sum(bytes)/1024/1024/1024 "SIZE in GB"
FROM dba_segments WHERE owner=upper('&owner') GROUP BY owner;
Enter value for owner: sham
old 2: where owner=upper('&owner') group by owner
new 2: where owner=upper('sham') group by owner
OWNER SIZE in GB
------- ----------
SHAM 40.35
SYS> select distinct tablespace_name from dba_segments WHERE owner in ('SHAM');
TABLESPACE_NAME
----------------
TBS1
TBS2
TBS3
USERS
FIND THE TABLESPACES AND SIZE OF THAT SCHEMA
SYS> select tablespace_name, sum(bytes/1024/1024/1024) "Size in GB"
from dba_segments where owner='SHAM' group by tablespace_name;
TABLESPACE_NAME Size in GB
------------------------------ ----------
TBS1 9.062
TBS2 6.048
TBS3 8. 82
USERS 16. 42
CHECK TABLESPACE QUOTAS FOR THAT SCHEMA
SYS> select * from dba_ts_quotas where username='SHAM';
..
...
[Trimmed]
Quota is the amount of space allocated to a user in a tablespace. In dba_ts_quotas view, MAXBYTES
column value of -1 indicates UNLIMITED, means that user can use as much space in that tablespace.
-
SCHEMA REFRESH IN ORACLE USING EXP/IMP
Exploring the Oracle DBA Technology by Gunasekaran ,Thiyagu
SPOOL THE OUTPUT OF ROLES & PRIVILEGES ASSIGNED TO THE USER
SYS> select * from dba_role_privs where grantee in('SHAM');
GRANTEE GRANTED_ROLE ADM DEF
------------------------------ ------------------------------ --- ---
SHAM RESOURCE NO YES
SHAM CONNECT NO YES
SYS> select * from dba_sys_privs where grantee in('SHAM');
GRANTEE PRIVILEGE ADM
------------------------------ ---------------------------------------- ---
SHAM CREATE SEQUENCE NO
SHAM CREATE ANY SEQUENCE NO
SHAM CREATE VIEW NO
SHAM CREATE DATABASE LINK NO
SHAM CREATE SYNONYM NO
SHAM CREATE PROCEDURE NO
SHAM CREATE TRIGGER NO
SHAM CREATE ANY TABLE NO
SHAM UNLIMITED TABLESPACE NO
9 rows selected.
SHAM> spool roles_privs.sql;
SHAM> select 'Grant' || role ||' to SHAM' "ROLES_PRIVILIGES" from session_roles;
SHAM> select 'Grant' || privilege ||' to SHAM' "ROLES_PRIVILIGES" from session_privs;
SHAM> spool off;
CHECK DATABASE LINKS FOR THAT SCHEMA
SYS> select owner, db_link, username from dba_db_links WHERE owner='SHAM';
OWNER DB_LINK USERNAME
------ ---------- --------
SHAM TESTLINK ROSE
CHECK ALL OBJECTS IN THAT SCHEMA
SYS> select owner, object_type, count(*) from dba_objects
WHERE owner=upper('&owner') AND object_name not like 'BIN$%'
GROUP BY object_type, owner ORDER BY object_type;
Enter value for owner: sham
OWNER OBJECT_TYPE COUNT(*)
------------------------------ ------------------- ----------
SHAM FUNCTION 1
SHAM INDEX 10
SHAM PACKAGE 1
SHAM PACKAGE BODY 1
SHAM PROCEDURE 1
-
SCHEMA REFRESH IN ORACLE USING EXP/IMP
Exploring the Oracle DBA Technology by Gunasekaran ,Thiyagu
SHAM SEQUENCE 2
SHAM SYNONYM 1
SHAM TABLE 12
SHAM TABLE PARTITION 3
SHAM TRIGGER 2
SHAM VIEW 1
SHAM DATABASE LINK 1
12 rows selected.
FIND TOTAL OBJECTS IN THAT SCHEMA
SHAM> select object_name, object_type, subobject_name, status FROM user_objects;
OBJECT_NAME OBJECT_TYPE SUBOBJECT_NAME STATUS
---------------------------- ---------------- --------------- -------
GRADE TABLE VALID
PROJECT TABLE VALID
TRANSPORT TABLE VALID
EMP_PROJ_INFO TABLE VALID
EMP_PERS_INFO TABLE VALID
EMP_INFO TABLE VALID
PAYROLL TABLE VALID
EMP TABLE VALID
DEPT TABLE VALID
SEQ_EMP_INFO1 SEQUENCE VALID
SEQ_EMP_INFO SEQUENCE VALID
TRI_EMP TRIGGER VALID
EMP_AUDIT TABLE VALID
LOGTAB TABLE VALID
USRLOG TRIGGER VALID
VEMP_INFO VIEW VALID
CONT_EMP TABLE VALID
CONT_EMP TABLE PARTITION P1 VALID
CONT_EMP TABLE PARTITION P2 VALID
CONT_EMP TABLE PARTITION P3 VALID
MULT_TAB FUNCTION VALID
PKGSAL PACKAGE BODY VALID
PROC1 PROCEDURE VALID
PAYROLL_INFO SYNONYM VALID
PAYROLL_SAL_IN4 INDEX VALID
EMP_DPID_IN1 INDEX VALID
EMP_PRJ_INFO_PRJID_IN3 INDEX VALID
EMP_PRJ_INFO_DPID_IN2 INDEX VALID
PKGSAL PACKAGE VALID
PAYROLL_ACCNO_C11_UQ INDEX VALID
EMP_PRJ_INFO_MAILID_C9_UQ INDEX VALID
PROJ_PRJID_C4_PK INDEX VALID
DEPT_DEPTID_C3_PK INDEX VALID
EMP_EMPID_C1_PK INDEX VALID
-
SCHEMA REFRESH IN ORACLE USING EXP/IMP
Exploring the Oracle DBA Technology by Gunasekaran ,Thiyagu
GRD_EMPGRD_C12_PK INDEX VALID
TESTLINK DATABASE LINK VALID
36 rows selected.
SHAM> select constraint_name, constraint_type, table_name, r_constraint_name,
r_owner FROM all_constraints WHERE owner='SHAM';
CONSTRAINT_NAME CONST TABLE_NAME R_CONSTRAINT_NAME R_OWNER
------------------------------ ----- -------------------- -------------------- --------
PAYROLL_EMPID_C7_FK R PAYROLL EMP_EMPID_C1_PK SHAM
EMP_PRJ_INFO_EMPID_C5_FK R EMP_PROJ_INFO EMP_EMPID_C1_PK SHAM
EMP_PERS_INFO_EID_C6_FK R EMP_PERS_INFO EMP_EMPID_C1_PK SHAM
TRNSPRT_PRJID_C10_FK R TRANSPORT EMP_EMPID_C1_PK SHAM
EMP_ELEVEL_C13_FK R EMP GRD_EMPGRD_C12_PK SHAM
GRD_EMPGRD_C12_PK P GRADE
PROJ_PRJID_C4_PK P PROJECT
PAYROLL_ACCNO_C11_UQ U PAYROLL
EMP_PRJ_INFO_MAILID_C9_UQ U EMP_PROJ_INFO
EMP_EMPID_C1_PK P EMP
DEPT_DEPTID_C3_PK P DEPT
EMP_DEPTID_C2_NTNL C EMP
EMP_PRJ_INFO_DEPTID_C8_NL C EMP_PROJ_INFO
GRD_DESC_C14_NL C GRADE
14 rows selected.
FIND INDEXES AND ITS ASSOCIATED TABLESPACES
SHAM> select table_name, index_name, tablespace_name from user_indexes;
TABLE_NAME INDEX_NAME TABLESPACE_NAME
------------------------------ ------------------------------ ------------------------------
PROJECT PROJ_PRJID_C4_PK TBS1
PAYROLL PAYROLL_ACCNO_C11_UQ TBS1
PAYROLL PAYROLL_SAL_IN4 USERS
EMP_PROJ_INFO EMP_PRJ_INFO_MAILID_C9_UQ TBS1
EMP_PROJ_INFO EMP_PRJ_INFO_DPID_IN2 USERS
EMP_PROJ_INFO EMP_PRJ_INFO_PRJID_IN3 USERS
EMP EMP_EMPID_C1_PK TBS1
EMP EMP_DPID_IN1 USERS
DEPT DEPT_DEPTID_C3_PK TBS1
GRADE GRD_EMPGRD_C12_PK TBS1
10 rows selected.
FIND THE ENCRYPTED PASSWORD FOR THE SCHEMA
SYS> select password from sys.user$ where name='SHAM';
PASSWORD
----------------
8657483259C22FDE
-
SCHEMA REFRESH IN ORACLE USING EXP/IMP
Exploring the Oracle DBA Technology by Gunasekaran ,Thiyagu
EXPORT THE SCHEMA
Export the schema from the crms database with the name of the file as refresh_sham.dmp
$ export ORACLE_SID=crms
$ exp system/manager file=refresh_sham.dmp log=schema_refresh_sham.log owner=sham
direct=y statistics=none
Export: Release 11.2.0.1.0 - Production on Fri May 28 23:03:36 2015
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Export done in US7ASCII character set and AL16UTF16 NCHAR character set
server uses WE8MSWIN1252 character set (possible charset conversion)
About to export specified users ...
. exporting pre-schema procedural objects and actions
. exporting foreign function library names for user SHAM
. exporting PUBLIC type synonyms
. exporting private type synonyms
. exporting object type definitions for user SHAM
About to export SHAM's objects ...
. exporting database links
. exporting sequence numbers
. exporting cluster definations
. about to export SHAMs tables via Direct path ...
. . exporting table CONT_EMP
. . exporting partition P1 42202 rows exported
. . exporting partition P2 44020 rows exported
. . exporting partition P3 42015 rows exported
. . exporting table DEPT 12 rows exported
. . exporting table EMP 168022 rows exported
. . exporting table EMP_AUDIT 24 rows exported
. . exporting table EMP_INFO 168022 rows exported
. . exporting table EMP_PERS_INFO 168022 rows exported
. . exporting table EMP_PROJ_INFO 168022 rows exported
. . exporting table GRADE 12 rows exported
. . exporting table LOGTAB 2666 rows exported
. . exporting table PAYROLL 168022 rows exported
. . exporting table PROJECT 22 rows exported
. . exporting table TRANSPORT 148213 rows exported
. exporting synonyms
. exporting views
. exporting stored procedures
. exporting operators
. exporting referential integrity constraints
. exporting triggers
. exporting indextypes
. exporting bitmap, functional and extensible indexes
-
SCHEMA REFRESH IN ORACLE USING EXP/IMP
Exploring the Oracle DBA Technology by Gunasekaran ,Thiyagu
. exporting posttables actions
. exporting materialized views
. exporting snapshot logs
. exporting job queues
. exporting refresh groups and children
. exporting dimensions
. exporting post-schema procedural objects and actions
. exporting statistics
Export terminated successfully without warnings.
SEND THE FULL EXPORT BACKUP TO THE TARGET DATABASE SERVER
$ gzip refresh_sham.dmp
$ scp refresh_sham.dmp.gz roles_privs.sql [email protected]:$HOME
[email protected]'s password:
refresh_sham.dmp.gz
IN TARGET SERVER GUNZIP .DMP EXPORT FILE
$ gunzip refresh_sham.dmp.gz
$ ls refresh*
refresh_sham.dmp
STEPS TO DROP AND RECREATE THE SCHEMA
Before drop the schema from the target database, take export backup of the schema.
Drop the schema(sham) from the crms database.
Recreate the user with default tablespace and quotas as per source tablespace tablespaces.
Assign temporary tablespace to that user.
Execute the spooled script (roles_privs.sql) which contains roles and privileges to the user.
EX : SYS>@roles_privs.sql;
Script grants the roles and privileges to the user obtained before dropping it.
Connect to the user and verify allocated tablespaces, roles and privileges.
** Before drop all the objects in that Schema, connect the schema and Spool the output.
POINTS TO NOTE
Suppose if you want to remove tables manually first you have to delete child tables otherwise
oracle wont allow directly to drop parents table. Lets see.
SHAM> drop table emp;
drop table emp
*
ERROR at line 1:
ORA-02449: unique/primary keys in table referenced by foreign keys
While dropping tables manually we can see ORA-02449 error. Use the below query to find reference
key constraints and drop the tables first.
-
SCHEMA REFRESH IN ORACLE USING EXP/IMP
Exploring the Oracle DBA Technology by Gunasekaran ,Thiyagu
SHAM> spool ref_constraints.sql;
SHAM> Select constraint_name, constraint_type, table_name
FROM all_constraints WHERE constraint_type='R' AND
r_constraint_name in (select constraint_name from all_constraints where table_name='EMP');
CONSTRAINT_NAME C TABLE_NAME
------------------------------ - ------------------------------
EMP_PRJ_INFO_EMPID_C5_FK R EMP_PROJ_INFO
PAYROLL_EMPID_C7_FK R PAYROLL
TRNSPRT_PRJID_C10_FK R TRANSPORT
EMP_PERS_INFO_EID_C6_FK R EMP_PERS_INFO
SCRIPT TO FIND & DROP OBJECTS FROM THAT SCHEMA
SHAM> spool drop_tables.sql;
SHAM> select 'drop table '||table_name||' cascade constraints purge; 'from user_tables;
SHAM> spool off;
SHAM> spool drop_other_objects.sql;
SHAM> select 'drop '||object_type||' '||object_name||';' from user_objects;
SHAM> spool off;
SHAM> DISC
YOU CAN DROP THE SCHEMA.
DROP THE SCHEMA
SYS> drop user sham cascade; # note SCN 4607636
User dropped.
RECREATE THE USER WITH DEFAULT TABLESPACE , TEMPORARY TABLESPACE & QUOTAS
SYS> create user sham identified by values '8657483259C22FDE'
default tablespace tbs1
quota unlimited on tbs1
quota unlimited on tbs2
quota unlimited on tbs3 # Need to create all the tablespaces, if they are not exist.
quota unlimited on users
temporary tablespace temp
PROFILE sw_dev; @roles_privs.sql;
roles_privs.sql
By executing above script we can assign all privileges to the new user as per source database.
you can do them manually.
-
SCHEMA REFRESH IN ORACLE USING EXP/IMP
Exploring the Oracle DBA Technology by Gunasekaran ,Thiyagu
IMPORT DATA INTO CRMS DATABASE
$ export ORACLE_SID=crms
$ imp system/manager file=refresh_sham.dmp log=refresh_sham_imp.log fromuser=sham touser=sham
Import: Release 11.2.0.1.0 - Production on Fri May 28 23:23:12 2015
Copyright (c) 1982, 2009, Oracle and/or its affiliates. All rights reserved.
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
Export file created by EXPORT:V11.02.00 via direct path
import done in US7ASCII character set and AL16UTF16 NCHAR character set
import server uses WE8MSWIN1252 character set (possible charset conversion)
. importing SHAM's objects into SHAM
. . importing partition "CONT_EMP":"P1" 42202 rows imported
. . importing partition "CONT_EMP":"P2" 44020 rows imported
. . importing partition "CONT_EMP":"P3" 42015 rows imported
. . importig table "DEPT" 12 rows imported
. . importing table "EMP" 168022 rows imported
. . importing table "EMP_AUDIT" 24 rows imported
. . importing table "EMP_INFO" 168022 rows imported
. . importing table "EMP_PERS_INFO" 168022 rows imported
. . importing table "EMP_PROJ_INFO" 168022 rows imported
. . importing table "GRADE" 12 rows imported
. . importing table "LOGTAB" 2666 rows imported
. . importing table "PAYROLL" 168022 rows imported
. . importing table "PROJECT" 22 rows imported
. . importing table "TRANSPORT" 148213 rows imported
About to enable constraints...
Import terminated successfully without warnings.
When import schema, you may get error ORA-01917
. . importing table "EMP" 168022 rows imported
IMP-00017: following statement failed with ORACLE error 1917:
"GRANT SELECT ON "EMP" TO "ROSE""
IMP-00003: ORACLE error 1917 encountered
ORA-01917: user or role 'ROSE' does not exist
Solution: You need to create user rose in the target database.
If the schema made any object privilege to other users in the source database, you have to assign
the same privilege to the user in target database. You must create the user in target database.
In source database, you can get information using following sql statement.
SHAM> select * from user_tab_privs_made;
..
-
SCHEMA REFRESH IN ORACLE USING EXP/IMP
Exploring the Oracle DBA Technology by Gunasekaran ,Thiyagu
BASIC CHECKS AFTER IMPORT THE SCHEMA
SYS> select owner, object_type, count(object_type) from dba_objects
WHERE owner ='SHAM' GROUP BY owner, object_type;
** Schema object count should be same before export and after import.
SYS> select username, password, default_tablespace, temporary_tablespace,
profile from dba_users WHERE username ='SHAM';
SYS> select owner,sum(bytes)/1024/1024/1024 "SIZE in GB" from dba_segments
WHERE owner=upper('&owner') GROUP BY owner;
SYS> select * from dba_role_privs where grantee in('SHAM');
SYS> select * from dba_sys_privs where grantee in('SHAM');
SYS> select * from dba_ts_quotas where username='SHAM';
FIND THE INVALID OBJECTS FOR THE SCHEMA
SYS> select owner, object_name, object_type, status from dba_objects
WHERE status 'VALID' AND OWNER='SHAM';
If you find any invalid objects execute following script.
SYS> @?/rdbms/admin/utlrp.sql;
Check the privileges, tablespace quotas, and dblinks for that the schema. This is essential.
ARE ALL GRANTS IMPORTED ?
NO. Roles and profiles are NOT exported.
If the schema received grant privilege from others does NOT get export when you export the schema.
So, it does NOT get import when you import the schema.
If the schema made grant privileges to others, those are exported when you export the schema.
1) Grants given by a user are exported with the user.
SHAM> grant select on sham.emp to scott;
SHAM> grant update on sham.emp to scott;
2) Grants received by a user are NOT exported with the user.
SCOTT> grant select on scott.salgrade to sham;
SCOTT> grant update on scott.salgrade to sham;
GATHER NEW STATISTICS FOR THE SCHEMA
SYS> exec dbms_stats.gather_schema_stats(ownname =>'SHAM', method_opt=>'FOR ALL INDEXES FOR ALL
INDEXED COLUMNS SIZE 1',estimate_percent => 15,granularity=>'ALL',cascade=>TRUE);
PL/SQL procedure successfully completed.