Shell script database derivative export specified format

Shell script database derivative export specified format

Function description
The requirements are as follows:
data in a specified format needs to be exported from the database, such as comma-separated or |-separated content.

export.sh

#!/usr/bin/bash

datefile=$1
work_path=$2

start_time=`date +%Y%m%d%H%M%S`
#work_path=/home/oracle/fk_data
DbCon=rdsadmin/rds@orcl
file_date=`date +%Y%m%d`
filename=${work_path}/"$datefile"_${file_date}.txt

sqlinfo="select * from $datefile;"
echo "执行的sql: $sqlinfo"

echo "----------------  start to exec job   $start_time "

echo "----------------  connect to $DbCon and try to exec sql..."
 
 
sqlplus -S $DbCon<< EndSql
set line 1000
set pagesize 0
set feedback off
set heading off
set trimspool on
set trims on
set echo off
set colsep '|'
--set termout off
spool $filename
$sqlinfo
spool off
exit
EndSql
#去除文件开头的空格及中间的空白字符
sed -i 's/ //g' ${work_path}/"$datefile"_${file_date}.txt
end_time=`date +%Y%m%d%H%M%S`
echo "----------------  cmd end , please check te result $end_time"

Logical description: Receive two parameters, the first parameter is the name of the table to be exported, the second parameter is the name of the exported data file, here the date is used as the identifier. The final generated file has a function to remove whitespace characters.

Note: The sqlinfo in the above script is like
select * from ts_maodou_data t;

MD5 encryption function

--MD5
create or replace function MD5(message in varchar2)
return varchar2
is
retval varchar2(32);

begin

       retval:=utl_raw.cast_to_raw(DBMS_OBFUSCATION_TOOLKIT.MD5(INPUT_STRING => message));
       
       return retval;  

end MD5;

Note: If the exported data requires md5 encryption, you need to create this function in the database.

Excuting an order

sh ./export.sh 表名 /home/oracle/maodou/oracledata

Other content

– Check the database character set encoding
select usenv('language') from dual;
– The server that executes the script specifies the character encoding
NLS_LANG="SIMPLIFIED CHINESE_CHINA.AL32UTF8"
– Make NLS_LANG effective
export NLS_LANG

Guess you like

Origin blog.csdn.net/weixin_38717886/article/details/115271232