Wednesday, 16 March 2011

How to FTP Archive Log Files to Standby / DR Server at Regular Intervals, Using Unix Script and Cron Job

How to FTP Archive Log Files to Standby / Disaster Recovery (DR) Server at Regular Intervals, Using Unix Script and Cron Job

 
The sample script will help to synchronize / transport archive log files from primary server to the DR / Standby Server

This sample copies all files since yesterday (estimated) to a temporary FTP directory and then starts FTPing to DR database. The process is very sensitive and could result in very serious consequences. Please be very careful while applying it on production database.


a. Create Unix script as listed following:

$ cd /data1/users/scripts/backup/

#!/bin/sh
HOST='testHost.com'
USER='testuser'
PASSWD='testpassword'

## define actual archive log directory
TARGET_DIR1=/data/app/oracle/orcl/FTP_ARCLOGS/

## define temporary archive log directory to be used by ftp
TARGET_DIR2=/data/app/oracle/orcl/archlogs/

SUCCESS1='NOT OK'
SUCCESS2='NOT OK'
SUCCESS3='NOT OK'

#CREATE A LOG FILE
NEW_DATE1=`date +"%m_%d_%y"`
LOGFILE_NAME='FTP_ARCLOGS'$NEW_DATE1'.log'
touch /data1/users/ftplog/$LOGFILE_NAME


##Clear the temporary archive log directory first
cd /data/app/oracle/orcl/FTP_ARCLOGS/
CUR_DIR=`pwd`'/'
if [ "$CUR_DIR" = "$TARGET_DIR1" ]
then
rm -f *
SUCCESS1='OK'
echo $SUCCESS1
fi

## Go to actual archive log directory and copy files(until yesterday) to
## the ftp directory
rcp -p `find /data/app/oracle/orcl/archlogs/ -name '*arc' -mtime -1` /data/app/oracle/orcl/FTP_ARCLOGS/


##Update log file with the list of files to be FTPed
cd /data/app/oracle/orcl/FTP_ARCLOGS/
echo 'List of files to be copied...... date/time: ' `date` >> /data1/users/ftplog/$LOGFILE_NAME
ls -l >> /data1/users/ftplog/$LOGFILE_NAME
CUR_DIR=`pwd`'/'
if [ "$CUR_DIR" = "$TARGET_DIR1" ]
then
echo 'Connecting to FTP server and start sending files. Please be patient during this operation.... date/time: ' `date` >> /data1/users/ftplog/$LOGFILE_NAME

## Kick start FTP to send files to target server
ftp -n -i $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
bin
cd cd /dataDR/app/oracle/orcl/archlogsDR/
mput *.arc
quit
END_SCRIPT
echo 'FTP operation completed successfully. --  date/time: ' `date` >> /data1/users/ftplog/$LOGFILE_NAME
SUCCESS3='OK'
else
`date` >> /data1/users/ftplog/$LOGFILE_NAME
SUCCESS3='NOT OK'
fi
exit 0

b. Configure the script in crontab to run regularly at 10:00 pm (for example)

0 22 * * * /data1/users/scripts/backup/ftp_logs_to_dr.sh


No comments:

Post a Comment