1

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.

qwerty
  • 4,451

1 Answers1

4

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.

Chris Down
  • 125,559
  • 25
  • 270
  • 266
  • 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 02 '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 02 '12 at 19:31
  • Sure, why not! If you feel like you can help, here you go: unix.stackexchange.com/questions/46985 – qwerty Sep 02 '12 at 19:38
  • "$date" or ${date} is safer if the surrounding text can change. – l0b0 Sep 03 '12 at 11:11
  • 1
    @l0b0 ${date} is not safe, "${date}" is. – Chris Down Sep 03 '12 at 15:13