首页 > 数据库 >【Shell】Display the ddl for all users in Oracle DB with bash script

【Shell】Display the ddl for all users in Oracle DB with bash script

时间:2023-05-26 20:14:40浏览次数:44  
标签:Shell grep users script DB echo export ORACLE HOME

 

脚本说明:

1、普遍用于 使用expdp/impdp 数据泵进行的数据(全库或者特定schemas)迁移

2、适用于无PDB的Oracle环境

3、适用于RAC,SI,ADG 以及多实例的环境

 

使用方法:

创建脚本为 display_all_users_ddl.sh 然后将正文内容贴入 并保存,然后执行 bash display_all_users_ddl.sh , 

若环境为多实例,那么可以显示的数字或者实例名即可,最终在当前目录会生成四个.log结尾的文件

 

# ##################################################
# This script generate DDL for ALL USERS [Creation & Privileges]
# ##################################################
SCRIPT_NAME="display_all_users_ddl"
# ############
# Description:
# ############
echo
echo "====================================================="
echo "This script generates the CREATION STATEMENT for ALL USERS."
echo "================================================="
echo
sleep 1

# #######################################
# Excluded INSTANCES:
# #######################################
# Here you can mention the instances the script will IGNORE and will NOT run against:
# Use pipe "|" as a separator between each instance name.
# e.g. Excluding: -MGMTDB, ASM instances:

EXL_DB="\-MGMTDB|ASM" #Excluded INSTANCES [Will not get reported offline].

# ############################
# Listing Available Databases:
# ############################

# Count Instance Numbers:
INS_COUNT=$( ps -ef|grep pmon|grep -v grep|egrep -v ${EXL_DB}|wc -l )

# Exit if No DBs are running:
if [ $INS_COUNT -eq 0 ]
then
echo No Database Running !
exit
fi

# If there is ONLY one DB set it as default without prompt for selection:
if [ $INS_COUNT -eq 1 ]
then
export ORACLE_SID=$( ps -ef|grep pmon|grep -v grep|egrep -v ${EXL_DB}|awk '{print $NF}'|sed -e 's/ora_pmon_//g'|grep -v sed|grep -v "s///g" )

# If there is more than one DB ASK the user to select:
elif [ $INS_COUNT -gt 1 ]
then
echo
echo "Select the ORACLE_SID:[Enter the number]"
echo ———————
select DB_ID in $( ps -ef|grep pmon|grep -v grep|egrep -v ${EXL_DB}|awk '{print $NF}'|sed -e 's/ora_pmon_//g'|grep -v sed|grep -v "s///g" )
do
if [ -z "${REPLY##[0-9]*}" ]
then
export ORACLE_SID=$DB_ID
echo Selected Instance:
echo
echo "********"
echo $DB_ID
echo "********"
echo
break
else
export ORACLE_SID=${REPLY}
break
fi
done

fi
# Exit if the user selected a Non Listed Number:
if [ -z "${ORACLE_SID}" ]
then
echo "You've Entered An INVALID ORACLE_SID"
exit
fi

# #########################
# Getting ORACLE_HOME
# #########################
ORA_USER=`ps -ef|grep ${ORACLE_SID}|grep pmon|grep -v grep|egrep -v ${EXL_DB}|awk '{print $1}'|tail -1`
USR_ORA_HOME=`grep ${ORA_USER} /etc/passwd| cut -f6 -d ':'|tail -1`

# SETTING ORATAB:
if [ -f /etc/oratab ]
then
ORATAB=/etc/oratab
export ORATAB
## If OS is Solaris:
elif [ -f /var/opt/oracle/oratab ]
then
ORATAB=/var/opt/oracle/oratab
export ORATAB
fi

# ATTEMPT1: Get ORACLE_HOME using pwdx command:
PMON_PID=`pgrep -lf _pmon_${ORACLE_SID}|awk '{print $1}'`
export PMON_PID
ORACLE_HOME=`pwdx ${PMON_PID}|awk '{print $NF}'|sed -e 's/\/dbs//g'`
export ORACLE_HOME
#echo "ORACLE_HOME from PWDX is ${ORACLE_HOME}"

# ATTEMPT2: If ORACLE_HOME not found get it from oratab file:
if [ ! -f ${ORACLE_HOME}/bin/sqlplus ]
then
## If OS is Linux:
if [ -f /etc/oratab ]
then
ORATAB=/etc/oratab
ORACLE_HOME=`grep -v '^\#' $ORATAB | grep -v '^$'| grep -i "^${ORACLE_SID}:" | perl -lpe'$_ = reverse' | cut -f3 | perl -lpe'$_ = reverse' |cut -f2 -d':'`
export ORACLE_HOME

## If OS is Solaris:
elif [ -f /var/opt/oracle/oratab ]
then
ORATAB=/var/opt/oracle/oratab
ORACLE_HOME=`grep -v '^\#' $ORATAB | grep -v '^$'| grep -i "^${ORACLE_SID}:" | perl -lpe'$_ = reverse' | cut -f3 | perl -lpe'$_ = reverse' |cut -f2 -d':'`
export ORACLE_HOME
fi
#echo "ORACLE_HOME from oratab is ${ORACLE_HOME}"
fi

# ATTEMPT3: If ORACLE_HOME is still not found, search for the environment variable: [Less accurate]
if [ ! -f ${ORACLE_HOME}/bin/sqlplus ]
then
ORACLE_HOME=`env|grep -i ORACLE_HOME|sed -e 's/ORACLE_HOME=//g'`
export ORACLE_HOME
#echo "ORACLE_HOME from environment is ${ORACLE_HOME}"
fi

# ATTEMPT4: If ORACLE_HOME is not found in the environment search user's profile: [Less accurate]
if [ ! -f ${ORACLE_HOME}/bin/sqlplus ]
then
ORACLE_HOME=`grep -h 'ORACLE_HOME=\/' $USR_ORA_HOME/.bash_profile $USR_ORA_HOME/.*profile | perl -lpe'$_ = reverse' |cut -f1 -d'=' | perl -lpe'$_ = reverse'|tail -1`
export ORACLE_HOME
#echo "ORACLE_HOME from User Profile is ${ORACLE_HOME}"
fi

# ATTEMPT5: If ORACLE_HOME is still not found, search for orapipe: [Least accurate]
if [ ! -f ${ORACLE_HOME}/bin/sqlplus ]
then
ORACLE_HOME=`locate -i orapipe|head -1|sed -e 's/\/bin\/orapipe//g'`
export ORACLE_HOME
#echo "ORACLE_HOME from orapipe search is ${ORACLE_HOME}"
fi

# TERMINATE: If all above attempts failed to get ORACLE_HOME location, EXIT the script:
if [ ! -f ${ORACLE_HOME}/bin/sqlplus ]
then
echo "Please export ORACLE_HOME variable in your .bash_profile file under oracle user home directory in order to get this script to run properly"
echo "e.g."
echo "export ORACLE_HOME=/u01/app/oracle/product/11.2.0/db_1"
exit
fi



# #########################
# Getting DB_NAME:
# #########################
DB_NAME_RAW=$(${ORACLE_HOME}/bin/sqlplus -S "/ as sysdba" <<EOF
set pages 0 feedback off;
prompt
SELECT name from v\$database
exit;
EOF
)
# Getting DB_NAME in Uppercase & Lowercase:
DB_NAME=`echo ${DB_NAME_RAW}| perl -lpe'$_ = reverse' |awk '{print $1}'|perl -lpe'$_ = reverse'`

# ################################################
# SQLPLUS: Get the creation statement for ALL USERS:
# ###############################################
# Variables
export LOGDATE=`date +%d-%b-%y`
SPOOLLOF=${USR_ORA_HOME}/List_Of_Users_${DB_NAME}_${LOGDATE}.log
SPOOL_FILE=${USR_ORA_HOME}/ALL_USERS_DDL_${DB_NAME}_${LOGDATE}.log
#if [ -f ${SPOOL_FILE} ]
#then
echo "*****************" > ${SPOOL_FILE}
echo "ALL DB USERS DLL:" >> ${SPOOL_FILE}
echo "*****************" >> ${SPOOL_FILE}
echo "" >> ${SPOOL_FILE}
#fi

# Perpare the List of user to loop on:
${ORACLE_HOME}/bin/sqlplus -S '/ as sysdba' << EOF
PROMPT
spool ${SPOOLLOF}
set pages 0
set echo off heading off feedback off
select username from dba_users where username not in ('SYS','SYSTEM','DBSNMP','EXFSYS','MDSYS','ORDDATA','UNDOTBS1') order by 1;
spool off
EOF

# Loop on each user with generating its DDL:
for USERNAME in `cat ${SPOOLLOF}`
do
export USERNAME

${ORACLE_HOME}/bin/sqlplus -S '/ as sysdba' << EOF
spool ${SPOOL_FILE} APPEND
set termout off
set linesize 190
set pages 50000
set feedback off
set trim on
set echo off
col USERNAME for a30
col account_status for a23

PROMPT
PROMPT --- DDL for USER [$USERNAME] ---
PROMPT

select a.username,a.account_status,a.profile,q.tablespace_name,q.bytes/1024/104 USED_MB, q.max_bytes "MAX_QUOTA_Bytes" from dba_users a, dba_ts_quotas q where a.username=q.username and a.username='$USERNAME';
set pages 0
set echo off heading off feedback off
— Generate Creation Statement:
SELECT 'CREATE USER ' || u.username ||' IDENTIFIED ' ||' BY VALUES "' || c.password || "' DEFAULT TABLESPACE ' || u.default_tablespace ||' TEMPORARY TABLESPACE ' || u.temporary_tablespace ||' PROFILE ' || u.profile || case when account_status= 'OPEN' then ';' else ' Account LOCK;' end "–Creation Statement"
FROM dba_users u,user$ c where u.username=c.name and u.username=upper('$USERNAME')
UNION
— Generate Granted Roles:
select 'GRANT '||GRANTED_ROLE||' TO '||GRANTEE|| case when ADMIN_OPTION='YES' then ' WITH ADMIN OPTION;' else ';' end "Granted Roles"
from dba_role_privs where grantee= upper('$USERNAME')
UNION
— Generate System Privileges:
select 'GRANT '||PRIVILEGE||' TO '||GRANTEE|| case when ADMIN_OPTION='YES' then ' WITH ADMIN OPTION;' else ';' end "Granted System Privileges"
from dba_sys_privs where grantee= upper('$USERNAME')
UNION
— Generate Object Privileges:
select 'GRANT '||PRIVILEGE||' ON '||OWNER||'.'||TABLE_NAME||' TO '||GRANTEE||case when GRANTABLE='YES' then ' WITH GRANT OPTION;' else ';' end "Granted Object Privileges"
from DBA_TAB_PRIVS where GRANTEE=upper('$USERNAME');
spool off
EOF
done

echo ""
echo "[ALL USERS DDL SAVED TO: ${SPOOL_FILE}]"
echo ""

  

标签:Shell,grep,users,script,DB,echo,export,ORACLE,HOME
From: https://www.cnblogs.com/Jeona/p/17435697.html

相关文章

  • PowerShell命令,找到项目里行数最多的scala文件
    代码Get-ChildItem-Recurse-Filter*.scala|ForEach-Object{$_|Add-Member-NotePropertyName'Lines'-NotePropertyValue(Get-Content$_.FullName|Measure-Object-Line).Lines-PassThru}|Sort-ObjectLines-Descending|Select-Object-First5......
  • linux shell中 test 的用法
    1)判断表达式 iftest (表达式为真) iftest!表达式为假 test表达式1–a表达式2                 两个表达式都为真 test表达式1–o表达式2                两个表达式有一个为真 2)判断字符串 test–n字符串   ......
  • Javascript 指南:条件语句
    if/elseif/else语句是程序如何以编程方式处理是/否问题。如果第一个条件的计算结果为true,则程序将运行第一个代码块。否则,它将运行else块。让天气=“下雨”;如果(天气===“下雨”){控制台。log("今天别忘了带伞!");}否则{控制台。日志(“今天可能会很好”!);}输出:Don......
  • Javascript 指南:数组
    数组数组是JavaScript的有序列表,可以存储任何数据类型,包括字符串、数字和布尔值。数组中的每个项目都位于一个编号位置。句法数组由方括号和里面的内容表示。数组中的元素应该用逗号分隔。让colors=["red","blue","green","yellow"];访问和更新元素要访问或更改数组中......
  • 视频直播源码,JavaScript 下载文件、图片
    视频直播源码,JavaScript下载文件、图片一、下载文件 letdownLoadFile=(obj,name,suffix)=>{   consturl=window.URL.createObjectURL(newBlob([obj]));   constlink=document.createElement('a');   link.style.display='none';   l......
  • WebShell 特征分析
    WebShell特征分析作者:HaiCheng@助安社区,关注公众号领取学习路线和资料。WebShell是黑客经常使用的一种恶意脚本,其目的是获得服务器的执行操作权限,常见的webshell编写语言为asp/jsp/php。主要用于网站管理,服务器管理,权限管理等操作。使用方法简单,只需要上传一个代码文件,通过网址访......
  • JavaScript 格式化金额
    JavaScript格式化金额一、使用toLocaleString()要格式化金额,可以使用JavaScript的toLocaleString()方法。该方法可以将数字转换为本地化的字符串表示形式,并可以指定货币符号、小数点和千位分隔符等格式。代码如下:美元constamount=1234567.89;constformattedAmou......
  • javascript设计模式-享元
    这是一种优化性能代码的模式,最适合解决因创建大量类似对象而累及性能的问题。对于那些可能一连几天也不会重新加载的大型应用系统非常有用。它用于减少应用程序所需要数量,通过将对象内部划分为内在数据和外在数据两类来实现。管理享元外在数据有许多方法:1、数据库;2、组合模式(利用......
  • 【深度剖析】JavaScript中块级作用域与函数作用域
    前言系列首发于公众号『前端进阶圈』,若不想错过更多精彩内容,请“星标”一下,敬请关注公众号最新消息。面试官必问系列:深入理解JavaScript块和函数作用域在JavaScript中,究竟是什么会生成一个新的作用域,只有函数才会生成新的作用域吗?那JavaScript其他结构能生成新的作用域吗?3.1......
  • laytpl( Layui 的一款轻量 JavaScript 模板引擎)
    laytpl 是Layui的一款轻量JavaScript模板引擎,在字符解析上有着比较出色的表现。laytpl是一款颠覆性的JavaScript模板引擎文档说明一、模版语法输出一个普通字段,不转义html:{{d.field}}输出一个普通字段,并转义html:{{=d.field}}JavaScript脚本:{{#JavaScriptstate......