Compare and copy files remotely using SCP

Posted By Sagar Patil

Following script will only work if your servers were enabled with password less login ( How to for linux /HP UNIX)

For TRUE64 Unix , I am using cksum here instead of linux md5sum. To use same script for linux just change cksum to md5sum or un comment lines in script.

Usage :

1. Your Archive Space filled up and database is stuck until you make space available. Rman backup runs every night and you don’t have access to netbackup scheduler. You want to keep on pushing archive files to a secure directory ASAP.

2. Your trace dump (udump/bdump) is filling up and you want to save trace files for future use by copying files at different location.

3. You have a dataguard standby server . Standby was down for couple of days and somehow MRP has not copied files and complaining about archives in an apply process.

How this script works? : This script will compare md5sum with local & remote files , and only copy files not available at remote site.It will also spool script “remove-$timeStamp.sh.old” to delete files once copied. I have not integrated delete functionality in script but you can delete files by running this script manually.

Parameters :

Name of Instance/File Name  : My archive files are named as “INSTANCE_NAME_%%Sequence%%.arc” like CVD_LIVE_00100.arc so I will run as “<script_name> CVD_LIVE”

Delete Days : If you pass  “<script_name> delete 5” it will delete files older than 5 days excluding today

#!/bin/bash

# Define the target host
host=”%backup_server%”

# Define the remote storage path
remotePath=”/backup2/oracle/CPI_L2_A”

# Command to use for removing files
rmCommand=”rm -f” # -f recommended, to prevent missing files breaking the script

#define the pattern to use when removing old files
# this removes old backups
removePattern=”*.[Aa][Rr][Cc]”
#this removes old remove scripts
#removePattern=”remove-*.sh”

# make sure to get a parameter
if [ “${1}” = “” ]; then
echo “Parameter required”
exit
fi

# if the parameter is delete, act specially
if [ “${1}” = “delete” ]; then
# delete needs an numeric augment
if [ “${2}” = “” ]; then
echo “delete requires an argument”
exit
fi
# remove all matching files modified in the last few days
# excluding those modified today
#    touch -t $(date +%m%d)0000 .tmp.$$
find $removePattern -mtime -${2} -and ! -newer .tmp.$$ -exec echo {} \;
rm .tmp.$$
exit
fi

# otherwise, act as if its the first part of a .arc file
list=`ls ${1}_*.[aA][rR][cC]`

timeStamp=`date +%Y-%m-%d-%H%M`

for i in $list
do
#    remoteres=`ssh $host md5sum $remotePath/$i 2>&1`
remoteres=`ssh $host cksum $remotePath/$i 2>&1`

#  localres=`md5sum $i 2>&1`
localres=`cksum $i 2>&1`

remoteMD5=`echo $remoteres | awk ‘{ print $1; }’`
localMD5=`echo $localres | awk ‘{ print $1; }’`

# these show the md5’s being returned above, just disable them not to see it
echo $i $remoteMD5
echo $i $localMD5

if [ “$remoteMD5” != “$localMD5” ]; then
# the files dont match, or the remote doesn’t have the file …
echo “$rmCommand $i” >> remove-$timeStamp.sh.old
scp $i $host:$remotePath/$i
fi
done

# make sure you can run the remove script …
if [ -f remove-$timeStamp.sh ]; then
chmod +x remove-$timeStamp.sh.old
fi

Leave a Reply

You must be logged in to post a comment.

Top of Page

Top menu