Copy and verify file from remote host script

Here is a simple script to copy & verify oracle binary file from remote host:

#!/bin/bash
#copy the lastest tested binary
#
if [ $# -lt 1 ]; then
  echo "
        usage:   get_binary.sh <version>
        example: get_binary.sh 11203
  "
  exit 1
fi

oraver=$1
target_dir=/oracle/binary/$oraver
source_host=source.wordpress.com
source_dir=/oracle/home/products/

logfile=$target_dir/get_binary_$oraver.log
tarfile=$oraver.tar.Z
cksumfile=$tarfile.cksum
lsinventoryfile=$tarfile.lsinventory

#Check if there is new binary 
cd $target_dir

#check if source cksum file is valid
recksum=0
scksum=`ssh $source_host "cat $source_dir/$cksumfile" 2>&1`
if [ $? -ne 0 ]; then
  echo "cksum file $source_host:$source_dir/$cksumfile doesn't exit"
  recksum=1
else 
  fsz=`ssh $source_host "stat --printf="%s" $source_dir/$tarfile"`
  tsz=`echo $scksum | awk '{print \$2}'` 
  if [ $fsz -ne $tsz ]; then
     echo "$source_host:$source_dir/$tarfile size $fsz didn't match size $tsz in $source_host:$source_dir/$cksumfile"
     recksum=1
  fi
fi
if [ $recksum -eq 1 ]; then
  echo "recreate cksum file"
  ssh $source_host "cd $source_dir; cksum $tarfile > $cksumfile"
  scksum=`ssh $source_host "cat $source_dir/$cksumfile" 2>&1`
fi

tcksum=`cat $cksumfile 2>&1`
if [[ "$scksum" == "$tcksum" ]] ; then
  echo "The source binary is the same as target. Exit."
  echo "If you want to update anyway, pls first rm $target_dir/$cksumfile at `hostname`"
  exit;
else

#Copy binary
date > $logfile
scp $source_host:$source_dir/$tarfile $tarfile.new
scp $source_host:$source_dir/$lsinventoryfile .
re=`mv $tarfile $tarfile.old 2>&1`
mv $tarfile.new $tarfile
cksum $tarfile > $cksumfile

#Check result
scksum=`ssh $source_host "cat $source_dir/$cksumfile"`
tcksum=`cat $cksumfile`
if [[ "$scksum" == "$tcksum" ]] ; then
  echo "Copying binary is successful"
else
  echo "Looks like copying binary is failed, cksum file didn't match, restore $tarfile"
  echo "Source $source_host:$source_dir/$cksumfile :"
  echo "$scksum"
  echo "Target $cksumfile :"                       
  echo "$tcksum"
  mv $tarfile $tarfile.new
  re=`mv $tarfile.old $tarfile 2>&1`
fi

echo "Source cksum: $scksum" >> $logfile
echo "Dest   cksum: $tcksum" >> $logfile
date >> $logfile

echo "logfile: $logfile at `hostname`"
fi
Advertisements

About Alex Zeng
I would be very happy if this blog can help you. I appreciate every honest comments. Please forgive me if I'm too busy to reply your comments in time.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: