Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I am running a website on an Amazon ec2 instance running the default Debian linux. I set up a cronjob to copy the site files, take a database imprint, and zip them up. I want to keep the file archives in the same directory as the bash script itself, which is located somewhat deep within /var/www/[a new directory]/[a new directory]/[etc]/. I created a mysql user that can only read and dump the database, but cannot write to it.

The script is as follows

#/bin/bash

CURRPATH=$(pwd)
SITEPATH="/var/www/html"

MYSQLUSER="user"
MYSQLPASS="password"
MYSQLDB="database"

CURRDATE=$(date +"%Y-%m-%d__%T_%p")
DATETAG="Backup_$CURRDATE"
TEMPDIR="$CURRPATH/$DATETAG"
ARCHIVENAME="$DATETAG.zip"

# I first copy the files so I can stick a database imprint in the same directory
cp -r  "$SITEPATH/" "$CURRPATH/"
mv "$CURRPATH/html" "$TEMPDIR"
mysqldump -u "$MYSQLUSER" -p"$MYSQLPASS" "$MYSQLDB" > "$TEMPDIR/BackupDB_$CURRDATE.sql"

cd $CURRPATH
zip -r "$ARCHIVENAME" "$DATETAG"
rm -rf "$TEMPDIR"

I set this with crontab, and it works perfectly, except for one problem - the archive ends up in the ~/root folder (I set up the crontab with the root user).

Is there a way to get this archive to spit out in the same directory as the script, the $CURRPATH variable?

share|improve this question

1 Answer 1

up vote 6 down vote accepted

CURRPATH is not the directory the script is in, it's the working directory that the script inherited from its parent process; for cron jobs, this is the home directory of the account it's running under (i.e. ~root). If you want the directory the script is in, use CURRPATH=$(dirname "$BASH_SOURCE") instead.

share|improve this answer
    
Thank you very much!! –  Nicholas Finch Jul 5 '13 at 6:12
    
@NicholasFinch if this works, don't forget to accept the answer, and perhaps vote it up if you feel it warrants it. –  EightBitTony Jul 5 '13 at 6:27
    
I do not have enough of a reputation to vote it up - would otherwise do so gladly :) –  Nicholas Finch Jul 5 '13 at 8:34
    
Also - please excuse my obvious ignorance on this matter - what do I press to 'accept' the answer? –  Nicholas Finch Jul 5 '13 at 8:39
1  
@NicholasFinch: You should see a check mark next to the answer. Click on it to mark the answer as accepted. Also, see BashFAQ/028 regarding script locations. –  Dennis Williamson Jul 5 '13 at 11:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.