1. 程式人生 > >shell指令碼操作oracle刪除表空間、建立表空間、刪除使用者

shell指令碼操作oracle刪除表空間、建立表空間、刪除使用者


#!/bin/sh
oraclehome=$ORACLE_HOME
echo $oraclehome
localdir="/oracle/data"
echo $localdir
#刪除已經存在的臨時dmp檔案
rm -rf $localdir/$2temp.dmp
rmresult=$?
echo "rm $2temp.dmp result:$rmresult"
#將使用者$1的表空間匯出
su - oracle -c "exp dba/dba file=$localdir/$2temp.dmp owner=$1"
expresult=$?
if [ "$expresult" != "0" ];then
        echo "exp $1 tablespace failure!!!"
fi
#先刪除使用者$2及其表空間,然後再新建該使用者及表空間
su - oracle -c "${ORACLE_HOME}/bin/sqlplus /nolog" <<EOF
connect / as sysdba
drop user $2 cascade;
drop tablespace $2 including contents and datafiles;
create tablespace $2 datafile '/oracle/product/10.2.0/oradata/$2.dbf' size 5M autoextend on;
create user $2 identified by "$2" default tablespace $2 temporary tablespace TEMP profile DEFAULT;
grant connect to $2;
grant resource to $2;
grant create any table to $2;
grant create any trigger to $2;
grant create any type to $2;
grant create any view to $2;
grant unlimited tablespace to $2;
exit
EOF

crdrresult=$?
if [ "$crdrresult" != "0" ];then
        echo "drop user and tablespace failure!!!"
        echo "create user and tablespace failure!!!"
else
#剛建完的使用者不能馬上使用,等候10秒
        sleep 10s
#更換dmp檔案中的表空間名
        sed -i 's/TABLESPACE "$1"/TABLESPACE "$2"/g' $localdir/$2temp.dmp
#使用imp命令匯出表空間資料到使用者$2的表空間
        su - oracle -c "imp dba/dba file=$localdir/$2temp.dmp  fromuser=$1 touser=$2"
        impresult=$?
        if [ "$impresult" != "0" ];then
                echo "imp failure!!!"
        else
                echo "imp success!!!"
        fi
fi