一键DG脚本(静默安装创建实例创建DG)

首先,这只是为了练习shell而写

大神们不屑请别看
不懂shell的可以看看,做个参考,把一些功能当做魔板

我知道红黑联盟,linux公社等会盗转,所以提前问候你们全家了

脚本大致步骤是:
1.判断是否创建oracle用户,如果没有创建就创建,并设置密码为oracle
2.解析config.conf文件,获取用户输入的SID等等参数
3.做安装前的参数配置,创建所需目录
4.安装数据库软件,配置监听,创建实例
5.配置pfile,设置force_logging,传输数据文件到备库(没用rman duplicate,没有为什么)
6.剩下就没啥了

其实最后写完发现所有的建库所需参数均可以自己在脚本中生成.dbt文件,但是我就不改了

#!/bin/bash
echo "#################################################"
echo "#                                               #"
echo "#                                               #"
echo "#                FaN‘s Script                   #"
echo "#                                               #"
echo "#                                               #"
echo "#################################################"

service iptables stop

SETPASSWD(){
#set password
expect -c "
           set timeout 2;
           spawn passwd oracle
           expect {
               password { send \"oracle\r\"; exp_continue }
               *assword* { send \"oracle\r\" }
           };
           "
}

#create user and group
cat /etc/passwd|awk -F‘:‘ ‘{print $1}‘|grep ‘oracle\>‘ > /dev/null
result=$?
if [ "$result" == "1" ];then
    echo "user oracle does not exist,Starting to create user!"
    /usr/sbin/groupadd oinstall > /dev/null
    /usr/sbin/groupadd dba > /dev/null
    /usr/sbin/useradd -g oinstall -G dba -d /home/oracle oracle
    cat /etc/passwd|awk -F‘:‘ ‘{print $1}‘|grep ‘oracle\>‘ > /dev/null
    result1=$?
    if [ "$result1" == "0" ];then
        echo "user oracle has been created"
    else
        echo "we has a few problems during creating the user,please check!"
    fi
#set passwd
    SETPASSWD >> /dev/null
    echo " The user oracle‘s password is set to oracle "
#chown chmod for /oracle
    chown oracle:oinstall /oracle
    chmod 755 /oracle
    chown oracle:oinstall /oracle/711
    chmod 755 /oracle/711
elif [ "$result" == "0" ];then
    echo "user oracle have already been created"
fi

#Parsing the config file
errParam() {
  echo "[ERROR] \`$1‘ is empty, check configfile ‘$configfile‘"
  echo ""
  exit
}

SOFTWARE_HOME=
ORACLE_SID=
INVENTORY_LOCATION=
ORACLE_BASE=
ORACLE_HOME=
DB_NAME=

configfile=/oracle/711/config.conf
if [ -f $configfile ]; then
  param=$(cat $configfile | tr -d "\015" | grep -v "^#" | grep -v "^$")
else
  echo "[ERROR] $configfile not found!"
  exit
fi

while read i
do
   key=$(echo "$i" | awk -F‘=‘ ‘{print $1}‘)
   value=$(echo "$i" | awk -F‘=‘ ‘{print $2}‘)
   [ "$key" == "SOFTWARE_HOME" ] && SOFTWARE_HOME=$value
   [ "$key" == "ORACLE_SID" ] && ORACLE_SID=$value
   [ "$key" == "INVENTORY_LOCATION" ] && INVENTORY_LOCATION=$value
   [ "$key" == "ORACLE_HOME" ] && ORACLE_HOME=$value
   [ "$key" == "ORACLE_BASE" ] && ORACLE_BASE=$value
   [ "$key" == "DB_NAME" ] && DB_NAME=$value
   [ "$key" == "ARCH" ] && ARCH=$value
   [ "$key" == "P_DB" ] && P_DB=$value
   [ "$key" == "P_IP" ] && P_IP=$value
   [ "$key" == "S_DB" ] && S_DB=$value
   [ "$key" == "S_IP" ] && S_IP=$value
   [ "$key" == "P_B_UNIQUE_NAME" ] && P_B_UNIQUE_NAME=$value
   [ "$key" == "S_B_UNIQUE_NAME" ] && S_B_UNIQUE_NAME=$value
   [ "$key" == "ctl" ] && ctl=$value
done <<< "$param"

[ "x$SOFTWARE_HOME" == "x" ] && errParam SOFTWARE_HOME
[ "x$ORACLE_SID" == "x" ] && ORACLE_SID=orcl
[ "x$INVENTORY_LOCATION" == "x" ] && INVENTORY_LOCATION=/oracle/Inventory
[ "x$ORACLE_BASE" == "x" ] && ORACLE_BASE=/oracle
[ "x$ORACLE_HOME" == "x" ] && ORACLE_HOME=/oracle/db11g
[ "x$DB_NAME" == "x" ] && DB_NAME=orcl
[ "x$ARCH" == "x" ] && ARCH=/oracle/arch
[ "x$P_B_UNIQUE_NAME" == "x" ] && P_B_UNIQUE_NAME=primary
[ "x$S_B_UNIQUE_NAME" == "x" ] && S_B_UNIQUE_NAME=standby

echo
echo "#################################################"
echo "SOFTWARE_HOME is $SOFTWARE_HOME"
echo "ORACLE_SID is $ORACLE_SID"
echo "INVENTORY_LOCATION is $INVENTORY_LOCATION"
echo "ORACLE_BASE is $ORACLE_BASE"
echo "ORACLE_HOME is $ORACLE_HOME"
echo "DB_NAME is $DB_NAME"
echo "Archivelogs are in $ARCH"
#echo "Controlfiles are in $ctl"
echo "#################################################"

#cont="&quot;$ctl&quot;"

#调用slient_setup.sh为备库安装数据库软件
echo "starting install oracle software on standby"
expect -c "
           spawn ssh $S_IP
           expect {
               yes/no { send \"yes\r\"; exp_continue }
               *assword* { send \"oracle\r\" }
           } ;
           expect ]# { send \"cd /oracle/711 \r\" };
           expect ]# { send \"sh /oracle/711/slient_setup.sh \r\" };
#          send \r;
           expect eof ;
       "

set_host(){
cat >> /etc/hosts << EOF
$P_IP	$P_DB
$S_IP	$S_DB
EOF
}

set_host

# Set up the shell variables:
echo "EDITOR=vi" >> /home/oracle/.bash_profile
echo "export EDITOR" >> /home/oracle/.bash_profile
echo "umask 022" >> /home/oracle/.bash_profile
echo "export ORACLE_SID=$ORACLE_SID" >> /home/oracle/.bash_profile
echo "export ORACLE_BASE=$ORACLE_BASE" >> /home/oracle/.bash_profile
echo "export ORACLE_HOME=$ORACLE_HOME" >> /home/oracle/.bash_profile
echo "export PATH=$ORACLE_HOME/bin:$GRID_HOME/bin:$PATH" >> /home/oracle/.bash_profile
echo "export LD_LIBRARY_PATH=$ORACLE_HOME/lib:$ORACLE_HOME/rdbms/lib:/lib:/usr/lib" >> /home/oracle/.bash_profile
echo "export CLASSPATH=$ORACLE_HOME/JRE:$ORACLE_HOME/jlib:$ORACLE_HOME/rdbms/jlib:$ORACLE_HOME/network/jlib" >> /home/oracle/.bash_profile
echo "stty erase ^h" >> /home/oracle/.bash_profile

#preinstall
echo "#add for database" >> /etc/sysctl.conf
echo "fs.file-max = 6815744" >> /etc/sysctl.conf
echo "net.ipv4.ip_local_port_range = 9000 65500" >> /etc/sysctl.conf
echo "net.core.rmem_max=262144" >> /etc/sysctl.conf
echo "net.core.rmem_max = 4194304" >> /etc/sysctl.conf
echo "net.core.wmem_max=262144" >> /etc/sysctl.conf
echo "net.core.wmem_max = 1048576" >> /etc/sysctl.conf
echo "fs.aio-max-nr = 1048576" >> /etc/sysctl.conf
echo "kernel.shmmni = 4096" >> /etc/sysctl.conf
echo "kernel.shmmax = 957691904" >> /etc/sysctl.conf
echo "kernel.shmall = 2097152" >> /etc/sysctl.conf
echo "kernel.sem = 250 32000 100 128" >> /etc/sysctl.conf
echo "net.core.rmem_default = 262144" >> /etc/sysctl.conf
echo "net.core.wmem_default = 262144" >> /etc/sysctl.conf

sysctl -p >> /dev/null

echo "oracle soft nproc 2047" >> /etc/security/limits.conf
echo "oracle hard nproc 16384" >> /etc/security/limits.conf
echo "oracle soft nofile 1024" >> /etc/security/limits.conf
echo "oracle hard nofile 65536" >> /etc/security/limits.conf

echo "session    required    pam_limits.so" >>/etc/pam.d/login

echo "if [ $USER = "oracle" ] || [ $USER = "grid" ]; then" >> /etc/profile
echo "if [ $SHELL = "/bin/ksh" ]; then" >> /etc/profile
echo "ulimit -p 16384" >> /etc/profile
echo "ulimit -n 65536" >> /etc/profile
echo "else" >> /etc/profile
echo "ulimit -u 16384 -n 65536" >> /etc/profile
echo "fi" >> /etc/profile
echo "umask 022" >> /etc/profile
echo "fi" >> /etc/profile

#make db_install.rsp
mk_db_install(){
    echo "oracle.install.responseFileVersion=/oracle/install/rspfmt_dbinstall_response_schema_v11_2_0" >> db_install.rsp
    echo "oracle.install.option=INSTALL_DB_SWONLY" >> db_install.rsp
    echo "ORACLE_HOSTNAME=$HOSTNAME" >> db_install.rsp
    echo "UNIX_GROUP_NAME=oinstall" >> db_install.rsp
    echo "INVENTORY_LOCATION=$INVENTORY_LOCATION" >> db_install.rsp
    echo "SELECTED_LANGUAGES=zh_CN,en" >> db_install.rsp
    echo "ORACLE_HOME=$ORACLE_HOME" >> db_install.rsp
    echo "ORACLE_BASE=$ORACLE_BASE" >> db_install.rsp
    echo "oracle.install.db.InstallEdition=EE" >> db_install.rsp
    echo "oracle.install.db.EEOptionsSelection=false" >> db_install.rsp
    echo "oracle.install.db.optionalComponents=" >> db_install.rsp
    echo "oracle.install.db.DBA_GROUP=dba" >> db_install.rsp
    echo "oracle.install.db.OPER_GROUP=oinstall" >> db_install.rsp
    echo "oracle.install.db.CLUSTER_NODES=" >> db_install.rsp
    echo "oracle.install.db.isRACOneInstall=false" >> db_install.rsp
    echo "oracle.install.db.racOneServiceName=" >> db_install.rsp
    echo "oracle.install.db.config.starterdb.type=GENERAL_PURPOSE" >> db_install.rsp
    echo "oracle.install.db.config.starterdb.globalDBName=" >> db_install.rsp
    echo "oracle.install.db.config.starterdb.SID=" >> db_install.rsp
    echo "oracle.install.db.config.starterdb.characterSet=" >> db_install.rsp
    echo "oracle.install.db.config.starterdb.memoryOption=false" >> db_install.rsp
    echo "oracle.install.db.config.starterdb.memoryLimit=" >> db_install.rsp
    echo "oracle.install.db.config.starterdb.installExampleSchemas=false" >> db_install.rsp
    echo "oracle.install.db.config.starterdb.enableSecuritySettings=true" >> db_install.rsp
    echo "oracle.install.db.config.starterdb.password.ALL=" >> db_install.rsp
    echo "oracle.install.db.config.starterdb.password.SYS=" >> db_install.rsp
    echo "oracle.install.db.config.starterdb.password.SYSTEM=" >> db_install.rsp
    echo "oracle.install.db.config.starterdb.password.SYSMAN=" >> db_install.rsp
    echo "oracle.install.db.config.starterdb.password.DBSNMP=" >> db_install.rsp
    echo "oracle.install.db.config.starterdb.control=DB_CONTROL" >> db_install.rsp
    echo "oracle.install.db.config.starterdb.gridcontrol.gridControlServiceURL=" >> db_install.rsp
    echo "oracle.install.db.config.starterdb.automatedBackup.enable=false" >> db_install.rsp
    echo "oracle.install.db.config.starterdb.automatedBackup.osuid=" >> db_install.rsp
    echo "oracle.install.db.config.starterdb.automatedBackup.ospwd=" >> db_install.rsp
    echo "oracle.install.db.config.starterdb.storageType=" >> db_install.rsp
    echo "oracle.install.db.config.starterdb.fileSystemStorage.dataLocation=" >> db_install.rsp
    echo "oracle.install.db.config.starterdb.fileSystemStorage.recoveryLocation=" >> db_install.rsp
    echo "oracle.install.db.config.asm.diskGroup=" >> db_install.rsp
    echo "oracle.install.db.config.asm.ASMSNMPPassword=" >> db_install.rsp
    echo "MYORACLESUPPORT_USERNAME=" >> db_install.rsp
    echo "MYORACLESUPPORT_PASSWORD=" >> db_install.rsp
    echo "SECURITY_UPDATES_VIA_MYORACLESUPPORT=false" >> db_install.rsp
    echo "DECLINE_SECURITY_UPDATES=true" >> db_install.rsp
    echo "PROXY_HOST=" >> db_install.rsp
    echo "PROXY_PORT=" >> db_install.rsp
    echo "PROXY_USER=" >> db_install.rsp
    echo "PROXY_PWD=" >> db_install.rsp
    echo "PROXY_REALM=" >> db_install.rsp
    echo "COLLECTOR_SUPPORTHUB_URL=" >> db_install.rsp
    echo "oracle.installer.autoupdates.option=SKIP_UPDATES" >> db_install.rsp
    echo "oracle.installer.autoupdates.downloadUpdatesLoc=" >> db_install.rsp
    echo "AUTOUPDATES_MYORACLESUPPORT_USERNAME=" >> db_install.rsp
    echo "AUTOUPDATES_MYORACLESUPPORT_PASSWORD=" >> db_install.rsp
}

#mk db_install.rsp
mk_db_install
echo
echo "db_install.rsp has been created"
echo
#chown chmod db_install.rsp
chown oracle:oinstall db_install.rsp
chmod 755 db_install.rsp

#mkdir ORACLE_HOME ORACLE_BASE INVENTORY_LOCATION
if [ ! -d "$ORACLE_BASE" ];then
    mkdir -p $ORACLE_BASE
fi

if [ ! -d "$ORACLE_HOME" ];then
    mkdir -p $ORACLE_HOME
fi

if [ ! -d "$INVENTORY_LOCATION" ];then
    mkdir -p $INVENTORY_LOCATION
fi

if [ ! -d "$ARCH" ];then
    mkdir -p $ARCH
fi

chown -R oracle:oinstall $ORACLE_BASE
chown -R oracle:oinstall $ORACLE_HOME
chown -R oracle:oinstall $ARCH
chown -R oracle:oinstall $INVENTORY_LOCATION

#install oracle software
setupDatabase() {
  runStr="
  cd $SOFTWARE_HOME
  nohup ./runInstaller -silent -force -responseFile /oracle/711/db_install.rsp >> /oracle/711/setupDatabase.out 2>&1 &
  "
  su - oracle -c "$runStr"
  while true
  do
     echo -n "."
     sleep 3s
     grep "Successfully Setup Software" /oracle/711/setupDatabase.out >> /dev/null
     if [ $? -eq 0 ]; then
       sh ${INVENTORY_LOCATION}/orainstRoot.sh
       sh ${ORACLE_HOME}/root.sh
       break
     fi
   done
}

sleep 1s
echo "steup oracle database..."
setupDatabase
echo "steup succsed."
echo

listenrsp(){
cat >> /oracle/711/listen.rsp << EOF
[GENERAL]
RESPONSEFILE_VERSION="11.2"
CREATE_TYPE="CUSTOM"
[oracle.net.ca]
INSTALLED_COMPONENTS={"server","net8","javavm"}
INSTALL_TYPE=""typical""
LISTENER_NUMBER=1
LISTENER_NAMES={"LISTENER"}
LISTENER_PROTOCOLS={"TCP;1521"}
LISTENER_START=""LISTENER""
NAMING_METHODS={"TNSNAMES","ONAMES","HOSTNAME"}
NSN_NUMBER=1
NSN_NAMES={"EXTPROC_CONNECTION_DATA"}
NSN_SERVICE={"PLSExtProc"}
NSN_PROTOCOLS={"TCP;HOSTNAME;1521"}
EOF
}

listenrsp
chown oracle:oinstall /oracle/711/listen.rsp
chmod 755 /oracle/711/listen.rsp

netcac(){
    cmd="
    nohup netca -silent -responseFile /oracle/711/listen.rsp >> /oracle/711/netca.log 2>&1 &
    "
    su - oracle -c "$cmd"
    while true
    do
        echo -n "."
        sleep 1s
        grep -E ‘The exit code is 0|退出代码是0‘ /oracle/711/netca.log >> /dev/null
        if [ $? -eq 0 ]; then
            echo "Listener Successfully Created"
            break
        fi
    done
}

sleep 1s
echo "starting create listener..."
netcac
echo "Create Successfully"
echo

#不想创建实力只执行以上的内容就可以了

mkdbcarsp(){
cat >> /oracle/711/dbca.rsp << EOF
[GENERAL]
RESPONSEFILE_VERSION = "11.2.0"
OPERATION_TYPE = "createDatabase"
[CREATEDATABASE]
GDBNAME = "$DB_NAME"
SID = "$ORACLE_SID"
TEMPLATENAME = "orcl.dbt"
[createTemplateFromDB]
SOURCEDB = "myhost:1521:orcl"
SYSDBAUSERNAME = "system"
TEMPLATENAME = "My Copy TEMPLATE"
[createCloneTemplate]
SOURCEDB = "orcl"
TEMPLATENAME = "My Clone TEMPLATE"
[DELETEDATABASE]
SOURCEDB = "orcl"
[generateScripts]
TEMPLATENAME = "New Database"
GDBNAME = "orcl11.us.oracle.com"
[CONFIGUREDATABASE]
[ADDINSTANCE]
DB_UNIQUE_NAME = "orcl11g.us.oracle.com"
NODELIST=
SYSDBAUSERNAME = "sys"
[DELETEINSTANCE]
DB_UNIQUE_NAME = "orcl11g.us.oracle.com"
INSTANCENAME = "orcl11g"
SYSDBAUSERNAME = "sys"
EOF
}

mkdbcarsp

val=$(cat /oracle/711/orcl7.dbt |grep "log_archive_dest_1"|awk -F‘=‘ ‘{print $4}‘|awk -F"‘" ‘{print $1}‘)
cp -p /oracle/711/orcl7.dbt ${ORACLE_HOME}/assistants/dbca/templates/orcl.dbt
eval sed -i ‘s#$val#$ARCH#‘ ${ORACLE_HOME}/assistants/dbca/templates/orcl.dbt

create_db(){
    cmd1="
    nohup dbca -silent -createDatabase -responseFile /oracle/711/dbca.rsp  -sysPassword "oracle" -systemPassword "oracle" >> /oracle/711/dbca.log 2>&1 &
    "
    su - oracle -c "$cmd1"
    while true
    do
        echo -n "."
        sleep 1s
        grep  "100%" /oracle/711/dbca.log >> /dev/null
        if [ $? -eq 0 ]; then
            echo "instance create Successfully"
            break
        fi
    done
}

sleep 1s
echo "starting create instance..."
create_db
echo
echo "Instance Create Successfully"

###在这之上的是安装数据库软件并创建实例

sleep 3s
#force logging
#!/bin/bash
crpfile(){
su - oracle -c "sqlplus -S / as sysdba << !
set trimspool on feedback off  pagesize 0 heading off
create pfile=‘/home/oracle/initorcl.ora‘ from spfile;
exit;
!
"
}

crpfile >> /oracle/711/dg.log 2>&1

ad=$(cat /home/oracle/initorcl.ora |grep "log_archive_dest_1=‘LOCATION")
af=$(cat /home/oracle/initorcl.ora |grep "log_archive_format")
eval sed -i ‘s#$ad##‘ /home/oracle/initorcl.ora
eval sed -i ‘s#$af##‘ /home/oracle/initorcl.ora

cp -p /home/oracle/initorcl.ora /home/oracle/initstandby.ora
stc=$(cat /home/oracle/initstandby.ora | grep "control_files")
audit=$(cat /home/oracle/initstandby.ora | grep "audit_file_dest")
eval sed -i ‘s#$stc##‘ /home/oracle/initstandby.ora
eval sed -i ‘s#$audit##‘ /home/oracle/initstandby.ora

#追加如下内容

insersp(){
cat >> /home/oracle/initorcl.ora << EOF

DB_UNIQUE_NAME=$P_B_UNIQUE_NAME
LOG_ARCHIVE_CONFIG=‘DG_CONFIG=($P_B_UNIQUE_NAME,$S_B_UNIQUE_NAME)‘
LOG_ARCHIVE_DEST_1=
 ‘LOCATION=$ARCH
  VALID_FOR=(ALL_LOGFILES,ALL_ROLES)
  DB_UNIQUE_NAME=$P_B_UNIQUE_NAME‘
LOG_ARCHIVE_DEST_2=
 ‘SERVICE=$S_B_UNIQUE_NAME SYNC  AFFIRM NET_TIMEOUT=30
  VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE)
  DB_UNIQUE_NAME=$S_B_UNIQUE_NAME‘
LOG_ARCHIVE_DEST_STATE_1=ENABLE
LOG_ARCHIVE_DEST_STATE_2=ENABLE
REMOTE_LOGIN_PASSWORDFILE=EXCLUSIVE
LOG_ARCHIVE_FORMAT=%t_%s_%r.arc
LOG_ARCHIVE_MAX_PROCESSES=3

FAL_SERVER=standby
DB_FILE_NAME_CONVERT=‘$ORACLE_BASE/oradata/standby‘,‘$ORACLE_BASE/oradata/$ORACLE_SID‘
LOG_FILE_NAME_CONVERT=
 ‘$ORACLE_BASE/oradata/standby‘,‘$ORACLE_BASE/oradata/$ORACLE_SID‘
STANDBY_FILE_MANAGEMENT=AUTO
EOF
}

insersp >> /oracle/711/dg.log 2>&1

cstdpfile(){
cat >> /home/oracle/initstandby.ora << EOF
control_files=‘$ORACLE_BASE/oradata/standby/std_control01.ctl‘
audit_file_dest=‘/oracle/admin/standby/adump‘
DB_UNIQUE_NAME=$S_B_UNIQUE_NAME
LOG_ARCHIVE_CONFIG=‘DG_CONFIG=($P_B_UNIQUE_NAME,$S_B_UNIQUE_NAME)‘
LOG_ARCHIVE_DEST_1=
 ‘LOCATION=$ARCH
  VALID_FOR=(ALL_LOGFILES,ALL_ROLES)
  DB_UNIQUE_NAME=$S_B_UNIQUE_NAME‘
LOG_ARCHIVE_DEST_2=
 ‘SERVICE=$P_B_UNIQUE_NAME SYNC  AFFIRM NET_TIMEOUT=30
  VALID_FOR=(ONLINE_LOGFILES,PRIMARY_ROLE)
  DB_UNIQUE_NAME=$P_B_UNIQUE_NAME‘
LOG_ARCHIVE_DEST_STATE_1=ENABLE
LOG_ARCHIVE_DEST_STATE_2=ENABLE
REMOTE_LOGIN_PASSWORDFILE=EXCLUSIVE
LOG_ARCHIVE_FORMAT=%t_%s_%r.arc
LOG_ARCHIVE_MAX_PROCESSES=3

FAL_SERVER=standby
DB_FILE_NAME_CONVERT=‘$ORACLE_BASE/oradata/$ORACLE_SID‘,‘$ORACLE_BASE/oradata/standby‘
LOG_FILE_NAME_CONVERT=
 ‘$ORACLE_BASE/oradata/$ORACLE_SID‘,‘$ORACLE_BASE/oradata/standby‘
STANDBY_FILE_MANAGEMENT=AUTO
EOF
}

cstdpfile >> /oracle/711/dg.log 2>&1

su - oracle -c "sqlplus -S / as sysdba << !
set trimspool on feedback off  pagesize 0 heading off
shutdown immediate;
startup pfile=‘/home/oracle/initorcl.ora‘;
shutdown immediate;
create spfile from pfile=‘/home/oracle/initorcl.ora‘;
startup mount;
exit;
!
"

#########现在是mount

shutoracle(){
Shut_Oracle_Down=$(
su - oracle -c "sqlplus -S / as sysdba << !
set trimspool on feedback off pagesize 0 heading off
shutdown immediate;
exit;
!
"
)
echo $Shut_Oracle_Down | grep "ORACLE instance shut down"
if [ $? -eq 0 ];then
        echo "Instacne Shutdown Successfully!"
else
        echo "Instacne still open, please shutdown manual(you have two minute to shutdown the database!)"
        sleep 120s
fi
}

mountoracle(){
Mount_Oracle=$(
su - oracle -c "sqlplus -S / as sysdba << !
set trimspool on feedback off pagesize 0 heading off
startup mount;
exit;
!
"
)
echo $Mount_Oracle | grep "Database mounted"
if [ $? -eq 0 ];then
        echo "Database Mounted Successfully!"
fi
}   

alterforce(){
alterlogging=$(
su - oracle -c "sqlplus -S / as sysdba << !
set trimspool on feedback off  pagesize 0 heading off
alter database force logging;
exit;
!
"
)
}

chklogging(){
chk=$(
su - oracle -c "sqlplus -S / as sysdba << !
set trimspool on feedback off  pagesize 0 heading off
select force_logging from v\\\$database;
exit;
!
"
)
if [ "$chk" == "NO" ];then
    echo "force logging is NO"
elif [ "$chk" == "YES" ]; then
    echo "force logging is enabled"
fi
}

chkmount(){
mountstat=$(
su - oracle -c "sqlplus -S / as sysdba << !
set trimspool on feedback off  pagesize 0 heading off
select status from v\\\$instance;
exit;
!
"
)
}

####开始查看是否启用force logging

echo "check force logging"
force_logging=$(
su - oracle -c "sqlplus -S / as sysdba << !
set trimspool on feedback off  pagesize 0 heading off
select force_logging from v\\\$database;
exit;
!
"
)

if [ "$force_logging" == "NO" ];then
    echo "force logging is NO"
    echo "startng alter database force logging"
    chkmount
    if [ "mountstat" == "MOUNTED" ];then
        #shutoracle
        alterforce
        chklogging
    else
        shutoracle
        mountoracle
        alterforce
        chklogging
    fi
elif [ "$force_logging" == "YES" ];then
    echo "force logging is enabled"
fi

#创建standby controlfile
csc(){
su - oracle -c "sqlplus -S / as sysdba << !
set trimspool on feedback off  pagesize 0 heading off
ALTER DATABASE CREATE STANDBY CONTROLFILE AS ‘/home/oracle/std_control01.ctl‘;
exit;
!
"
}

findalert(){
alertpath=$(
su - oracle -c "sqlplus -S / as sysdba << !
set trimspool on feedback off  pagesize 0 heading off
select value from v\\\$parameter where name=‘background_dump_dest‘;
exit;
!
"
)
}

if [ "$mountstat" == "MOUNTED" ];then
    csc
    if [ -f /home/oracle/std_control01.ctl ];then
        echo "Standby Controlfile Create Successfully"
    else
        echo "Faild To Create Standby Controlfile"
    fi
else
    shutoracle
    echo $Shut_Oracle_Down | grep "ORACLE instance shut down"
    if [ $? -eq 0 ];then
        mountoracle
        echo $Mount_Oracle | grep "Database mounted"
        if [ $? -eq 0 ];then
            csc
            if [ -f /home/oracle/std_control01.ctl ];then
                echo "Standby Controlfile Create Successfully"
            else
                echo "Faild To Create Standby Controlfile"
                exit -1
            fi
        fi
    fi
fi

sendpfile(){
expect -c "
           spawn scp /home/oracle/initstandby.ora [email protected]$S_IP:$ORACLE_HOME/dbs/initstandby.ora
           expect {
               yes/no { send \"yes\r\"; exp_continue }
               *assword* { send \"oracle\r\" }
           };
           expect ]  { send exit\r } ;
           expect 100% ;
           expect eof ;
       "
}

mkd_1(){
expect -c "
           spawn ssh [email protected]$S_IP
           expect {
               yes/no { send \"yes\r\"; exp_continue }
               *assword* { send \"oracle\r\" }
           } ;
           expect ] { send \"mkdir -p /oracle/oradata/standby \r\" };
           expect eof ;
       "
}

mkd_2(){
expect -c "
           spawn ssh [email protected]$S_IP
           expect {
               yes/no { send \"yes\r\"; exp_continue }
               *assword* { send \"oracle\r\" }
           } ;
           expect ] { send \"mkdir -p /oracle/admin/standby/adump \r\" };
           expect eof ;
       "
}

sendstdc(){
expect -c "
           spawn scp /home/oracle/std_control01.ctl [email protected]$S_IP:$ORACLE_BASE/oradata/standby/std_control01.ctl
           expect {
               yes/no { send \"yes\r\"; exp_continue }
               *assword* { send \"oracle\r\" }
           };
           expect 100%
           expect eof ;
       "
}

sendsystem(){
expect -c "
           set timeout -1
           spawn scp $ORACLE_BASE/oradata/$ORACLE_SID/system01.dbf [email protected]$S_IP:$ORACLE_BASE/oradata/standby/
           expect {
               yes/no { send \"yes\r\"; exp_continue }
               *assword* { send \"oracle\r\" }
           };
           expect 100%
           expect eof ;
       "
}

sendsysaux(){
expect -c "
           set timeout -1
           spawn scp $ORACLE_BASE/oradata/$ORACLE_SID/sysaux01.dbf [email protected]$S_IP:$ORACLE_BASE/oradata/standby/
           expect {
               yes/no { send \"yes\r\"; exp_continue }
               *assword* { send \"oracle\r\" }
           };
           expect 100%
           expect eof ;
       "
}

sendtemp(){
expect -c "
           set timeout -1
           spawn scp $ORACLE_BASE/oradata/$ORACLE_SID/temp01.dbf [email protected]$S_IP:$ORACLE_BASE/oradata/standby/
           expect {
               yes/no { send \"yes\r\"; exp_continue }
               *assword* { send \"oracle\r\" }
           };
           expect 100%
           expect eof ;
       "
}

sendundo(){
expect -c "
           set timeout -1
           spawn scp $ORACLE_BASE/oradata/$ORACLE_SID/undotbs01.dbf [email protected]$S_IP:$ORACLE_BASE/oradata/standby/
           expect {
               yes/no { send \"yes\r\"; exp_continue }
               *assword* { send \"oracle\r\" }
           };
           expect 100%
           expect eof ;
       "
}

senduser(){
expect -c "
           set timeout -1
           spawn scp $ORACLE_BASE/oradata/$ORACLE_SID/users01.dbf [email protected]$S_IP:$ORACLE_BASE/oradata/standby/
           expect {
               yes/no { send \"yes\r\"; exp_continue }
               *assword* { send \"oracle\r\" }
           };
           expect 100%
           expect eof ;
       "
}

sendpasswd(){
expect -c "
           spawn scp $ORACLE_HOME/dbs/orapw$ORACLE_SID [email protected]$S_IP:$ORACLE_HOME/dbs/orapwstandby
           expect {
               yes/no { send \"yes\r\"; exp_continue }
               *assword* { send \"oracle\r\" }
           };
           expect 100%
           expect eof ;
       "
}

echo "mkdir standby for standby"
echo
sleep 3s
mkd_1 >> /oracle/711/dg.log 2>&1
echo "mkdir adump for standby"
echo
sleep 3s
mkd_2 >> /oracle/711/dg.log 2>&1
echo "send orapw file to standby"
echo
sleep 3s
sendpasswd >> /oracle/711/dg.log 2>&1
echo "send pfile to standby"
echo
sleep 3s
sendpfile >> /oracle/711/dg.log 2>&1
echo "send standby controlfile to standby"
echo
sleep 3s
sendstdc >> /oracle/711/dg.log 2>&1
echo "send system.dbf to standby"
echo
sleep 3s
sendsystem >> /oracle/711/dg.log 2>&1
echo "send sysaux.dbf to standby"
echo
sleep 3s
sendsysaux >> /oracle/711/dg.log 2>&1
echo "send temp.dbf to standby"
echo
sleep 3s
sendtemp >> /oracle/711/dg.log 2>&1
echo "send undo.dbf to standby"
echo
sleep 3s
sendundo >> /oracle/711/dg.log 2>&1
echo "send user.dbf to standby"
echo
sleep 3s
senduser >> /oracle/711/dg.log 2>&1

echo "add tns"
#tns
cat >> $ORACLE_HOME/network/admin/tnsnames.ora << EOF
$P_B_UNIQUE_NAME=
 (DESCRIPTION=
  (ADDRESS=(PROTOCOL=tcp)(HOST=$P_DB)(PORT=1521))
  (CONNECT_DATA=
    (SERVICE_NAME=$P_B_UNIQUE_NAME)
    (SERVER=DEDICATED)
  )
 )

$S_B_UNIQUE_NAME=
 (DESCRIPTION=
  (ADDRESS=(PROTOCOL=tcp)(HOST=$S_DB)(PORT=1521))
  (CONNECT_DATA=
    (SERVICE_NAME=$S_B_UNIQUE_NAME)
    (SERVER=DEDICATED)
  )

 )
EOF

sendtns(){
expect -c "
           spawn scp $ORACLE_HOME/network/admin/tnsnames.ora [email protected]$S_IP:$ORACLE_HOME/network/admin/tnsnames.ora
           expect {
               yes/no { send \"yes\r\"; exp_continue }
               *assword* { send \"oracle\r\" }
           };
           expect 100%
           expect eof ;
       "
}

echo "send tns to standby"
sendtns >> /oracle/711/dg.log 2>&1

add_p_stdlog(){
su - oracle -c "sqlplus -S / as sysdba << !
set trimspool on feedback off  pagesize 0 heading off
alter database add standby logfile ‘/oracle/oradata/$ORACLE_SID/std_redo01.log‘ size 50m;
alter database add standby logfile ‘/oracle/oradata/$ORACLE_SID/std_redo02.log‘ size 50m;
alter database add standby logfile ‘/oracle/oradata/$ORACLE_SID/std_redo03.log‘ size 50m;
alter database add standby logfile ‘/oracle/oradata/$ORACLE_SID/std_redo04.log‘ size 50m;
exit;
!
"
}

echo "create standby redolog for primary"
add_p_stdlog >> /oracle/711/dg.log 2>&1

register(){
su - oracle -c "sqlplus -S / as sysdba << !
set trimspool on feedback off pagesize 0 heading off
alter system register;
exit;
!
"
}
register >> /oracle/711/dg.log 2>&1

#去备库mount 建standby redo
#调用mount_standby.sh

expect -c "
           spawn ssh $S_IP
           expect {
               yes/no { send \"yes\r\"; exp_continue }
               *assword* { send \"oracle\r\" }
           } ;
           expect ] { send \"cd /oracle/711 \r\" };
           expect ] { send \"sh /oracle/711/mount_standby.sh \r\" };
#          send \r;
           expect eof ;
       "

#至此主库mount,备库mount

sleep 10s

#open主库
get_standby_status(){
standby_status=$(
su - oracle -c "sqlplus -S sys/[email protected] as sysdba << !
set trimspool on feedback off pagesize 0 heading off
select status from v\\\$instance;
exit;
!
"
)
}

echo
echo "get standby status"
get_stanby_status
echo $standby_status

open(){
su - oracle -c "sqlplus -S / as sysdba << !
set trimspool on feedback off pagesize 0 heading off
alter database open;
exit;
!
"
}

#if [ "$standby_status" == "Mounted" ];then
#   echo "开始收尾工作open主库"
#   open >> /oracle/711/dg.log 2>&1
#else
#   echo "standby not mounted please check"
#   exit -1
#fi

while true
do
    get_standby_status
    if [ "$standby_status" == "MOUNTED" ];then
        echo "开始收尾工作open主库"
        open >> /oracle/711/dg.log 2>&1
        break
    fi
done

expect -c "
           spawn ssh $S_IP
           expect {
               yes/no { send \"yes\r\"; exp_continue }
               *assword* { send \"oracle\r\" }
           } ;
           expect ] { send \"cd /oracle/711 \r\" };
           expect ] { send \"sh /oracle/711/recover.sh \r\" };
#          send \r;
           expect eof ;
       "

echo "all done!"       

config文件类似于这样:

# 指定软件存放目录
SOFTWARE_HOME=/tmp/database

#指定oracle实例名称,默认orcl
ORACLE_SID=orcl

#指定INVENTORY
INVENTORY_LOCATION=/oracle/oraInventory

#指定oracle基目录,默认/oracle
ORACLE_BASE=/oracle

#SET ORACLE_HOME,默认/oracle/db11g
ORACLE_HOME=/oracle/db11g

#SET DB_NAME,默认orcl
DB_NAME=orcl

#指定归档路径,默认为{ORACLE_HOME}/arch
ARCH=/oracle/arch

#指定主库hostname,此脚本并不会配置按照您的填写配置主机名
P_DB=master

#指定主库的ip,此脚本并不会配置按照您的填写配置ip
P_IP=192.168.134.139

#指定备库hostname,此脚本并不会配置按照您的填写配置主机名
S_DB=slave

#指定备库的ip,此脚本并不会配置按照您的填写配置ip
S_IP=192.168.134.134

#指定主库B_UNIQUE_NAME,默认为primary
P_B_UNIQUE_NAME=primary

#指定备库B_UNIQUE_NAME,默认为standby
S_B_UNIQUE_NAME=standby

#指定控制文件,默认两个分别为{ORACLE_BASE}/oradata/{DB_UNIQUE_NAME}/control01.ctl和{ORACLE_BASE}/oradata/{DB_UNIQUE_NAME}/control02.ctl
#ctl=/disk1/control01.ctl,/disk2/control02.ctl,/disk3/control03.ctl

到这脚本没有完,具体看附件

哦传不了附件,那就算了吧

版权声明:本文为博主原创文章,未经博主允许不得转载。红黑联盟,linux公社盗转死妈

时间: 2024-11-06 23:34:08

一键DG脚本(静默安装创建实例创建DG)的相关文章

Oracle 11.2.0.4静默安装软件和创建数据库

在没有图形界面的情况时,安装Oracle数据库软件和创建数据库就只能使用命令行的方式进行. 本文假设安装的前提条件都已经配置好. 一.静默安装数据库软件 1.准备参数文件 这个参数文件是用于安装软件使用的,里边配置了需要安装的版本.语言.ORACLE_HOME等信息. 这个文件的模版在database/response目录下的db_install.rsp,文件中的各个参数作用参考http://docs.oracle.com/cd/E11882_01/em.112/e12255/oui3_resp

安卓静默安装应用之创建手机桌面图标,并点击图标安装新的应用

当用户安装了一个应用,这个应用的开发者如果足够自信或者基于其他一些原因,当然希望用户也能安装自己其他的应用去试试,这样就能笼络更多用户,将网撒的更大.所以也就不难理解为啥很多时候我们明明就安装了一个新闻应用,但手机桌面却多了一个游戏市场的应用图标,然后我们点击这个图标就会安装这对应的APP游戏市场应用.更进一步,就做到静默安装,在后台安装了新的应用,用户回到桌面才发现, "OMG  我啥时候下了这apk我自己都不知道..." 这次我们用简单的方法先实现创建手机桌面的快捷方式,并点击该图

静默安装Oracle及创建数据库11G R2

oracle.install.responseFileVersion=/oracle/install/rspfmt_dbinstall_response_schema_v11_2_0 oracle.install.option=INSTALL_DB_SWONLY DECLINE_SECURITY_UPDATES=true UNIX_GROUP_NAME=oinstall INVENTORY_LOCATION=/u01/app/oracle/oraInventory SELECTED_LANGUA

Shell脚本静默安装数据库Oracle 12c (Centos6)

刚入职不久,主管就叫我用shell脚本部署Oracle数据库,一开始懵的一批,手动安装也搞了很久,皇天不负有心人,哈哈!!!搞了几天终于搞好了,也搞了份脚本安装oracle.(对于经常搭建Oracle数据库,重复步骤做得很烦的盆友可以来看看) Oracle安装脚本: [[email protected] ~]# cat oracle_install.sh #!/bin/bash #install oracle #CentOS release 6.9  #关闭selinux sed -i 's/S

linux CentOS7最小化安装环境静默安装Oracle11GR2数据库(静默创建实例)

执行安装前的准备工作: 1.安装操作系统:http://blog.51cto.com/mflag/23003592.安装依赖包:http://blog.51cto.com/mflag/23009093.上传并解压安装包;http://blog.51cto.com/mflag/23009124.修改配置文件:http://blog.51cto.com/mflag/2300914 运维.网管必会技能:linux centos7命令行静默安装oracle11GR2视频课程视频教程:http://edu

linux CentOS7下快速静默安装Oracle11GR2数据库并创建实例

linux CentOS7下快速静默安装Oracle11GR2数据库并创建实例本文章适合已经熟悉安装过程,需要快速部署使用初学者建议分步学习:https://edu.51cto.com/course/15530.html linux.x64_11gR2_database_1of2.zip (oracle官网下载)linux.x64_11gR2_database_2of2.zip (oracle官网下载)rlwrap-0.42.tar.gz (51CTO下载里搜)三个安装包: 一.上传安装包 mk

CentOS 7 静默安装oracle 11R2 ,并手动创建数据库 createdatabase

官方文档:http://docs.oracle.com/cd/E11882_01/install.112/e24326/toc.htm#BHCCADGD 需要的安装包: binutils-2.23.52.0.1-12.el7.x86_64 compat-libcap1-1.10-3.el7.x86_64 compat-libstdc++-33-3.2.3-71.el7.i686 compat-libstdc++-33-3.2.3-71.el7.x86_64 gcc-4.8.2-3.el7.x86

Oracle 实例创建脚本

遥想刚入职时,一台服务器就挂了,所以就是在"危难"中有机会学习.在夜深人静时,我想去回忆一下Oracle实例的创建.一种方法,DBCA,图形界面,但是需要依赖xming或xmanager这类软件:另一种方法就是脚本创建Oracle数据库实例.第一种方法简单,易上手,但是对初学者,不能学习到更多数据库知识:第二种方法用脚本就能更了然数据库的各种文件以及参数.本文只介绍脚本创建Oracle实例. 记得2010年,还是用Oracle10g,如今用11g,他们的差异在于dump 目录略不同.我

依赖注入之setter注入---只需修改配置,电脑就可以安装不同的打印机;读取properties配置文件并创建实例;实现不采用new的方式直接实例化对象

1.项目截图 2.黑白打印机类 package com.example.demo.printer; public class GrayPrinter implements Printer{ @Override public void init() { System.out.println("启动打印机"); } @Override public void print(String txt) { System.out.println("打印黑白文字:".concat(