Oracle数据库自启动,DG配置和备份脚本

Oracle数据库自启动,DG配置和备份脚本

owner
2024-09-14 / 0 评论 / 28 阅读 / 正在检测是否收录...

数据库配置自启动

修改/etc/oratab文件

#



# This file is used by ORACLE utilities.  It is created by root.sh
# and updated by either Database Configuration Assistant while creating
# a database or ASM Configuration Assistant while creating ASM instance.

# A colon, ':', is used as the field terminator.  A new line terminates
# the entry.  Lines beginning with a pound sign, '#', are comments.
#
# Entries are of the form:
#   $ORACLE_SID:$ORACLE_HOME:<N|Y>:
#
# The first and second fields are the system identifier and home
# directory of the database respectively.  The third field indicates
# to the dbstart utility that the database should , "Y", or should not,
# "N", be brought up at system boot time.
#
# Multiple entries with the same $ORACLE_SID are not allowed.
#
#
{ORACLE_SID}:{ORACLE_HOME}:Y                                #增加该行内容 sid 和 home替换为数据库的信息

Oracle用户下修改"$ORACLE_HOME/bin/dbstart" 和 "$ORACLE_HOME/bin/dbshut"

..............
# Set path if path not set (if called from /etc/rc)
SAVE_PATH=/bin:/usr/bin:/etc:${PATH} ; export PATH
SAVE_LLP=$LD_LIBRARY_PATH

# First argument is used to bring up Oracle Net Listener
#ORACLE_HOME_LISTNER=$1                                        #注释掉改行
ORACLE_HOME_LISTNER=$ORACLE_HOME                    #添加该内容
if [ ! $ORACLE_HOME_LISTNER ]; then
  echo "Since ORACLE_HOME is not set, cannot auto-start Oracle Net Listener."
  echo "Usage: $0 ORACLE_HOME"
else
..............

root用户下增加以下内容到/etc/rc.local

su - oracle -lc "/u01/app/oracle/product/19.0.0/dbhome_1/bin/lsnrctl start" #启动监听
su - oracle -lc /u01/app/oracle/product/19.0.0/dbhome_1/bin/dbstart                    #启动数据库

/etc/rc.local文件需要给可执行权限

chmod +x /etc/rc.local

DG库实现启动后自动应用归档日志

Oracle用户下创建脚本文件/home/oracle/del_arch/autoapply.sh

#!/bin/sh
# 检查数据库是否启动的函数
check_db_is_up() {
    sqlplus -silent / as sysdba << EOF
SET PAGESIZE 0 FEEDBACK OFF VERIFY OFF HEADING OFF ECHO OFF
SELECT 1 FROM DUAL;
EXIT;
EOF
}
# 循环直到数据库启动
while ! check_db_is_up; do
  echo "Database is not up yet. Waiting for 10 seconds..."
  sleep 10
done
# 数据库已启动,设置环境变量并执行恢复脚本
export ORACLE_SID=webemr
sqlplus / as sysdba << EOF >> /home/oracle/del_arch/autoapply.log 2>&1
alter database recover managed standby database using current logfile disconnect from session;
exit
EOF

然后root用户下增加以下内容到/etc/rc.local

su - oracle -lc /home/oracle/del_arch/autoapply.sh

自动清理归档日志

Oracle用户下创建脚本文件/home/oracle/del_arch/autoapply.sh

#!/bin/sh
export ORACLE_SID=webemr
rman target / nocatalog log /home/oracle/del_arch/del_arch.sh << EOF
run {
  crosscheck archivelog all;
  delete  noprompt  archivelog all completed before 'sysdate-3'; #删除3天前的归档日志
}
exit
EOF

然后root用户下执行"crontab -e"来创建定时任务,添加以下内容

0 2 * * * su - oracle -c /home/oracle/del_arch/del_arch.sh

使用 "crontab -l" 检查定时任务

[root@localhost ~]# crontab -l
0 2 * * * su - oracle -c /home/oracle/del_arch/del_arch.sh

创建自动备份及清理备份的任务

创建脚本文件/u01/rmanback/rmanbak.sh

#!/bin/sh
export ORACLE_SID=$ORACLE_SID
rman target sys/aqa nocatalog log /u01/rmanback/rmanbak.log << EOF
run {
  allocate channel t1 type disk;
  allocate channel t2 type disk;
  sql 'alter system archive log current';
  backup as compressed backupset database tag 'dbfullbackup' format='/u01/rmanback/%T_db_%U_%d';
  backup current controlfile format='/u01/rmanback/%T_ctl_%U_%d';
  backup spfile format='/u01/rmanback/%T_sp_%U_%d';
  sql 'alter system archive log current';
backup as compressed backupset archivelog from time "sysdate-1" until time "sysdate" format='/u01/rmanback/%T_arc_%U_%d';
  crosscheck backupset;
  crosscheck archivelog all;
  delete  noprompt  archivelog all completed before 'sysdate-30';
  delete  noprompt  backupset completed before 'sysdate-2';
  release channel t1;
  release channel t2;
}
exit
EOF

然后root用户下执行"crontab -e"来创建定时任务,添加以下内容

0 2 * * * su - oracle -c /u01/rmanback/rmanbak.sh

使用 "crontab -l" 检查定时任务

[root@localhost ~]# crontab -l
0 2 * * * su - oracle -c /u01/rmanback/rmanbak.sh
0

评论 (0)

取消