巧用shell脚本生成快捷脚本(r2第12天)

时间:2022-05-04
本文章向大家介绍巧用shell脚本生成快捷脚本(r2第12天),主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

在升级的过程中,可能需要准备一些额外的脚本,比如说做数据迁移的时候为了考虑性能,需要做如下的额外工作: 1.将部分表置为nologging 2.将部分index置为nologging 3.将部分foreign key constraint置为disable 4.将部分trigger 置为disable 在完成数据升级后,再置为logging,enable状态。 但是在准备脚本的过程中,总是为这些小脚本而头疼,可能在升级前临时增加了一些表或者取消了部分表。或者有了其他的变更,维护这些脚本就显得有些体力工作了。 最后下决心改变这种状态,直接根据规则生成新的脚本。在不同的环境中脚本内容可能略有不同,但是功能不打折。 首先需要准备一个文件tablst,里面是文件的列表 比如: table1 table2 table3 ... 然后使用如下的脚本,就能生成完整的脚本,在升级前nologging,disable的工作就生成脚本到pre目录下,logging,enable的工作就生成脚本到post目录下 脚本内容也没有了冗余。



logging_flag=logging
nologging_flag=nologging
disable_flag=disable
enable_flag=enable
awk '{print "'''" $1 "'''" ","}' ../parfile/tablst |sed -e '/^$/d' -e '$s/.$//' > tablst.temp
table_list=`cat tablst.temp`


function pre_act
{
logging_ind=$1
enable_ind=$2
act_type=$3
     sqlplus -s $4 <<EOS
     set feedback off
     set pages 0
     set linesize 200
     spool $act_type/tab_$logging_ind.sql
     prompt -- table $logging_ind
     select 'alter table '||table_name||' $logging_ind;' from user_tables where  table_name in ($table_list);
     spool off;
     spool $act_type/index_$logging_ind.sql
     prompt -- index $logging_ind
     select 'alter index '||index_name||' $logging_ind;' from user_indexes where table_name in ($table_list);
     spool off;
     spool $act_type/fk_constraint_$disable_flag.sql
     prompt --FK constraint $enable_ind
     SELECT
           'ALTER TABLE '||TABLE_NAME||' $enable_ind  CONSTRAINT '|| CONSTRAINT_NAME||';' FROM USER_CONSTRAINTS WHERE
            CONSTRAINT_TYPE='R' UNION SELECT 'ALTER TABLE '||UCA.TABLE_NAME||' DISABLE  CONSTRAINT '|| UCA.CONSTRAINT_NAME||';'
     FROM
            USER_CONSTRAINTS UCA , 
            (SELECT  CONSTRAINT_NAME
               FROM  USER_CONSTRAINTS
               WHERE CONSTRAINT_TYPE IN ('P','U')
     ) tmp
     WHERE UCA.CONSTRAINT_TYPE = 'R' 
       AND tmp.constraint_name = UCA.R_CONSTRAINT_NAME
       and UCA.table_name in ($table_list) ;
     spool off;
     spool $act_type/trigger_disable.sql
     prompt trigger disable
     SELECT
           'ALTER TRIGGER ' ||TRIGGER_NAME||' $enable_ind ;'
     FROM
            USER_TRIGGERS;
     spool off;
EOS
}


pre_act  $nologging_flag $disable_flag pre $1
pre_act  $logging_flag   $enable_flag  post $1

脚本生成的sql脚本如下:
pre > 
total 83
-rw-r--r-- 1 xxxx dba 11280 Jun 23 21:00 fk_constraint_disable.sql
-rw-r--r-- 1 xxxx dba 42631 Jun 23 21:00 index_nologging.sql
-rw-r--r-- 1 xxxx dba 13888 Jun 23 21:00 tab_nologging.sql
-rw-r--r-- 1 xxxx dba   621 Jun 23 21:00 trigger_disable.sql

post > ls -lrt
total 69
-rw-r--r-- 1 xxxx dba 13886 Jun 23 21:00 tab_logging.sql
-rw-r--r-- 1 xxxx dba 42629 Jun 23 21:00 index_logging.sql
-rw-r--r-- 1 xxxx dba 11279 Jun 23 21:00 fk_constraint_disable.sql
-rw-r--r-- 1 xxxx dba   621 Jun 23 21:00 trigger_disable.sql