oracle_通过表空间迁移(TTS)实现快速数据库重建
(2015-08-25 23:19:59)
标签:
ttsrecreatedatabaseselfcheck |
分类: oracleops |
1>OLD:检查表空间是否符合传输条件
REM --------------------------------------------------------------------------
REM In case TTS has not been used yet there
will be the need to pre-create
REM table
TRANSPORT_SET_VIOLATIONS
REM below will raise an error but will create
TRANSPORT_SET_VIOLATIONS
REM
ʹÓÃDBMS_TTS±í¿Õ¼ä¼ì²é²»´æÔڵıí¿Õ¼äʵÏÖÌáǰ´´½¨TRANSPORT_SET_VIOLATIONS±í¡£
exec dbms_tts.transport_set_check(
REM tts_check
REM --------------------------------------------------------------------------
REM Below is a check if the set
of TTS is possible, in other words:
REM Checks if a set of tablespaces (to
be transported) is self-contained
REM in case errors are raised you have
to check and resolve them
create or replace procedure
tts_check(tablespace_names in varchar2)
is
cursor c_TRANSPORT_SET_VIOLATIONS is
begin
end;
/
show errors
set serveroutput on
exec dbms_output.enable(1000000);
REM The list of tablespaces is provided
as 1 string
value, example:
REM exec tts_check('USERS,TOOLS,MY_DATA1,MY_INDEX1');
exec tts_check('&tablespace_names');
在我的例子中,创建一张表在USERS表空间中,且对应索引在IND表空间中。
检查结果如下。
SQL>
exec tts_check('&tablespace_names');
Enter value for tablespace_names: USERS
Starting to check tablespaces as
specified
ORA-39907: Index
SUN.IDX_TEST1 in tablespace IND points to
table SUN.TEST1 in
tablespace USERS.
In case there are no line(s)
after 'Starting to check
...'
It does imply that the check went fine and
there are no issues to resolve.
PL/SQL procedure successfully completed.
SQL>
exec tts_check('&tablespace_names');
Enter value for tablespace_names: IND
Starting to check tablespaces as
specified
ORA-39907: Index
SUN.IDX_TEST1 in tablespace IND points to
table SUN.TEST1 in
tablespace USERS.
In case there are no line(s)
after 'Starting to check
...'
It does imply that the check went fine and
there are no issues to resolve.
PL/SQL procedure successfully completed.
SQL> /
Procedure created.
SQL> exec tts_check('&tablespace_names');
Enter value for tablespace_names: USERS,IND
Starting to check tablespaces as
specified
In case there are no line(s)
after 'Starting to check
...'
It does imply that the check went fine and
there are no issues to resolve.
PL/SQL procedure successfully completed.
在现网的环境中,往往都是将表和索引分布到不同的表空间中。
这里我们传输USERS和IND表空间。
2>
column
parameter format
a30;
column value format a30;
select * from nls_database_parameters where
parameter like '%CHARACTERSET%';
3>
Set
the correct
NLS-characterset:
export NLS_LANG='.'
Set the correct ORA_NLS parameter:
Note 77442.1 ORA_NLS (ORA_NLS32,
ORA_NLS33, ORA_NLS10) Env Variables
explained
10.x: export
ORA_NLS10=$ORACLE_HOME/nls/data
9.x:
Expdp example:
connect / as sysdba
create directory tts_exp as
'/u01/app2/tmp';
expdp_full_norows.par:
userid=system
directory=tts_exp
dumpfile=expdp_full_norows.dmp
logfile=expdp_full_norows.log
full=y
content=metadata_only
expdp parfile=expdp_full_norows.par
Check the logfile as created, it should
not contain errors/warnings.
(for instance expdp does not support
XMLSchema objects)
4>
$>
export DISPLAY=....
$> dbca &
The quickest way will be to use the
template database as present in
ORACLE_HOME. As mentioned before you have to
use the same NLS settings of the
original database. For the rest of
the parameters you can use a copy of
the one of the original database, which
can be done after database creation
*In case of versions <</span>
10.2.0.x you might need
to run the upgrade script in order
to upgrade datadictionary to correct patch
level
5>
OLD: 获取创建用户语句
REM
tts_exp_users
REM --------------------------------------------------------------------------
REM Below will show all users who do have objects in
the tablespaces
REM to be transported. The output will be spooled
to screen and will consist
REM of create user statements. The
correct password/privileges/... will
REM be imported/set by the full
export/import
--使用下面过程获取创建用户脚本,而权限和密码通过FULL导入处理。
create or replace procedure
tts_exp_users(tablespace_names in varchar2)
is
string varchar2(4096);
type userlist is table of
dba_users.username%type;
users userlist;
type cursor_ref is ref cursor;
c_cur cursor_ref;
begin
string:=chr(39)||upper(replace(replace(tablespace_names,'
'),',',''','''))||chr(39);
dbms_output.put_line(string);
open c_cur for 'select distinct owner from
dba_segments
where tablespace_name in ('||string||')';
fetch c_cur bulk collect into
users;
close c_cur;
for i in users.first ..
users.last
loop
dbms_output.put_line('create user '||users(i)||'
identified by '||users(i)||';');
end loop;
end;
/
set
serveroutput
on
exec dbms_output.enable(1000000);
REM The list of tablespaces is provided
as 1 string
value, example:
REM exec tts_exp_users('USERS,TOOLS,MY_DATA1,MY_INDEX1');
SQL> exec
tts_exp_users('&tablespace_names');
Enter value for tablespace_names: USERS,IND
'USERS','IND'
create user SUN identified by SUN;
PL/SQL procedure successfully completed.
6>
REM
tts_readonly
REM --------------------------------------------------------------------------
REM tts_readonly is a procedure to put
all tablespaces as present in
string
REM in read only
mode.
create or replace procedure
tts_readonly(tablespace_names in varchar2)
is
string
ts_fail integer:=0;
type tablespacetyp is table of
dba_tablespaces%rowtype;
tslist tablespacetyp;
type cursor_ref is ref cursor;
c_cur cursor_ref;
begin
tslist(i).status);
end;
/
set
serveroutput
on
exec dbms_output.enable(1000000);
REM The list of tablespaces is provided
as 1 string
value, example:
REM exec tts_readonly('USERS,IND');
SQL> exec tts_readonly('&tablespace_names');
Enter value for tablespace_names: USERS,IND
PL/SQL procedure successfully completed.
SQL> SELECT tablespace_name,status FROM dba_tablespaces;
TABLESPACE_NAME
------------------------------ ---------
SYSTEM
SYSAUX
UNDOTBS1
TEMP
USERS
IND
7>
Set
the correct
NLS-characterset:
$> export NLS_LANG='.'
Set the correct ORA_NLS parameter:
Note 77442.1 ORA_NLS (ORA_NLS32,
ORA_NLS33, ORA_NLS10) Env Variables
explained
10.x: export
ORA_NLS10=$ORACLE_HOME/nls/data
9.x:
expdp_tts.par:
userid=system
directory=tts_exp
dumpfile=expdp_tts.dmp
logfile=expdp_tts.log
transport_full_check=y
transport_tablespaces=(USERS,IND)
$> expdp parfile=expdp_tts.par
Check the logfile as created, it should
not contain errors/warnings.
8>
Set
the correct
NLS-characterset:
export NLS_LANG='.'
Set the correct ORA_NLS parameter:
Note 77442.1 ORA_NLS (ORA_NLS32,
ORA_NLS33, ORA_NLS10) Env Variables
explained
10.x: export
ORA_NLS10=$ORACLE_HOME/nls/data
9.x:
Expdp example:
connect / as sysdba
create directory tts_exp as
'/u01/app2/tmp';
expdp_full_norows.par:
userid=system
directory=tts_exp
dumpfile=expdp_full_norows.dmp
logfile=expdp_full_norows.log
full=y
content=metadata_only
expdp parfile=expdp_full_norows.par
Check the logfile as created, it should
not contain errors/warnings.
(for instance expdp does not support
XMLSchema objects)
9>
这里我们因为是在一台机器上,且创建成把原库毁掉,且转换到新数据库
OLD: OVERVIEW所有相关的数据文件
set
serveroutput
on
exec dbms_output.enable(1000000);
REM The list of tablespaces is provided
as 1 string
value, example:
REM exec tts_show_datafiles('USERS,IND');
SQL> exec tts_show_datafiles('&tablespace_names');
Enter value for tablespace_names: USERS,IND
Tablespace: IND File:
/u01/app2/ora11/oradata/prim/index01.dbf
Tablespace: USERS File:
/u01/app2/ora11/oradata/prim/users01.dbf
PL/SQL procedure successfully completed.
SQL>
所有相关的数据文件CP到新地址,我们这里不执行。
10>
Set
the correct
NLS-characterset:
$> export NLS_LANG='.'
Set the correct ORA_NLS parameter:
Note 77442.1 ORA_NLS (ORA_NLS32,
ORA_NLS33, ORA_NLS10) Env Variables
explained
10.x: export
ORA_NLS10=$ORACLE_HOME/nls/data
9.x:
connect / as sysdba
create directory tts_imp as
'/u01/app2/tmp/';
impdp_tts.par:
userid=system
directory=tts_imp
dumpfile=expdp_tts.dmp
logfile=impdp_tts.log
transport_datafiles=('/u01/app2/ora11/oradata/prim/users01.dbf','/u01/app2/ora11/oradata/prim/index01.dbf')
[ora11@prim tmp]$ impdp parfile=impdp_tts.par
Import: Release 11.2.0.3.0 - Production
on Sun Sep 13 23:39:35 2015
Copyright (c) 1982,
2011, Oracle
and/or its affiliates.
Password:
Connected to: Oracle Database
With the Partitioning, OLAP, Data Mining
and Real Application Testing options
Master table "SYSTEM"."SYS_IMPORT_TRANSPORTABLE_01"
successfully loaded/unloaded
Starting "SYSTEM"."SYS_IMPORT_TRANSPORTABLE_01":
Processing object type TRANSPORTABLE_EXPORT/PLUGTS_BLK
Processing object type TRANSPORTABLE_EXPORT/TABLE
Processing object type TRANSPORTABLE_EXPORT/INDEX/INDEX
Processing object type TRANSPORTABLE_EXPORT/INDEX_STATISTICS
Processing object type
TRANSPORTABLE_EXPORT/POST_INSTANCE/PLUGTS_BLK
Job "SYSTEM"."SYS_IMPORT_TRANSPORTABLE_01" successfully completed
at 23:39:43
SQL> SELECT tablespace_name,status FROM
dba_tablespaces;
TABLESPACE_NAME
------------------------------ ---------
SYSTEM
SYSAUX
UNDOTBS1
TEMP
IND
USERS
11>
我们这里关闭原数据库,且在新库将表空间置为读写模式
关闭之前,在原库和新库上都能查下相关表,shutdown immediate 原库。
SQL> SELECT count(1) FROM sun.test1;
----------
SQL> SELECT tablespace_name,status FROM dba_tablespaces;
TABLESPACE_NAME
------------------------------ ---------
SYSTEM
SYSAUX
UNDOTBS1
TEMP
IND
USERS
6 rows selected.
SQL> SELECT count(1) FROM sun.test1;
----------
SQL> alter tablespace users read write;
Tablespace altered.
SQL> alter tablespace ind read write;
Tablespace altered.
SQL> DELETE FROM sun.test1 WHERE object_id > 100;
13558 rows deleted.
SQL> commit;
Commit complete.
SQL> SELECT count(1) FROM sun.test1;
----------
下面是提供的过程,来进行read write处理
REM
tts_readwrite
REM --------------------------------------------------------------------------
REM tts_readwrite is a procedure to put
all tablespaces as present in
string
REM in read only mode (only
valid if status is read only)
create or replace procedure
tts_readwrite(tablespace_names in varchar2)
is
string
ts_fail integer:=0;
type tablespacetyp is table of
dba_tablespaces%rowtype;
tslist tablespacetyp;
type cursor_ref is ref cursor;
c_cur cursor_ref;
begin
end;
/
set
serveroutput
on
exec dbms_output.enable(1000000);
REM The list of tablespaces is provided
as 1 string
value, example:
REM exec tts_readwrite('USERS,,IND');
exec tts_readwrite('&tablespace_names');
12>
OLD:
REM
tts_ts_users
REM --------------------------------------------------------------------------
REM tts_ts_users is a procedure extract the
alter user statements in order
REM to correct the default + temporary
tablespace of users of TTS
create or replace procedure
tts_ts_users(tablespace_names in varchar2)
is
string varchar2(4096);
type userlist is table of
dba_users.username%type;
users userlist;
type cursor_ref is ref cursor;
c_cur cursor_ref;
def_ts
temp_ts dba_users.temporary_tablespace%type;
begin
end;
/
set
serveroutput
on
exec dbms_output.enable(1000000);
REM The list of tablespaces is provided
as 1 string
value, example:
REM exec tts_ts_users('USERS,IND');
spool tts_exp_users_alter.sql
exec tts_ts_users('&tablespace_names');
spool off
在新库执行上面输出脚本。
13>
Set
the correct
NLS-characterset:
$> export NLS_LANG='.'
Set the correct ORA_NLS parameter:
Note 77442.1 ORA_NLS (ORA_NLS32,
ORA_NLS33, ORA_NLS10) Env Variables
explained
10.x: export
ORA_NLS10=$ORACLE_HOME/nls/data
9.x:
imp parfile=imp_full_norows.par
connect / as sysdba
create directory tts_imp as
'&directory_name';
impdp_full_norows.par:
userid=system
directory=tts_imp
dumpfile=expdp_full_norows.dmp
logfile=impdp_full_norows.log
full=y
content=metadata_only
table_exists_action=skip
impdp parfile=impdp_full_norows.par
14>
connect
/
as sysdba
@?/rdbms/admin/utlrp.sql
15)
OLD+NEW: Checking of objects per
schema
Same code being used on OLD and
NEW:
set pagesize 9999
spool check_objects_&old_new
column owner format a20;
select owner,object_type,count(*) from
dba_objects
group by owner,object_type order by
owner,object_type;
*) keep in mind that objects/grants/... made
in SYS account are not exported
*) remember the procedures TTS_... will show a
difference
$> stty cols 160
$> sdiff check_objects_old.lst check_objects_new.lst|more
16) OLD:
stop the database, cleanup after a
week/month/... after all is working
fine.