#!/bin/bash
if [ $1 = "-help" ]; then
more <<@EOF
**********************************************
DESCRIPTION
This script is for NPS 6.1 ED2 Database Backup.
It generates backup scripts to the specified backup direcoty and also add a schedule job in crontab.
***********************
USAGE
install_ora_bu.sh <backup_dir> [nfs_server nfs_dir]
You can backup database on either local directory or nfs directory.
* Local directory: install_ora_bu.sh <local backup dir>.
For example:
./install_ora_bu.sh /var/tmp/backup
* NFS directory: install_ora_bu.sh <local mount point> <nfs server> <nfs shared dir>.
For example:
./install_ora_bu.sh /mnt/backup 10.56.139.180 /var/tmp/shared
**********************************************
@EOF
elif ! [ $# -eq 1 -o $# -eq 3 ]; then
echo "Unexpected input parameter. Please type -help for usage."
exit 1
fi
BACKUP_DIR=$1
length=${#BACKUP_DIR}
[ ${BACKUP_DIR:$length-1:1} = "/" ] && BACKUP_DIR=${BACKUP_DIR::$length-1}""
mkdir -p $BACKUP_DIR
chown oracle:dba $BACKUP_DIR
# if nfs shared disk, mount it
if [[ $# -eq 3 ]] ; then
mount -o hard,intr,rsize=32768,wsize=32768 $2:$3 $BACKUP_DIR/
if [ $? != 0 ]
then
echo "NFS-mount failed"
exit 1
fi
fi
if [ "$HW_PLATFORM" = "IBMBladeCenter" ]; then
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
# create full backup script special for cluster env.
cat <<@EOF > $BACKUP_DIR/cluster_ora_full_backup.sh
#!/bin/bash
BACKUP_DIR="$BACKUP_DIR"
/etc/init.d/oracle status |grep "Oracle database is running" > /dev/null
if [ /$? -ne 0 ]; then
echo "Oracle instance is not active on this node, skip database full backup!"
exit
else
echo "Begin database full backup ... "
su - oracle -c "$BACKUP_DIR/ora_full_backup.sh"
fi
@EOF
#<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
# create archive backup script special for cluster env.
cat <<@EOF > $BACKUP_DIR/cluster_ora_arch_backup.sh
#!/bin/bash
BACKUP_DIR="$BACKUP_DIR"
/etc/init.d/oracle status |grep "Oracle database is running" > /dev/null
if [ /$? -ne 0 ]; then
echo "Oracle instance is not active on this node, skip database archive backup!"
exit
else
echo "Begin database archive backup ... "
su - oracle -c "$BACKUP_DIR/ora_arch_backup.sh"
fi
@EOF
#<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
chmod u+x $BACKUP_DIR/cluster_ora_full_backup.sh
chmod u+x $BACKUP_DIR/cluster_ora_arch_backup.sh
fi
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
# generate full backup script
cat <<@EOF > $BACKUP_DIR/ora_full_backup.sh
#!/bin/bash
BACKUP_DIR="$BACKUP_DIR"
nls_date_format="yyyy-mm-dd hh24:mi:ss"
@EOF
cat <<"@EOF" >> $BACKUP_DIR/ora_full_backup.sh
. /home/oracle/.bash_profile
now=`date '+%Y-%m-%d %H:%M:%S'`
# test if db on this node
sqlplus -L testuser/testuser | grep ORA-01017 > /dev/null
if [ $? -ne 0 ]; then
echo "Error: Oracle not start, can not perform full backup! ($now)" |tee -a $BACKUP_DIR/rmanErr.log
exit
fi
# rotate logs
for i in 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
do
next=`expr $i + 1`
test -f $BACKUP_DIR/rman.log.$i && mv $BACKUP_DIR/rman.log.$i $BACKUP_DIR/rman.log.$next
done
echo "*** Database full backup begins at $now" > $BACKUP_DIR/rman.log.0
#rman script
$ORACLE_HOME/bin/rman target / <<EOF >> $BACKUP_DIR/rman.log.0
configure retention policy to recovery window of 15 days;
CONFIGURE CONTROLFILE AUTOBACKUP ON;
CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO '$BACKUP_DIR/%F';
CONFIGURE DEVICE TYPE DISK PARALLELISM 1 BACKUP TYPE TO COMPRESSED BACKUPSET;
CONFIGURE CHANNEL DEVICE TYPE DISK FORMAT '$BACKUP_DIR/%U';
crosscheck archivelog all;
backup database plus archivelog;
delete noprompt archivelog all backed up 2 times to device type disk;
crosscheck backup;
delete noprompt obsolete;
exit;
EOF
now=`date '+%Y-%m-%d %H:%M:%S'`
echo "*** Database full backup finished at $now" >> $BACKUP_DIR/rman.log.0
@EOF
#<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
# generate archivelog backup script
cat <<@EOF > $BACKUP_DIR/ora_arch_backup.sh
#!/bin/bash
BACKUP_DIR="$BACKUP_DIR"
nls_date_format="yyyy-mm-dd hh24:mi:ss"
@EOF
cat <<"@EOF" >> $BACKUP_DIR/ora_arch_backup.sh
. /home/oracle/.bash_profile
now=`date '+%Y-%m-%d %H:%M:%S'`
# test if db on this node
sqlplus -L testuser/testuser | grep ORA-01017 > /dev/null
if [ $? -ne 0 ]; then
echo "Error: Oracle not start, can not perform archivelog backup! ({$now})" |tee -a $BACKUP_DIR/rmanErr.log
exit
fi
# rotate logs
for i in 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
do
next=`expr $i + 1`
test -f $BACKUP_DIR/rman.log.$i && mv $BACKUP_DIR/rman.log.$i $BACKUP_DIR/rman.log.$next
done
echo "*** Database archive log backup begins at $now" > $BACKUP_DIR/rman.log.0
#rman script
$ORACLE_HOME/bin/rman target / <<EOF > $BACKUP_DIR/rman.log.0
configure retention policy to recovery window of 15 days;
CONFIGURE CONTROLFILE AUTOBACKUP ON;
CONFIGURE CONTROLFILE AUTOBACKUP FORMAT FOR DEVICE TYPE DISK TO '$BACKUP_DIR/%F';
CONFIGURE DEVICE TYPE DISK PARALLELISM 1 BACKUP TYPE TO COMPRESSED BACKUPSET;
CONFIGURE CHANNEL DEVICE TYPE DISK FORMAT '$BACKUP_DIR/%U';
crosscheck archivelog all;
backup archivelog all;
delete noprompt archivelog all backed up 2 times to device type disk;
exit;
EOF
now=`date '+%Y-%m-%d %H:%M:%S'`
echo "*** Database archive log backup finished at $now" >> $BACKUP_DIR/rman.log.0
@EOF
#<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
chown oracle:dba $BACKUP_DIR/ora_full_backup.sh
chmod u+x $BACKUP_DIR/ora_full_backup.sh
chown oracle:dba $BACKUP_DIR/ora_arch_backup.sh
chmod u+x $BACKUP_DIR/ora_arch_backup.sh
#su - oracle -c "mv /home/oracle/ora_arch_backup.sh $BACKUP_DIR/ora_arch_backup.sh"
#su - oracle -c "mv /home/oracle/ora_full_backup.sh $BACKUP_DIR/ora_full_backup.sh"
##########
# generate crontab file
crontab -l | grep -v "ora_full_backup.sh" | grep -v "ora_arch_backup.sh" > $BACKUP_DIR/crontab.txt
if [ "$HW_PLATFORM" = "IBMBladeCenter" ]; then
if [ $# -eq 3 ]; then
#nfs-mounted backup_dir
cat <<@EOF >> $BACKUP_DIR/crontab.txt
0 3 * * 0 mount -o hard,intr,rsize=32768,wsize=32768 $2:$3 $BACKUP_DIR; $BACKUP_DIR/cluster_ora_full_backup.sh; umount $BACKUP_DIR
0 3 * * 1,2,3,4,5,6 mount -o hard,intr,rsize=32768,wsize=32768 $2:$3 $BACKUP_DIR; $BACKUP_DIR/cluster_ora_arch_backup.sh; umount $BACKUP_DIR
@EOF
else
cat <<@EOF >> $BACKUP_DIR/crontab.txt
0 3 * * 0 $BACKUP_DIR/cluster_ora_full_backup.sh
0 3 * * 1,2,3,4,5,6 $BACKUP_DIR/cluster_ora_arch_backup.sh
@EOF
fi
else
# generate crontab file
crontab -l | grep -v "ora_full_backup.sh" | grep -v "ora_arch_backup.sh" > $BACKUP_DIR/crontab.txt
if [ $# -eq 3 ]; then
#nfs-mounted backup_dir
cat <<@EOF >> $BACKUP_DIR/crontab.txt
0 3 * * 0 mount -o hard,intr,rsize=32768,wsize=32768 $2:$3 $BACKUP_DIR; su - oracle -c "$BACKUP_DIR/ora_full_backup.sh"; umount $BACKUP_DIR
0 3 * * 1,2,3,4,5,6 mount -o hard,intr,rsize=32768,wsize=32768 $2:$3 $BACKUP_DIR; su - oracle -c "$BACKUP_DIR/ora_arch_backup.sh"; umount $BACKUP_DIR
@EOF
else
cat <<@EOF >> $BACKUP_DIR/crontab.txt
0 3 * * 0 su - oracle -c "$BACKUP_DIR/ora_full_backup.sh"
0 3 * * 1,2,3,4,5,6 su - oracle -c "$BACKUP_DIR/ora_arch_backup.sh"
@EOF
fi
fi
# activate crontab entries
crontab $BACKUP_DIR/crontab.txt
echo "Database backup scripts have been created in $BACKUP_DIR successfully!"
echo "Database backup schedule job generated!"
#if [ "$HW_PLATFORM" = "HPProLiant" ]
#then
#DEL="rm "
#else
#DEL="dd if=/dev/zero of="
#fi
#su - oracle -c "sqlplus -silent / as sysdba" <<@EOF > $BACKUP_DIR/rmLogAndTemp.sh
#SET ECHO OFF NEWPAGE 0 SPACE 0 PAGESIZE 0 FEED OFF HEAD OFF TRIMSPOOL ON linesize 10000 trim on
#select '$DEL'||member from v/$logfile;
#select '$DEL'||name from v/$tempfile;
#exit
#@EOF
#umount $BACKUP_DIR
本文介绍了一个用于NPS6.1ED2数据库备份的bash脚本,该脚本不仅生成了全备和增量备份数字化文件,还自动将这些任务加入到cron计划任务中实现定时执行。

被折叠的 条评论
为什么被折叠?



