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 trying to store the date in a variable ($DATE) so I can use it later, but it doesn't seem to work. See anything wrong?

DATE=$(date +"%Y-%m-%d %R")
sudo mysqldump -u root -pnotgivingyoumypassword --all-databases > ~/mysql_backups/$DATE.sql
cd /var/www && sudo tar -czf ~/www_backups/$DATE.tar ./

Update The error message is:

get:tar: 20\:41.tar: Cannot stat: No such file or directory tar: Exiting with failure status due to previous errors

It appears to work for the mysql dump, but it fails when using it in the last command.

share|improve this question
    
Do yourself a favor and don't put spaces in filenames you use in automated manners. You'll run into lots of problems with scripts that don't expect them (even your own, as you've just experienced). –  Mat Sep 2 '12 at 19:14
1  
@Mat There's no reason to be scared of spaces if variable expansions are quoted correctly. –  Chris Down Sep 2 '12 at 19:33
    
    
@Chris, there's no need to be scared of spaces in filenames, but there are plenty of reasons to be annoyed by them. –  cas Sep 3 '12 at 12:35

1 Answer 1

You need to quote your expansion of $DATE, it is undergoing wordsplitting, and thus tar is being passed two arguments instead of one once $DATE has been expanded (and the same for the redirection). Here is a fixed version (with $DATE changed to $date; by convention only environment variables should be in full uppercase):

date=$(date +"%Y-%m-%d %R")
sudo mysqldump -u root -pnotgivingyoumypassword --all-databases > ~/mysql_backups/"$date.sql"
cd /var/www && sudo tar -czf ~/www_backups/"$date.tar" ./

See http://mywiki.wooledge.org/Quotes.

share|improve this answer
    
You're right. I noticed it and i was about to fix it, but i managed to delete my entire home folder in the proccess. Yay for me! Thank you for pointing it out though, hopefully it'll help someone else some day. Quick unrelated question: In a fresh ubuntu install, the folders located inside the home folder (Desktop, Downloads and whatever else is in there) are empty by default, right? If that is the case, could you possibly name all of the folders located in there? I only lost one important file and fortunately i have a backup of it, but i want all of my folders back. Could you help me out? –  qwerty Sep 2 '12 at 19:25
    
@qwerty If you have a separate question, it should be asked as such to preserve the Q&A structure of the site. –  Chris Down Sep 2 '12 at 19:31
    
Sure, why not! If you feel like you can help, here you go: unix.stackexchange.com/questions/46985 –  qwerty Sep 2 '12 at 19:38
    
"$date" or ${date} is safer if the surrounding text can change. –  l0b0 Sep 3 '12 at 11:11
1  
@l0b0 ${date} is not safe, "${date}" is. –  Chris Down Sep 3 '12 at 15:13

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.