Oracle自动化安装脚本-part01-亲试ok

 
#!/bin/bash
 
node_num=$1
base_config=./network.conf   网络配置文件
software_config=./software.conf  软件包配置文件
tmp_file=/tmp/autorac.tmp
DISPLAY_ADDR=192.168.8.229
 
if [ ! ${node_num} ];then
echo "specify the node number"
exit 0
fi
 
 
Get_Block()
{
block_name=$1
config_file=$2
begin_line=0
end_line=0
line_num=0
while read line
do
        line_num=`expr ${line_num} + 1`
        is_get=`echo $line | awk "/\[${block_name}]/{print 1;next}{print 0}"`
        if [ ${is_get} -eq 1 ];then
                begin_line=${line_num}
                continue
        fi
 
        if [ ${begin_line} -ne 0 ];then
                is_get=`echo $line | awk "/\[.*]/{print 1;next}{print 0}"`
                if [ ${is_get} -eq 1 ];then
                        end_line=`expr ${line_num} - 1`
                        break
                fi
        fi
 
done < ${config_file}
}
 
 
IP_fun()
{
field_num=`expr $1 + 1`
block_name=$2
tmp_file=/tmp/autorac.tmp
 
Get_Block ${block_name} ${base_config}
 
sed -n "${begin_line},${end_line}p" ${base_config} | grep -v ^$ > ${tmp_file}
 
is_ip=`echo ${block_name} | awk '/IP/{print 1;next}{print 0}'`
 
if [ ${is_ip} -eq 1 ];then
        IPADDR=`awk -F"[:,]" -v num=${field_num} '/IP-LIST/{print $num}' ${tmp_file}`
        HOSTNAME=`awk -F"[:,]" -v num=${field_num} '/HOSTNAME/{print $num}' ${tmp_file}`
        NETMASK=`awk -F"[:,]" '/NETMASK/{print $2}' ${tmp_file}`
        GATEWAY=`awk -F"[:,]" '/GATEWAY/{print $2}' ${tmp_file}`
        DNS1=`awk -F"[:,]" '/DNS1/{print $2}' ${tmp_file}`
        DNS2=`awk -F"[:,]" '/DNS2/{print $2}' ${tmp_file}`
 
 
        if [ "${block_name}" == "PUBLIC-IP" -o "${block_name}" == "PRIVATE-IP" ];then
                if [ "${block_name}" == "PUBLIC-IP" ];then
                        network="ifcfg-eth0"
                        sed -i "s/HOSTNAME=.*/HOSTNAME=${HOSTNAME}/" /etc/sysconfig/network
                        echo "IPADDR:"${IPADDR}
                        echo "HOSTNAME:"${HOSTNAME}
                else
                        network="ifcfg-eth1"
                fi
 
         tmp_ipaddr_file=/tmp/autorac_ipaddr_file.tmp
                 awk '/DEVICE/||/TYPE/||/HWADDR/||/NM_CONTROLLED/{print $1}' /etc/sysconfig/network-scripts/${network} >${tmp_ipaddr_file}
                 echo "IPADDR="${IPADDR} >>${tmp_ipaddr_file}
                 echo "NETMASK="${NETMASK} >>${tmp_ipaddr_file}
                 echo "GATEWAY="${GATEWAY} >>${tmp_ipaddr_file}
                 echo "DNS1="${DNS1} >>${tmp_ipaddr_file}
                 echo "DNS2="${DNS2} >>${tmp_ipaddr_file}
                 echo -e "ARPCHECK=no\nONBOOT=\"yes\"\nBOOTPROTO=static" >>${tmp_ipaddr_file}
 
                 cat ${tmp_ipaddr_file} > /etc/sysconfig/network-scripts/${network}
 
        fi
 
        rac_node_name=/tmp/autorac_node.tmp
        echo >${rac_node_name}
        node_total=`awk -F"[:,]" '/IP-LIST/{print NF}' ${tmp_file}`
        for i in `seq 2 ${node_total}`
        do
                IPADDR=`awk -F"[:,]" -v num=${i} '/IP-LIST/{print $num}' ${tmp_file}`
                HOSTNAME_1=`awk -F"[:,]" -v num=${i} '/HOSTNAME/{print $num}' ${tmp_file}`
        HOSTNAME=${HOSTNAME_1:-${HOSTNAME}}
 
                  echo -e ${IPADDR} ${HOSTNAME} #ORACLE_CONFIG>> /etc/hosts
 
                if [ "${block_name}" == "PUBLIC-IP" -o "${block_name}" == "PRIVATE-IP" ];then
                        echo ${block_name}:${HOSTNAME} >> ${rac_node_name}
                fi
        done
fi
 
#is_storage=`echo ${block_name} | awk '/STORAGE/{print 1;next}{print 0}'`
 
}
 
sed -i '/ORACLE_CONFIG/d' /etc/hosts
 
IP_fun ${node_num} PUBLIC-IP
IP_fun ${node_num} PRIVATE-IP
IP_fun ${node_num} VIP
IP_fun ${node_num} SCAN-IP
 
echo '#1.关闭防火墙,selinux'
#shutdown iptables
service iptables stop
chkconfig iptables off
 
#shutdown fireware
setenforce 0
sed -i 's/SELINUX=.*/SELINUX=disable/g' /etc/selinux/config
 
echo '#2.关闭NTP'
#disable ntp server
service ntpd status | grep run && service ntpd stop
chkconfig ntpd off
if [ -e /etc/ntp.conf ];then
mv /etc/ntp.conf /etc/ntp.conf.bak
fi
 
 
echo '#3.安装软件包'
#install package
 
Get_Block()
{
block_name=$1
config_file=$2
begin_line=0
end_line=0
line_num=0
while read line
do
        line_num=`expr ${line_num} + 1`
        is_get=`echo $line | awk "/\[${block_name}]/{print 1;next}{print 0}"`
        if [ ${is_get} -eq 1 ];then
                begin_line=${line_num}
                continue
        fi
 
        if [ ${begin_line} -ne 0 ];then
                is_get=`echo $line | awk "/\[.*]/{print 1;next}{print 0}"`
                if [ ${is_get} -eq 1 ];then
                        end_line=`expr ${line_num} - 1`
                        break
                fi
        fi
 
done < ${config_file}
}
 
 
Install_package_fun()
{
for i in `ls /etc/yum.repos.d/ | grep -v bak$`
do
        mv /etc/yum.repos.d/$i /etc/yum.repos.d/$i.bak
done
 
cat > /etc/yum.repos.d/centos6.repo  <<EOF
[centos6.8]
name=centos6.8
enabled=1
gpgcheck=0
EOF
 
yum clean all
 
 
os_type=`lsb_release -a | awk -F"[:\t]+" '/Distributor ID/{print $2}'`
os_release=`lsb_release -a | awk -F"[:\t]+" '/Release/{printf("%d\n",$2)}'`
echo $os_type
echo $os_release
 
if [ ${os_type} == "CentOS" -o ${os_type} == "Red" ];then
        Get_Block "CentOS${os_release}" ${software_config}
        sed -n "${begin_line},${end_line}p" ${software_config} | grep -v ^$ | awk -F'-[0-9].[0-9]' '! /\[/{print $1}' > ${tmp_file}
        while read line
        do
                yum install -y -q ${line}
      rpm -qa --queryformat "%{NAME}-%{VERSION}-%{RELEASE} (%{ARCH})\n" | grep ${line}
        done < ${tmp_file}
fi
}
echo -e "\033[31mCDROM HAS BEEN MOUNTTED\033[0m" && Install_package_fun
 
echo '#4.清空之前的安装环境'
userdel -rf oracle
userdel -rf grid
groupdel oinstall
groupdel dba
groupdel oper
groupdel asmadmin
groupdel asmdba
groupdel asmoper
rm -rf /u01
rm -rf /home/oracle
rm -rf /home/grid
 
 
echo '#5.创建组和用户'
groupadd -g 501 oinstall
groupadd -g 502 dba
groupadd -g 503 oper
groupadd -g 504 asmadmin
groupadd -g 505 asmdba
groupadd -g 506 asmoper
useradd -u 501 -g oinstall -G dba,asmdba,asmadmin,asmoper grid
useradd -u 502 -g oinstall -G dba,oper,asmdba,asmadmin oracle
echo -n oracle | passwd --stdin oracle
echo -n grid   | passwd --stdin grid
 
echo '#6.创建目录'
mkdir -p /u01/app/oracle
mkdir -p /u01/app/oracle/product/11.2.0/db_1
chown -R oracle:oinstall /u01/app/oracle
 
mkdir -p /u01/app/grid
mkdir -p /u01/app/grid/product/11.2.0/db_1
chown -R grid:oinstall /u01/app/grid
chown -R grid:oinstall /u01/app/grid/product/11.2.0/db_1
 
chmod -R 775 /u01/
 
echo '#7.修改用户grid的初始化参数文件'
is_modified=`awk 'BEGIN{a=0}/grid_config/{a+=1}END{print a}' /home/grid/.bash_profile`
 
if [ ${is_modified} -eq 0 ];then
cat>> /home/grid/.bash_profile<< EOF
#grid_config
export TMP=/tmp;
export TMPDIR=\$TMP;
export ORACLE_SID=+ASM1;
export ORACLE_BASE=/u01/app/grid;
export ORACLE_HOME=/u01/app/grid/product/11.2.0/db_1/;
export NLS_DATE_FORMAT="yyyy-mm-dd HH24:MI:SS";
export PATH=\$ORACLE_HOME/BIN:\$PATH;
export NLS_LANG=AMERICAN_AMERICA.ZHS16GBK;
EOF
fi
 
 
 
echo '#8.修改用户oracle的初始化参数文件'
is_modified=`awk 'BEGIN{a=0}/oracle_config/{a+=1}END{print a}' /home/oracle/.bash_profile`
 
if [ ${is_modified} -eq 0 ];then
cat>> /home/oracle/.bash_profile<< EOF
#oracle_config
export TMP=/tmp;
export TMPDIR=\$TMP;
export ORACLE_BASE=/u01/app/oracle;
export ORACLE_HOME=/u01/app/oracle/product/11.2.0/db_1;
export ORACLE_UNQNAME=prod;
export ORACLE_SID=prod1;
export ORACLE_TERM=xterm;
export PATH=/usr/sbin:\$PATH;
export PATH=\$ORACLE_HOME/bin:\$PATH;
export LD_LIBRARY_PATH=\$ORACLE_HOME/lib:/lib:/usr/lib;
export CLASSPATH=\$ORACLE_HOME/JRE:\$ORACLE_HOME/jlib:\$ORACLE_HOME/rdbms/jlib;
export NLS_DATE_FORMAT="yyyy-mm-dd HH24:MI:SS";
export NLS_LANG="SIMPLIFIED CHINESE_CHINA.ZHS16GBK";
EOF
fi
 
echo '#9.网络参数调整'
is_modified=`awk 'BEGIN{a=0}/sysctl_config/{a+=1}END{print a}' /etc/sysctl.conf`
 
if [ ${is_modified} -eq 0 ];then
sed -i 's/^kernel.shmmax/#kernel.shmmax/g' /etc/sysctl.conf
sed -i 's/^kernel.shmall/#kernel.shmall/g' /etc/sysctl.conf
cat>> /etc/sysctl.conf<<EOF
#sysctl_config
fs.aio-max-nr = 1048576 fs.file-max = 6815744 kernel.shmall = 2097152
kernel.shmmax = 1417529344
kernel.shmmni = 4096
kernel.sem = 250 32000 100 128
net.ipv4.ip_local_port_range = 9000 65500
net.core.rmem_default = 262144
net.core.rmem_max = 4194304
net.core.wmem_default = 262144
net.core.wmem_max = 1048586
EOF
fi
 
 
echo '#10.资源限制参数调整'
is_modified=`awk '/oracle/||/grid/{a+=1}END{print a}' /etc/security/limits.conf`
if [ ${is_modified} -en 0 ];then
    cat>> /etc/security/limits.conf<<EOF
    oracle soft nproc 2047
    oracle hard nproc 16384
    oracle soft nofile 1024
    oracle hard nofile 65536
    oracle soft stack 10240
    grid soft nproc 2047
    grid hard nproc 16384
    grid soft nofile 1024
    grid hard nofile 65536
    grid soft stack 10240
EOF
fi
 
echo '#11.修改主机参数文件'
is_modified=`awk 'BEGIN{a=0}/profile_config/{a+=1}END{print a}' /etc/profile`
 
if [ ${is_modified} -eq 0 ];then
cat >>/etc/profile<<EOF
#ORACLE_CONFIG
if [ \$USER = "oracle" ]; then
        if [ \$SHELL = "/bin/ksh" ]; then
              ulimit -p 16384
              ulimit -n 65536
        else
              ulimit -u 16384 -n 65536
        fi
fi
alias grep='grep --color=auto'
export PATH=\$PATH:/u01/app/11.2.0/grid/bin
export DISPLAY=$DISPLAY_ADDR
EOF
fi
 
while [ ${exitcode:=0} -eq 0 ]
do
times=`expr ${times:=0} + 1`
is_install_multipath=`rpm -qa | grep -E "device-mapper-[0-9]|multipath" |wc -l`
if [ ${is_install_multipath} -ge 2 ];then
    if [ ! -e /etc/multipath.conf ];then
        cp /usr/share/doc/device-mapper-multipath-0.4.9/multipath.conf /etc/multipath.conf && service multipathd restart || echo -e "\033[31mMultipath ERROR\033[0m"
    fi
 
    is_modified=`awk 'BEGIN{a=0}/ORACLE_CONFIG/{a+=1}END{print a}' /etc/multipath.conf`
    if [ ${is_modified} -eq 0 ];then
        echo '#ORACLE_CONFIG' >> /etc/multipath.conf
        awk -F'[ \t]+' 'BEGIN{print "multipaths '{'";a=0}NF==2&&/mpath/{a+=1;printf("\tmultipath '{'\n\t\twwid\t%s\n\t\talias\tlun%d\n\t'}'\n",$2,a)} \
            END{print "'}'"}' /etc/multipath/bindings >> /etc/multipath.conf
    fi
 
    service multipathd restart
    sleep 5
    if [ $? -ne 0 ];then
        echo -e "\033[31mMultipath ERROR\033[0m"
    else        
        is_multipath=`multipath -ll | awk -F'[ \t]' '/lun/{print $3}'|wc -l`
    fi
    exitcode=1
else
    rm -rf /var/lib/yum/history/*.sqlite&&yum install -y -q *multipath*&& echo -e "\033[31mInstall device-mapper-* Complete\033[0m"
    if [ ${times} -ge 3 ];then
        echo -e "\033[31mInstall Package device-mpper-* ERROR\033[0m"
        break
    fi
fi
done
 
if [ ${is_multipath:=0} -ge 1 ];then
   sed -i '/^[^#]/d' /etc/udev/rules.d/60-raw.rules
   for i in `multipath -ll | grep lun | sort| awk -F'[ \t]' '{print $2}'|sed -e 's/(//g' -e 's/)//g'`
   do
        num=`expr ${num:=0} + 1`
        echo "KERNEL==\"sd*\", ACTION==\"add\", PROGRAM==\"/lib/udev/scsi_id --whitelisted --device=/dev/\$name\", \
        RESULT==\"$i\", NAME=\"raw${num}\"" >> /etc/udev/rules.d/60-raw.rules
   done
   echo "NAME==\"raw[0-9]*\",OWNER=\"grid\", GROUP=\"asmadmin\", MODE=\"0660\"" >> /etc/udev/rules.d/60-raw.rules
   start_udev
else
   echo -e "\033[31mNO STORAGE DEVICE BE FOUND\033[0m"
fi
 
 
 
echo '12.配置节点间的信任关系'
ssh_keygen()
{
for user in oracle grid
do
        awk -F':' '/PUBLIC-IP/{print $2}' ${rac_node_name} | while read line
        do
                node_name=${line}
                ssh root@${node_name} su - ${user} -c "
                rm -f ~/.ssh/id_rsa\r
                rm -f ~/.ssh/id_dsa\r
                rm -f ~/.ssh/authorized_keys\r
                ssh-keygen -t rsa -q -f ~/.ssh/id_rsa -N 099q4naefadfasfdadfAADF8W0309QERQSDQ97FGJH\r
                ssh-keygen -t dsa -q -f ~/.ssh/id_rsa -N 099q4naefadf0w323346ssd0ADF8WsdRQSDQ97FGJH\r"
 
                ssh root@${node_name} su - ${user} -c "cat ~/.ssh/*.pub" >> all_node_authorized_keys
        done
 
        awk -F: '/PUBLIC-IP/{print $2}' ${rac_node_name}| while read line
        do
                node_name=${line}
                scp all_node_authorized_keys ${user}@${node_name}:~/.ssh/authorized_keys
 
        done
done
 
 
for user in oracle grid
do
        awk -F':' '/PUBLIC-IP/{print $2}' ${rac_node_name} |while read line
        do
                node_name=${line}
                awk -F':' '{print $2}' ${rac_node_name} | while read line
                do
                        host_name=${line}
                        ssh root@${node_name} su - ${user} -c "ssh ${host_name} date"
                done
        done
done
}
 
if [ ${node_num} -eq 1 ];then
    ssh_keygen
fi
posted @ 2019-12-10 09:40  Oracle-fans  阅读(471)  评论(0编辑  收藏  举报