I have written a small script that is meant to create a gzipped tar file and scp it to a friends computer every day at 0300. It all runs to plan, but as soon as I have cron kick off the job, everything runs save the scp line.
the job runs as root from cron, same as when I run it manually. There is a line in the code if perform_backups is set to YES, that uses scp to upload the resulting file via scp. I run the job as root so the script has the permissions it needs to backup file, but then I need the scp line to run as me because I have an id_rsa.pub key on the remote machine allowing me to login without a password. I don't want to install the id_rsa.pub on his machine as root, (very insecure) so I need the scp command to be kicked off with the su -c "<command>" syntax.
It all runs to plan until I have cron run it. Can anyone please tell me what I'm doing wrong?
the job runs as root from cron, same as when I run it manually. There is a line in the code if perform_backups is set to YES, that uses scp to upload the resulting file via scp. I run the job as root so the script has the permissions it needs to backup file, but then I need the scp line to run as me because I have an id_rsa.pub key on the remote machine allowing me to login without a password. I don't want to install the id_rsa.pub on his machine as root, (very insecure) so I need the scp command to be kicked off with the su -c "<command>" syntax.
It all runs to plan until I have cron run it. Can anyone please tell me what I'm doing wrong?
Code:
#!/usr/local/bin/bash
##############################################################################
#
# snapshot.sh
# Usage: test for and backup needed sys files
# Author: Me
# Rev: 1.0
# Last Updated: 7/28/12
# Rev Log:
# 7/28/12 - Started writing - finished
# 7/29/12 - Added scp functionality
# 7/29/12 - Added Mail function
#
##############################################################################
bd=/root/SNAPSHOTS # Backup Dir - Where the files go
perform_backups="YES" # Turns on Backup feature "(YES|NO)"
uid="XXXXXXXXXXXXX" # Username SCP uses to log in
rdir="/usr/home/$uid/SNAPSHOTS" # remote Dir
email="XXXXXXXXXXXXXXXXX" # Where emails go
rsys="XXXXXXXXX" # System to store files on
uid="xxxxxxxxxxxxx" # Username SCP uses to log in
rsa="/$uid/.ssh/id.rsa.pub" # rsa key location
##############################################
# DO NOT BACKUP DIR YOUR BACKUPS ARE IN !!!! #
##############################################
declare -a files=(/etc/passwd /etc/master.passwd /usr/home \
/root/bin /usr/local/etc /etc/rc.conf /etc/hosts /etc/namedb \
/etc/resolv.conf /etc/pf.conf /usr/www /var/backups/mysql_dbs \
/usr/share/skel)
###############################################################################
numf=${#files[@]}
date=`date +%m-%d-%y`
time=`date '+TIME: %H:%M:%S'`
lf=$bd/snap_$date.log
box=`hostname -s`
tmp=$box"_"$date
tarf="snap_$tmp.tar"
zipf=$bd/$tarf.gz
rzipf=$tarf.gz
# test for and set up Backup Dir
if [ ! -d $bd ]; then
mkdir $bd
chmod -R 770 $bd
echo "--- File created $date $time ---" >> $lf
touch $bd/starter.file
(tar -pcf $bd/$tarf $bd/starter.file) &>> $lf
rm $bd/starter.file
fi
echo "============================================" >> $lf
# if Directory already has a backup for the day
# rename it. If it already has an ORG file, delete it
if [ -e $zipf ]; then
if [ -e $bd/ORG.tar.gz ]; then
echo "ORG.tar.gz deleted to make room for a new one" >> $lf
rm $bd/ORG.tar.gz
fi
echo "$zipf already exists ---> file renamed ORG.tar.gz" >> $lf
mv $zipf $bd/ORG.tar.gz
fi
echo "Snap shot process started at: $time" >> $lf
echo "Adding files to Tar Backup ($tarf)" >> $lf
# Write array to tape archiver utility
# and preserve permissions
for (( c=0; c<$numf; c++))
do
wkg_file=${files[$c]}
if [ -e "$wkg_file" ]; then
(tar -prf $bd/$tarf $wkg_file) 2>/dev/null
if [ $? -eq "0" ]; then
echo "$wkg_file ---> Successful" >> $lf
else
echo "$wkg_file ---> FAILED" >> $lf
fi
fi
done
# Gzip the resulting tar file and perform backup if selected
echo "Compressing Snapshot to $zipf" >> $lf
gzip -fq $bd/$tarf
if [[ $? -eq 0 ]]; then
echo "Compression ---> Succesful" >> $lf
if [[ "$perform_backups" == "YES" ]]; then
wd=`pwd`
[color="Red"]su $uid -c "scp $zipf $uid@$rsys:$rdir/$box/$rzipf" &>> $lf[/color]
if [[ $? -ne 0 ]]; then
echo "Upload ---> FAILED" >> $lf
fi
else
echo "Uploads are turned OFF !!" >> $lf
fi
else
echo "Compression ---> FAILED" >> $lf
echo "Backup NOT Attempted ! !" >> $lf
fi
mail -s "Snapshot for $date on $box" $email < $lf
exit