PDA

View Full Version : Unix shell script file fails as cron


ChrisK
02-14-2007, 02:12 PM
I'm running a shell script file which does a db backup with mysqldump, gzip's it and then uploads it to a ftp server with ncftpput. It runs perfectly fine from the console logged in as both root and a regular user. the file as well as the directory it resides in and the dir where the backup files are stored locally has 777 permissions. The script does the backup fine as cron but fails to connect and upload the files with ncftpput.

Can anyone please help me with this?

Here's the script:

#!/bin/bash

# -----------------------------------------------------------------------------
#
# mysqlbackup ver 1.0
#
# (c) Copyright 2003 Mark Steel. All rights reserved.
#
# This script may only be distributed unmodified.
#
# This script is intended to be used to backup all databases on a given
# server, and ftp them to the remote host of your choice.
#
# NOTE: THIS SCRIPT IS NOT GPL
# -----------------------------------------------------------------------------

# MySQL Username & Password
mysqluser="user"
mysqlpass="pass"

# Local path to store backups
localpath="/local/path"

# FTP Information and remote directory
ftpserver="ftp.server.com"
ftpuserid="login"
ftppasswd="pass"
ftpfolder="backup"



# DO NOT MODIFY BELOW THIS LINE!
# -----------------------------------------------------------------------------
date=`date '+%Y%m%d-%H%M'`
for db in `mysqlshow -uroot -prooFUcha69 | \
tr -d " " | \
tr -d "|" | \
grep -v "+" | \
grep -v "Databases"`; do

mysqldump -u$mysqluser -pmysqlpass --add-drop-table -a $db > $localpath/$date-$db.sql 2>&1

gzip -9 $localpath/$date-$db.sql > /dev/null 2>&1
done

ncftpput -u $login -p $pass $ftpserver $ftpfolder $localpath/$date*gz > /dev/null 2>&1


if [ $? != 0 ]; then
echo && echo "Script failed"
exit
fi

# end

When it runs as cron it backs up the files but doesn't upload then and I get a "Script failed"

Thanks alot for any help!

/Chris