oracle-11g shell备份脚本

#!/bin/sh

#作者:long QQ:755136755

#按用户备份,并打包,本地保存

#复制oracle用户的环境变量

export ORACLE_BASE=/data/u01/app/oracle #定义ORACLE_BASE

export ORACLE_HOME=/data/u01/app/oracle/product/11.2.0/dbhome_1 #定义ORACLE_HOME

export ORACLE_SID=orcl #定义SID

export ORACLE_UNQNAME=$ORACLE_SID

export PATH=$ORACLE_HOME/bin:$PATH

export NLS_LANG="SIMPLIFIED CHINESE_CHINA.AL32UTF8"

#export NLS_LANG=SIMPLIFIED CHINESE_CHINA.ZHS16GBK

###############################

USERNAME=sys #启动备份的用户名,必须具有备份权限

PASSWORD=123456 #密码

OWNERS=(lhz longhz test) #要备份用户

CURDATE=$(date '+%F_h%Hm%Ms%S') #当前时间

backup_directory=/ora_bak #主备份目录

backup_directory_time=${backup_directory}/$(hostname)_${ORACLE_SID}_${CURDATE} #每次备份目录、打包目录

data_file_prefix=$(hostname)_${ORACLE_SID} #定义文件名前缀

BACKUP_MAIN_log=${backup_directory}/main_ora_${ORACLE_SID}_$(date '+%Y-%m').log #主要日志

ALL_PARA=$* #存放所有参数

[ -d ${backup_directory_time} ] || mkdir -p ${backup_directory_time} #文件不存在则创建备份文件夹

###############################

function echo_color () {

what=$*

if [[ "$1" =~ "ERROR" ]];then

echo -e "\033[31m ${what} \033[0m"

elif [[ "$1" =~ "WARN" ]];then

echo -e "\033[33m ${what} \033[0m"

else

echo -e "\033[32m ${what} \033[0m"

fi

}

null_exp(){

thek=`echo "'"${1}"'" | tr 'a-z' 'A-Z'` #用户转换为大写

echo_color "$(date '+%F_%T'), 查找用户:${thek}下空表,并生成操作语句, 请稍等..."

if [[ "${USERNAME}" == "sys" ]];then

CONN="conn ${USERNAME}/${PASSWORD} as sysdba ;"

else

CONN="conn ${USERNAME}/${PASSWORD} ;"

fi

alt_aly_sql=`

sqlplus -s /nolog <

${CONN}

set echo on;

set timing off;

set feedback on;

set trimspool off;

set linesize 120;

set pagesize 2000;

set newpage 1;

set heading on;

set term off;

set termout on;

set timing off;

select 'alter table '||OWNER||'.'||table_name||' allocate extent;' from dba_tables t where t.INITIAL_EXTENT is null and t.owner=${thek};

select 'analyze table '||OWNER||'.'||table_name||' compute statistics;' from dba_tables t where t.INITIAL_EXTENT is null and t.owner=${thek};

exit;

EOF`

alt_aly_sql=$(echo "${alt_aly_sql}" |sed -n '/^a/p')

echo "${alt_aly_sql}" >> ${backup_directory_time}/null_alt_aly_sql.log

echo_color "[alt]${alt_aly_sql}[alt]"

if [[ "${alt_aly_sql}" =~ "table" ]];then

echo_color "$(date '+%F_%T'), 开始操作、分析空表, 请稍等..."

opt_log=`

sqlplus -s /nolog <

${CONN}

set echo on;

set timing off;

set feedback on;

set trimspool off;

set linesize 120;

set pagesize 2000;

set newpage 1;

set heading on;

set term off;

set termout on;

set timing off;

${alt_aly_sql}

exit;

EOF`

else

echo_color "$(date '+%F_%T')无操作空表语句!"

fi

echo_color "[opt]${opt_log}[opt]"

echo "${opt_log}" >> ${backup_directory_time}/null_opt_log.log

}

main_fun(){

cou_suc=0 #备份成功数

cd ${backup_directory_time} #进入目录

echo "----${CURDATE}----" >> ${BACKUP_MAIN_log}

for thei in ${OWNERS[@]} #遍历用户数组

do

[[ "${ALL_PARA[@]}" =~ "n" ]] || null_exp ${thei} #查找每个用户的空表

echo_color "[INFO:]SID: ${ORACLE_SID}, 开始备份的用户:${thei}"

BACKUP_LOG=${data_file_prefix}_${thei}_${CURDATE}.log

BACKUP_FILE=${data_file_prefix}_${thei}_${CURDATE}.dmp

if [[ "${USERNAME}" == "sys" ]];then

exp \'${USERNAME}/${PASSWORD}@${ORACLE_SID} as sysdba \' file=${BACKUP_FILE} log=${BACKUP_LOG} owner=${thei} && let cou_suc+=1 #备份成功加1

else

exp \'${USERNAME}/${PASSWORD}@${ORACLE_SID} \' file=${BACKUP_FILE} log=${BACKUP_LOG} owner=${thei} && let cou_suc+=1 #备份成功加1

fi

awk -v f=${BACKUP_FILE} -v t=${CURDATE} -v u=${thei} \

'/^\. \. 正在导出表/{OFS="\t";print t,"USER:"u,$4,$5,$6;a=a+$5}END{OFS="\t";print t,"USER:"u,"本次总导出数:",""a,f}' ${BACKUP_LOG} >> ${BACKUP_MAIN_log}

biaoshu_hangshu[${cou_suc}]=`awk -v uu=${thei} '/^. . 正在导出表/{a+=1;b+=$5}END{print uu"(表数:"a", 行数:"b")"}' ${BACKUP_LOG}`

done

if [[ ${#OWNERS[@]} -eq ${cou_suc} ]];then #备份成功数等于用户数组数,则成功

echo_color "[INFO:]备份完成(用户:${OWNERS[@]}), 开始压缩文件!压缩列表如下: "

tar czvf ${backup_directory_time}.tar * && cd .. && rm -fr ${backup_directory_time} #压缩成功后删除源文件

echo_color "[INFO:]备份并压缩完成: ${backup_directory_time}.tar ,详细: ${biaoshu_hangshu[@]}"

echo_color "[INFO:]详细日志: ${BACKUP_MAIN_log} "

echo -e "----${biaoshu_hangshu[@]} 压缩完成: ${backup_directory_time}.tar \n" >> ${BACKUP_MAIN_log}

echo_color "tips:导入命令示例:imp \'sys/123456@SID as sysdba\' buffer=655360 file=文件.dmp fromuser=用户 touser=用户 log=/tmp/imp.log"

exit 0

else #备份失败

echo_color "[ERROR:]${CURDATE}, 备份失败!"

echo -e "${CURDATE}\t${OWNERS[@]}\t备份失败!" >> ${BACKUP_MAIN_log}

exit 3

fi

}

main_fun

你可能感兴趣的:(oracle-11g shell备份脚本)