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
.
Best Answer
Try removing the single-quotes from the COMMAND3 creation line:
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:
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.