Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm writing a backup script for my email and cloud service.
For simplicity, I only posted the mail-backup part here.

Basically, I have a Host-Machine with some Virtual Boxes running in it.
These Virtual Boxes access Host-Machines' /mnt/alias/storage/ where there is the vmail folder (called shishaMail in my case)

(Following might not be necessary information)
I would like to say that everything within /mnt/alias/ is an ln -s from /mnt/rack/* with pretty names.
In this rack folder are the mounts of the drives.
(Not necessary information END)

Here is my script

#!/bin/bash
# backup script 

BASEURL="/mnt/alias/backup"
MAILURL="/mnt/alias/storage/shishaMail"

DAY=`eval date +"%d"`
MONTH=`eval date +"%m"`
YEAR=`eval date +"%Y"`
HOUR=`eval date +"%H"`
MINUTE=`eval date +"%M"`

PIMPURL=$BASEURL/$YEAR/$MONTH/$DAY/

COMMAND1="cd $PIMPURL"
$COMMAND1 2>/dev/null

if [ $? -eq 0 ]
then
                echo "Command 1 was successful"
else
                echo "There ain't folders!!"
                echo "I'll mkdir some!"

                COMMAND2="mkdir -p $PIMPURL"
                $COMMAND2 2>/dev/null
fi
if [ $? -eq 0 ]
then
                echo 'Command 1 or/and 2 was successful'
                echo "STARTING MAIL BACKUP"

                COMMAND3="tar -cvzf '"$PIMPURL"shisha_"$HOUR"_"$MINUTE"_.data.tar.gz' "$MAILURL
                echo $COMMAND3
                $COMMAND3 2>/dev/null

                if [ $? -eq 0 ]
                then
                                echo 'MAIL BACKUP SUCCESSFULL'
                else
                                echo "FAILURE!!"
                fi
else
                echo 'Nope! Must be some kind of strage Err0r!!'
fi

So if I execute this script I get the following error:

Command 1 was successful
Command 1 or/and 2 was successful
STARTING MAIL BACKUP
tar -czvf '/mnt/alias/backup/2014/06/13/shisha_10_25_.data.tar.gz' /mnt/alias/storage/shishaMail
tar: Removing leading `/' from member names
tar (child): '/mnt/alias/backup/2014/06/13/shisha_10_25_.data.tar.gz': Cannot open: No such file or directory
tar (child): Error is not recoverable: exiting now
FAILURE!!

As you see there is an echo of COMMAND3 which would be:

tar -czvf '/mnt/alias/backup/2014/06/13/shisha_10_21_.data.tar.gz' /mnt/alias/storage/shishaMail

And if I execute this command by hand, as the same user I execute the script as (which would be root in both cases), it works.
I don't get why the script complains about a not existing file or directory as the directories exist except the *.tar.gz file as this has to be created by tar.

share|improve this question
2  
You say an the end “And if i execute this command by hand, as the same user i execute the script (which would be root in both cases) i get an error.” — are the last 4 words correct? I was expecting a contrast. –  richard Jun 13 at 8:52
    
And does /mnt/alias/backup/2014/06/13/ exist? Also, what Richard said. How does the command behave differently when run manually? –  terdon Jun 13 at 8:56
    
@richard pardon me, when i copy the COMMAND3 echo and execute it manually, it works as assumed. @terdon yes i am sure /mnt/alias/backup/2014/06/13/ exists. In fact, my script checks if it can switch the the repository. If he cannot, it will try to create the folder by the mkdir -p command –  derty Jun 13 at 9:04

1 Answer 1

up vote 3 down vote accepted

Try removing the single-quotes from the COMMAND3 creation line:

COMMAND3="tar -cvzf "$PIMPURL"shisha_"$HOUR"_"$MINUTE"_.data.tar.gz "$MAILURL

When you execute that line by hand, the shell removes the quotes before tar ever sees the arguments.

You're not inserting spaces in the filename, therefore quotes are not necessary. Actually, you could simplify that line some more:

COMMAND3="tar -cvzf ${PIMPURL}shisha_${HOUR}_${MINUTE}_.data.tar.gz ${MAILURL}"

The curly braces are to delimit the variable names, otherwise it'd try expanding 'PIMPURLshisha', probably not what you want. Never hurts to use them, as it makes variables stand out too, easier to pick out.

share|improve this answer
    
Thanks a bucket lornix! And i finally learned how to delimit variable names^^ Ahh, man i hate bash syntax but at least it a lot of fun. –  derty Jun 13 at 9:45
    
I learn something every day! Enjoy! –  lornix Jun 13 at 9:48

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.