备份恢复

一些数据对象的查找脚本

July 28, 2012 maintain, oracle No comments

在帮银联恢复数据库的时候 客户要求导出库里的一些存储过程,这里整理了一些脚本供大家使用

创建view

select 
  'CREATE OR REPLACE VIEW '||O.NAME||' ('||
   replace(c.cols,',',','||chr(10))||')'||CHR(10)||
  'as'||chr(10), v.text
from
user$ u, obj$ o, view$ v,
( SELECT COL.OBJ#, COL.COLS
  FROM
  (SELECT 
    OBJ#, COL#, substr(SYS_CONNECT_BY_PATH(NAME,','),2) COLS
  FROM COL$
  WHERE COL# > 0
  START WITH COL# = 1
  CONNECT BY PRIOR OBJ# = OBJ# AND PRIOR COL# = COL# - 1 ) COL,
  (SELECT OBJ#, COUNT(*) COLCNT FROM COL$ 
  WHERE COL# > 0 GROUP BY OBJ#) CN
  WHERE COL.OBJ# = CN.OBJ# AND COL.COL# = CN.COLCNT
) C
where u.user#=o.owner# and o.obj# = c.obj#
  and v.obj# = o.obj# and u.name=':user'

需要导出sys.col$ sys.obj$

创建sequence

SELECT 
  'CREATE SEQUENCE '|| SEQ_NAME || 
  ' MINVALUE '||minval ||
  ' MAXVALUE '||MAXVAL ||
  ' START WITH '||LASTVAL ||
  ' ' || CYC || ' ' || ORD ||
  DECODE(SIGN(CACHE), 1,' CACHE '|| CACHE, 'NOCACHE') ||
  ';' SEQ_DDL
from
  (select u.name OWNER, o.name SEQ_NAME,
      s.minvalue MINVAL, s.maxvalue MAXVAL, 
      s.increment$ INC,
      decode (s.cycle#, 0, 'NOCYCLE', 1, 'CYCLE ') CYC,
      decode (s.order$, 0, 'NOORDER', 1, 'ORDER') ORD,
      s.cache, s.highwater LASTVAL
  from seq$ s, obj$ o, user$ u
  where u.user# = o.owner#
    and o.obj# = s.obj# 
    and u.name=':user')
    
需要导出sys.seq$ ,sys.user$ , sys.obj$

创建index

SELECT 
  'CREATE '||decode(bitand(IDX.property, 1), 1, 'UNIQUE', '')||
  ' INDEX '||I.NAME||' ON '||T.NAME||'('||IDX.PATH||');' INDEX_DDL
FROM 
  USER$ U, OBJ$  T, OBJ$ I, 
  (
    select I.PROPERTY, I.BO#, I.OBJ#, C.POS#,
            SUBSTR(sys_connect_by_path(CN.NAME,','),2) path
    from IND$ I, ICOL$ C, COL$ CN
    WHERE I.OBJ# = C.OBJ# AND I.BO# = C.BO#
      AND I.BO# = CN.OBJ# AND C.COL# = CN.INTCOL#
    start with C.POS#=1 
    connect by PRIOR I.OBJ# = I.OBJ# 
            AND prior C.POS# = C.POS# - 1 ) IDX,
  (SELECT I.BO#, I.OBJ#, COUNT(*) COLCNT 
    FROM ICOL$ I GROUP BY I.BO#, I.OBJ#) IDXC
WHERE 
  U.USER# = T.OWNER# AND 
  IDX.BO# = T.OBJ# AND
  IDX.OBJ# = I.OBJ# AND
  IDX.BO# =  IDXC.BO# AND
  IDX.OBJ# = IDXC.OBJ# AND
  IDX.POS# = IDXC.COLCNT AND
  U.NAME = ':user'
ORDER BY T.NAME, I.NAME

需要导出 sys.icol$, sys.col$,sys.ind$,sys.user$,sys.obj$

创建 存储过程

SELECT DECODE(S.LINE,1,'CREATE OR REPLACE ','')||SOURCE SOURCE
FROM 
  USER$ U, OBJ$  O, SOURCE$ S
WHERE 
  U.USER# = O.OWNER# AND 
  O.OBJ# = S.OBJ# AND
  U.NAME = ':user' 
  AND O.NAME = ':过程名'

需要导出 sys.obj$, sys.source$

创建 trigger and type

select u.name, o.name,
decode(o.type#, 12, 'TRIGGER', 13, 'TYPE', 14, 'TYPE BODY',
               'UNDEFINED') ,
DECODE(S.LINE,1,'CREATE OR REPLACE ','')||s.source 
from sys.obj$ o, sys.source$ s, sys.user$ u
where o.obj# = s.obj#
  and o.owner# = u.user#
  and u.name=':user'
  and ( o.type# in ( 12, 14) OR
       ( o.type# = 13 AND o.subname is null)) 

需要导出对象 sys.obj$,sys.source$,sys.user$

创建dblink 一般重新建就可以了 实在要弄 可以导出sys.link$,sys.user$

模拟一次online redo crash 的恢复

December 7, 2011 Internals, oracle No comments

[oracle@liu dbs]$ sqlplus ‘/as sysdba’

SQL*Plus: Release 10.2.0.1.0 – Production on Wed Dec 7 00:06:50 2011

Copyright (c) 1982, 2005, Oracle. All rights reserved.

Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 – Production
With the Partitioning, OLAP and Data Mining options

SQL> create table t as select * from dba_segments; —–创建测试表 t

Table created.

SQL> select * from v$log;

GROUP# THREAD# SEQUENCE# BYTES MEMBERS ARC STATUS FIRST_CHANGE# FIRST_TIM
———- ———- ———- ———- ———- — —————- ————- ———
1 1 27 52428800 1 NO CURRENT 782969 06-DEC-11
2 1 24 52428800 1 YES INACTIVE 696233 02-DEC-11
3 1 25 52428800 1 YES INACTIVE 696534 04-DEC-11
10 1 26 52428800 1 YES INACTIVE 749727 06-DEC-11

[oracle@liu liu]$ echo “my sql” > redo01.log
[oracle@liu liu]$
[oracle@liu liu]$

SQL> shutdown abort;————-直接 shutdown abort 模拟crash
ORACLE instance shut down.
SQL> startup
ORACLE instance started.

Total System Global Area 218103808 bytes
Fixed Size 1218580 bytes
Variable Size 75499500 bytes
Database Buffers 134217728 bytes
Redo Buffers 7168000 bytes
Database mounted.
ORA-00313: open failed for members of log group 1 of thread 1
ORA-00312: online log 1 thread 1: ‘/u01/app/oracle/oradata/liu/redo01.log’
ORA-27046: file size is not a multiple of logical block size
Additional information: 1

SQL> create pfile from spfile;

File created.

SQL> !

[oracle@liu dbs]$ vi initliu.ora

liu.__db_cache_size=134217728
liu.__java_pool_size=4194304
liu.__large_pool_size=4194304
liu.__shared_pool_size=67108864
liu.__streams_pool_size=0
*.audit_file_dest=’/u01/app/oracle/admin/liu/adump’
*.background_dump_dest=’/u01/app/oracle/admin/liu/bdump’
*.compatible=’10.2.0.1.0′
*.control_files=’/u01/app/oracle/oradata/liu/control01.ctl’,’/u01/app/oracle/oradata/liu/control02.ctl’,’/u01/app/oracle/oradata/liu/control03.ctl’
*.core_dump_dest=’/u01/app/oracle/admin/liu/cdump’
*.db_block_size=8192
*.db_domain=”
*.db_file_multiblock_read_count=16
*.db_name=’liu’
*.db_recovery_file_dest=’/u01/app/oracle/flash_recovery_area’
*.db_recovery_file_dest_size=2147483648
*.db_unique_name=’liu’
*.dispatchers='(PROTOCOL=TCP) (SERVICE=liuXDB)’
*.fal_client=’liu’
*.fal_server=’standby’
*.job_queue_processes=10
*.log_archive_config=’DG_CONFIG=(liu,standby)’
*.log_archive_dest_1=’location=/u01/arch/ valid_for=(ALL_LOGFILES,ALL_ROLES) db_unique_name=liu’
*.log_archive_dest_2=’service=standby lgwr async valid_for=(online_logfiles,primary_role) db_unique_name=standby’
*.log_archive_format=’%t_%s_%r.dbf’
*.open_cursors=300
*.pga_aggregate_target=71303168
*.processes=300
*.remote_login_passwordfile=’EXCLUSIVE’
*.sessions=335
*.sga_target=216006656
*.standby_file_management=’auto’
*.undo_management=’AUTO’
*.undo_tablespace=’UNDOTBS1′
*.user_dump_dest=’/u01/app/oracle/admin/liu/udump’
*._allow_resetlogs_corruption=TRUE
*._allow_error_simulation=TRUE

[oracle@liu dbs]$ sqlplus ‘/as sysdba’

SQL*Plus: Release 10.2.0.1.0 – Production on Tue Dec 6 23:57:41 2011

Copyright (c) 1982, 2005, Oracle. All rights reserved.

Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 – Production
With the Partitioning, OLAP and Data Mining options

SQL> shutdown abort;
ORACLE instance shut down.
SQL> create spfile from pfile;

File created.

SQL> startup nomount;
ORACLE instance started.

Total System Global Area 218103808 bytes
Fixed Size 1218580 bytes
Variable Size 75499500 bytes
Database Buffers 134217728 bytes
Redo Buffers 7168000 bytes
SQL> alter database mount;

Database altered.

SQL> show parameter all

NAME TYPE VALUE
———————————— ———– ——————————
_allow_error_simulation boolean TRUE
_allow_resetlogs_corruption boolean TRUE
fast_start_parallel_rollback string LOW
parallel_adaptive_multi_user boolean TRUE
parallel_automatic_tuning boolean FALSE
parallel_execution_message_size integer 2148
parallel_instance_group string
parallel_max_servers integer 40
parallel_min_percent integer 0
parallel_min_servers integer 0
parallel_server boolean FALSE

NAME TYPE VALUE
———————————— ———– ——————————
parallel_server_instances integer 1
parallel_threads_per_cpu integer 2
recovery_parallelism integer 0
SQL> recover database
ORA-00283: recovery session canceled due to errors
ORA-00313: open failed for members of log group 1 of thread 1
ORA-00312: online log 1 thread 1: ‘/u01/app/oracle/oradata/liu/redo01.log’
ORA-27046: file size is not a multiple of logical block size
Additional information: 1

SQL> recover database until cancel;
ORA-00279: change 782970 generated at 12/06/2011 22:05:16 needed for thread 1
ORA-00289: suggestion : /u01/arch/1_27_767998669.dbf
ORA-00280: change 782970 for thread 1 is in sequence #27

Specify log: {=suggested | filename | AUTO | CANCEL}
auto
ORA-00308: cannot open archived log ‘/u01/arch/1_27_767998669.dbf’
ORA-27037: unable to obtain file status
Linux Error: 2: No such file or directory
Additional information: 3

ORA-00308: cannot open archived log ‘/u01/arch/1_27_767998669.dbf’
ORA-27037: unable to obtain file status
Linux Error: 2: No such file or directory
Additional information: 3

ORA-01547: warning: RECOVER succeeded but OPEN RESETLOGS would get error below
ORA-01194: file 1 needs more recovery to be consistent
ORA-01110: data file 1: ‘/u01/app/oracle/oradata/liu/system01.dbf’

SQL>
SQL> alter database open resetlogs;
alter database open resetlogs
*
ERROR at line 1:
ORA-01092: ORACLE instance terminated. Disconnection forced

SQL> shutdown immediate;
ORA-24324: service handle not initialized
ORA-01041: internal error. hostdef extension doesn’t exist
SQL>
SQL>
SQL> startup;
ORA-24324: service handle not initialized
ORA-01041: internal error. hostdef extension doesn’t exist
SQL> exit
Disconnected from Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 – Production
With the Partitioning, OLAP and Data Mining options

[oracle@liu dbs]$ ps -ef |grep ora_
oracle 10245 10041 0 00:01 pts/3 00:00:00 grep ora_
[oracle@liu dbs]$ sqlplus ‘/as sysdba’

SQL*Plus: Release 10.2.0.1.0 – Production on Wed Dec 7 00:01:43 2011

Copyright (c) 1982, 2005, Oracle. All rights reserved.

Connected to an idle instance.

SQL> startup;
ORACLE instance started.

Total System Global Area 218103808 bytes
Fixed Size 1218580 bytes
Variable Size 75499500 bytes
Database Buffers 134217728 bytes
Redo Buffers 7168000 bytes
Database mounted.
Database opened.
SQL>
SQL>
SQL>
SQL> set line 200
SQL> select * from v$log;

GROUP# THREAD# SEQUENCE# BYTES MEMBERS ARC STATUS FIRST_CHANGE# FIRST_TIM
———- ———- ———- ———- ———- — —————- ————- ———
1 1 1 52428800 1 YES INACTIVE 782971 07-DEC-11
2 1 2 52428800 1 YES INACTIVE 782972 07-DEC-11
3 1 3 52428800 1 NO CURRENT 802977 07-DEC-11
10 1 0 52428800 1 YES UNUSED 0

SQL> alter system switch logfile;

System altered.

SQL> /

System altered.

SQL> /

System altered.

SQL> /

System altered.

SQL> select * from v$log;

GROUP# THREAD# SEQUENCE# BYTES MEMBERS ARC STATUS FIRST_CHANGE# FIRST_TIM
———- ———- ———- ———- ———- — —————- ————- ———
1 1 5 52428800 1 YES INACTIVE 803877 07-DEC-11
2 1 6 52428800 1 YES INACTIVE 803879 07-DEC-11
3 1 7 52428800 1 NO CURRENT 803882 07-DEC-11
10 1 4 52428800 1 YES INACTIVE 803875 07-DEC-11

SQL> alter system switch logfile;

System altered.

SQL> select * from v$log;

GROUP# THREAD# SEQUENCE# BYTES MEMBERS ARC STATUS FIRST_CHANGE# FIRST_TIM
———- ———- ———- ———- ———- — —————- ————- ———
1 1 5 52428800 1 YES INACTIVE 803877 07-DEC-11
2 1 6 52428800 1 YES INACTIVE 803879 07-DEC-11
3 1 7 52428800 1 YES INACTIVE 803882 07-DEC-11
10 1 8 52428800 1 NO CURRENT 803887 07-DEC-11

SQL> alter system switch logfile;

System altered.

SQL> select * from v$log;

GROUP# THREAD# SEQUENCE# BYTES MEMBERS ARC STATUS FIRST_CHANGE# FIRST_TIM
———- ———- ———- ———- ———- — —————- ————- ———
1 1 9 52428800 1 NO CURRENT 803893 07-DEC-11
2 1 6 52428800 1 YES INACTIVE 803879 07-DEC-11
3 1 7 52428800 1 YES INACTIVE 803882 07-DEC-11
10 1 8 52428800 1 YES INACTIVE 803887 07-DEC-11

SQL> shutdown immediate;
dDatabase closed.
Database dismounted.
ORACLE instance shut down.

SQL> startup;
ORACLE instance started.

Total System Global Area 218103808 bytes
Fixed Size 1218580 bytes
Variable Size 75499500 bytes
Database Buffers 134217728 bytes
Redo Buffers 7168000 bytes
Database mounted.
Database opened.
SQL> desc t;
ERROR:
ORA-04043: object t does not exist

看到虽然open了database 但是redo01.log 中的记录也将丢失