1. 脚本如下

说明:按照客户要求,在主备数据库上同时部署脚本,自动判断是否是主库,如果是主库则进行备份,备库不需要备份,记录日志为备份库即可,以下脚本仅供参考,可以进一步优化。

脚本说明:

1、DATABASE_ROLE查看主备库角色变量

2、Main()函数 数据库备份脚本

3、根据主备库关键字判断是否执行main()函数

#!/bin/bash  

# Author:roidba

# filename:/backup/backup.sh

#logfile:/backup/rman_fullbackup.log

ORACLE_SID=orcl; export ORACLE_SID

ORACLE_BASE=/u01/app/oracle; export ORACLE_BASE

ORACLE_HOME=$ORACLE_BASE/product/11.2.0/dbhome_1; export ORACLE_HOME

PATH=.:${JAVA_HOME}/bin:${PATH}:$HOME/bin:$ORACLE_HOME/bin

####variables###

DATABASE_ROLE=`sqlplus -silent "/as sysdba" <

SET PAGESIZE 0 FEEDBACK OFF VERIFY OFF HEADING OFF ECHO OFF  

select DATABASE_ROLE from v\\\$database;

EXIT;  

EOF`

###function###

main()  

{

($ORACLE_HOME/bin/rman  <

connect target /  

run {  

CONFIGURE CONTROLFILE AUTOBACKUP ON;

CONFIGURE BACKUP OPTIMIZATION ON;

allocate channel d1 type disk;

allocate channel d2 type disk;

backup as compressed backupset database format '/backup/fulldbbakcup_%s_%p_%t.rmn' tag='fulldbbackup' include current controlfile;

backup as compressed backupset archivelog all format '/backup/backup_archive_%s_%p_%t.arc';

release channel d1;

release channel d2;

crosscheck backup;

delete noprompt expired backup;

delete noprompt archivelog until time 'sysdate - 3' backed up 1 times to device type disk;

delete noprompt backupset of archivelog until time 'sysdate - 3' ;

delete noprompt backupset of database completed before 'sysdate - 3';

}   

exit;  

EOF

)

}

 

###if-then-else###

 

if [ "$DATABASE_ROLE" = "PRIMARY" ];

then

    main

echo "***database role is primary*** " >>/backup/rman_fullbackup.log

else

    echo "database role is standby"

fi

 

2. 自动备份,使用crontab调度

sh /backup/rman_backup.sh >>/backup/rman_fullbackup.log &

crontab -e

07 20 * * *  sh /backup/rman_backup.sh >>/backup/rman_fullbackup.log &

3. 理解crontab格式

$more /etc/crontab

SHELL=/bin/bash

PATH=/sbin:/bin:/usr/sbin:/usr/bin

MAILTO=root

HOME=/

 

# For details see man 4 crontabs

 

# Example of job definition:

# .---------------- minute (0 - 59)

# |  .------------- hour (0 - 23)

# |  |  .---------- day of month (1 - 31)

# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...

# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat

# |  |  |  |  |

# *  *  *  *  * user-name command to be executed

 

$