#!/bin/sh

# backup script for oracle archive redo logs.
#
# copy this file to the oracle server, update the variables at the
# beginning of the script and then put it in a daily cron job like
# this: 
#
# 0 6 * * * /home/oracle/bin/oracle-backup.sh | mail -s "db backup on db3" nemo@invisiblehand.net
#
# Author: nemo@invisiblehand.net
# First version: July 20, 2004
# $Date: 2004/08/05 16:51:37 $ $Revision: 1.4 $

ORIG_FILES="*.arc"
BZ_FILES="*.arc.bz2"
AWK="awk"
# for solaris use "/usr/bin/nawk" in order for match() to work
SCP="/usr/bin/scp"
ZIP="/usr/bin/bzip2"
TODAY=`date +"%b %d"`

backup_archive_redo_logs()
{

LOG_DIR=$1
BACKUP_DEST=$2
BZIPPED_DIR=$LOG_DIR"/done"

cd $LOG_DIR

echo "command line: "$0
echo "initial contents:"
echo ""
pwd
ls -l 
echo "####################"

for  i  in $ORIG_FILES
do
    ls -l $i | $AWK -v today="$TODAY" 'match($0, today) {mod_today=1} END{exit mod_today}' -
    if [ $? = 0 ]
    then
        if [ -f $i ] 
	then 
	    echo $i " not modified today. compressing...";
	    $ZIP $i
	fi    
    else
        echo $i "  modified today, not touching it!";
    fi
done

mkdir -p $BZIPPED_DIR
for i in $BZ_FILES
do
    if [ -f $i ] 
    then 
	echo "Moving "$i " to " $BZIPPED_DIR
	mv $i $BZIPPED_DIR
    fi	
done

echo "after compression: "
ls -l
echo "####################"
# now move them to the backup destination

cd $BZIPPED_DIR

echo "before transfer" 
pwd
ls -l

for i in $BZ_FILES
do
    if [ -f $i ] 
    then 
	echo "scp "$i " to " $BACKUP_DEST
	$SCP -B $i $BACKUP_DEST
	if [ $? = 0 ]
	then
	    echo "success! now rm "$i
	    rm $i 
	else 
	    echo "failure not removing "$i" from "$BZIPPED_DIR
	fi
    fi	
done
}

# main

# do one instance
backup_archive_redo_logs "/somedir/archive/mydb_sid" "backup-user@backup-host.invisiblehand.net:/somedir/archive/mydb_sid"

# and another
backup_archive_redo_logs "/somedir/archive/mydb_sid2" "backup-user@backup-host.invisiblehand.net:/somedir/archive/mydb_sid2"


############ 
# Note for scp to work without password, you must first
# have a private/public RSA key pair setup.

# The following instructions from
# http://forums.devshed.com/archive/t-61606 show how.

# Machine 1 is source (the one where this script will run, and push
# files from  here to destination, machine 2)

# 1. On machine 1
# ssh-keygen -t dsa

# Leave the default file to save the key (id_dsa) as is. When prompted
# to enter the password, hit enter to leave it blank. This should create
# two files within /home/john/.ssh/ called id_dsa and id_dsa.pub
# 
# 2. Transfer id_dsa.pub to machine 2
# scp id_dsa.pub john@machine2:/home/john
# 
# Enter the password and send the file to machine 2.
# 
# 3. Log into the second machine. Create a .ssh directory under your login (i.e. /home/john/.ssh), if it doesn't already exist.
# 
# 4. In machine 2, type the following:
# cat id_dsa.pub >> /home/john/.ssh/authorized_keys2
# rm id_dsa.pub
# 
# This creates a file (authorized_keys2) containing a list of authorized keys to connect to the machine. If you want to have more than one computer connect to this machine, append more keys to the authorized_keys2 file using the cat command.
# 
# 5. On machine 1, type the following:
# ssh-agent bash
# 
# 
# Substitute your own favourite shell name (bash, sh, csh, zsh etc.) for tcsh. Now you can run scp commands without it prompting you for the password.
# scp test.pl me@myMachine:/home/john/
