Bash script not running right under Cron

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?

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
 
May I misunderstand you, but why you don't save the keyfile in /root/.ssh/?
So root could login as the user and use this keyfile.

regards
ath0
 
I don't want root to actually perform the transfer.
the person I am using as a remote backup does not want me to use the root account under any circumstances with an rsa key. I have the root password, but only for extreme emergencies.
 
Configure an account and run the script on that. Use the key to login.
 
I have an account configured. It's not my root account but my regular user account. like I said, everything is running fine until I let cron run it. The scp line doesn't work.
 
I had once some problems with some commands not wanting to be launched in shell scripts.
I had to put the complete path to these commands.

I can't see if you are getting an error or warning message for your scp command, it can't harm your script if you try to put the complete path of scp, /usr/bin/scp, in your su -c command.
 
Just for info

$ bash --version
GNU bash, version 4.2.28(0)-release (i386-portbld-freebsd9.0)

$ uname -a
FreeBSD XXXXX 9.0-RELEASE FreeBSD 9.0-RELEASE #1: Mon May 7 23:58:35 CEST 2012 root@XXXXX:/usr/obj/usr/src/sys/GENERIC i386

# cat /root/bin/test-scp.sh
#!/usr/local/bin/bash
su user -c "scp /home/user/archive.zip [email]user@external.host.net:archive.zip[/email]" &>> /tmp/test-scp.log

I had also put the script in the /etc/crontab, and it's working fine. The file is copied to my external host.
So I'm thinking something else may be wrong.
 
Figured it out!
When I ran the script (for the first time) on the new machines it was destined for, with or without cron, it hung. It was hanging becuase it was asking my local computer if I wanted to add the key to our local system. Didn't know that until I tried to execture the su from root on the machine manually.

Thank you all for your help. :e
 
Back
Top