最近项目有一个需求,需要在多个数据库的schema上跑一些脚本。希望dba能够提供一个脚本,能够根据需求在环境中执行指定的脚本。
乍一听,没什么技术难点,为了更明白的说明问题,我举个例子。
有4个DB Instance: DB1,DB2,DB3,DB4
有6个DB Schemas, 5个table,分布如下:
db schemas tables
user1@DB1 table1, table2, table3, table4,table5
user2@DB2 table1, table2, table3, table4,table5
user3@DB3 table1, table3
user4@DB4 table1, table4
user5@DB1 table1, table5
user6@DB2 table2, table3, table4
实现的目标如下,对于同时含有table1--5的db schema才需要执行指定的脚本,脚本内容都是些dml操作。
目前的情况只能够得到db schema的列表,对于里面是否还有5个表,还没有细粒度的管理。
脚本需要从db schema的列表中筛选出符合的 db schema,然后执行脚本内容。
################# pl/sql执行情况 #############################
#!/bin/ksh
export ScriptName=`basename $0`
export ScriptDir=`dirname $0`
echo $ScriptName
echo $ScriptDir
rep_conn=$1
Env_Code=$2
sqlplus -s ${rep_conn} set serveroutput on
set feedback off
spool app_change_tmp.log
declare
conn_str varchar2(100);
target_conn varchar2(200);
tmp_cnts number;
cursor cur_conn_strs is
select distinct ''||username||'/'||password||'@'||db_instance||'' conn_str from xxxx --查询制定的配置表,从里面得到一个基本的db schema列表
group by username,password,db_instance ;
begin
for cur_conn_str in cur_conn_strs loop
dbms_output.put_line('conn '||cur_conn_str.conn_str);
dbms_output.put_line('set serveroutput on');
dbms_output.put_line('set feedback on');
dbms_output.put_line('set echo on');
dbms_output.put_line('declare');
dbms_output.put_line('tmp_cnt number;');
dbms_output.put_line('begin');
dbms_output.put_line('select count(*) into tmp_cnt from user_synonyms where synonym_name');
dbms_output.put_line(' in('||chr(39)||'T1'||chr(39)||','||chr(39)||'T2'||chr(39)||','||chr(39)||'T3'||chr(39)||','||chr(39)||'T4'||chr(39)||','||chr(39)||'T5'||chr(39)||');');
dbms_output.put_line('dbms_output.put_line(tmp_cnt);');
dbms_output.put_line('if(tmp_cnt>=5) then ');
dbms_output.put_line('dbms_output.put_line('||chr(39)||'app POST SCRIPTS RUNNING...'||chr(39)||');');
dbms_output.put_line('@script/script1.ps ');
dbms_output.put_line('@script/script2.ps ');
dbms_output.put_line('@script/script3.ps ');
dbms_output.put_line('dbms_output.put_line('||chr(39)||'app POST SCRIPTS RUNNING...'||chr(39)||');');
dbms_output.put_line('end if;');
dbms_output.put_line('end;');
dbms_output.put_line('/');
dbms_output.put_line(tmp_cnts);
end loop;
end;
/
spool off;
@app_change_tmp.log
EOS
如上的Pl/sql生成的动态pl/sql如下, 先判断是否还有T1--T5,如果条数符合,就执行脚本内容,但是有个限制就是执行脚本的时候如果脚本中有“set linesize... set define off之类的设置的话,脚本是运行不了的,对于ddl的执行也有一些限制。
################# 生成的动态 pl/sql 如下 #############################
conn user1/user1@DB1
set serveroutput on
set feedback on
set echo on
declare
tmp_cnt number;
begin
select count(*) into tmp_cnt from user_tables where table_name
in('T1','T2','T3','T4','T5');
dbms_output.put_line(tmp_cnt);
if(tmp_cnt>=5) then
dbms_output.put_line('app POST SCRIPTS RUNNING...');
@script/script1.ps
@script/script2.ps
@script/script3.ps
dbms_output.put_line('app POST SCRIPTS RUNNING...');
end if;
end;
/
conn user2/user2@DB1
set serveroutput on
set feedback on
set echo on
declare
tmp_cnt number;
begin
select count(*) into tmp_cnt from user_tables where table_name
in('T1','T2','T3','T4','T5');
dbms_output.put_line(tmp_cnt);
if(tmp_cnt>=5) then
dbms_output.put_line('app POST SCRIPTS RUNNING...');
@script/script1.ps
@script/script2.ps
@script/script3.ps
dbms_output.put_line('app POST SCRIPTS RUNNING...');
end if;
end;
/
################# pl/sql执行情况 #############################
############## shell 脚本实现动态shell ################################
echo 'app CHANGE START....'
cat $ScriptDir/script1.ps > $ScriptDir/app_all.ps
cat $ScriptDir/script2.ps >> $ScriptDir/app_all.ps
cat $ScriptDir/script3.ps>> $ScriptDir/app_all.ps
echo `sqlplus -s ${rep_conn} set feedback off pages 0
select distinct ''||username||'/'||password||'@'||db_instance||'' conn_str from xxxxxx --查询制定的配置表,从里面得到一个基本的db schema列表
group by username,password,db_instance ;
EOF`|awk '{for (i=1;i print "set feedback off pages 0";
print "select (case when (select count(*) from user_synonyms where synonym_name in ('\''T5'\'','\''T1'\'','\''T2'\'','\''T3'\'','\''T4'\''))>=5 then '\''Y @app_all.ps'\'' else '\''N no_need_to_run_app_script'\'' end) from dual; ";print "EOS` " $i}}'> $ScriptDir/dynamic_tmp.ksh
ksh $ScriptDir/dynamic_tmp.ksh |awk '{ if( $1 =="Y" ){ print "sqlplus -s " $3 " $ScriptDir/app_change_tmp.ksh
ksh $ScriptDir/app_change_tmp.ksh
rm $ScriptDir/dynamic_tmp.ksh
echo 'app CHANGE ENDED....'
rm $ScriptDir/app_change_tmp.ksh
#################生成的动态shell脚本1内容如下#################
echo `sqlplus -s user1/user1@DB1 set feedback off pages 0
select (case when (select count(*) from user_tables where table_name in in('T1','T2','T3','T4','T5');)>=5 then 'Y @adj_all.ps' else 'N no_need_to_run_adj_script' end) from dual;
EOS` user1/user1@DB1
echo `sqlplus -s user2/user2@DB2 set feedback off pages 0
select (case when (select count(*) from user_tables where table_name in in('T1','T2','T3','T4','T5');)>=5 then 'Y @adj_all.ps' else 'N no_need_to_run_adj_script' end) from dual;
EOS` user2/user2@DB2
#################执行动态shell脚本1后生成的脚本2内容如下#################
sqlplus -s user1/user1@DB1 @adj_all.ps
EOS
sqlplus -s user2/user2@DB2 @adj_all.ps
EOS
############## shell 脚本实现动态shell ################################