What do you think of my script to make an immediate DB and document root zip backup? Any flaw you might find. Please inform me of it:

mysqldump -u root -p --all-databases | zip ${drt}/db-$(date +\%F-\%T).zip
zip -r all_zipped-$(date +\%F-\%T).zip ${drt}/ -x "*/cache/*"
rm -rf  ${drt}/db-$(date +\%F-\%T).zip

It is important to me to have the backup in document root because of comfortability. It's a small server environment (with external automatic backups) and I think it'll be redundant to have a backup dir just for once-in-a-while immediate backups I do with that nice script above.


1 Answer 1



It looks to me that the name of the zip file used in the first and the last command were intended to be the same, that is the ${drt}/db-$(date +\%F-\%T).zip part:

mysqldump ... | zip ${drt}/db-$(date +\%F-\%T).zip
rm -rf  ${drt}/db-$(date +\%F-\%T).zip

They will only be the same if the mysqldump and rm commands run within the same second.

Don't repeat yourself

If a non-trivial command is expected to return the same value every time you run it, then extract it to a variable. Not only to save unnecessary processing power, but also to keep the non-trivial logic in one place, so that it's easy to change if ever needed, in one place.

Double-quote command arguments

It's a good rule of thumb to double-quote command arguments when they contain variables, to protect yourself from unintended word splitting and glob expansions.

Useless flags

In rm -rf ${drt}/db-$(date +\%F-\%T).zip, since we're deleting a file, not a directory, the -r flag is useless. Avoid useless flags.

Alternative solution

I suggest to write like this:


date=$(date +\%F-\%T)

mysqldump -u root -p --all-databases | zip "${drt}/db-$date.zip"
zip -r "all_zipped-$date.zip" "${drt}"/ -x "*/cache/*"
rm -f "${drt}/db-$date.zip"
  • \$\begingroup\$ I didn't know how to handle that problem that problem of different timestamps in the files, for mysqldump ... | zip ${drt}/db-$(date +\%F-\%T).zipVS rm -rf ${drt}/db-$(date +\%F-\%T).zip. Thanks for showing a way and for the rest of the answer. I am fully aware that rf is redundant when it's a file; It was a mistake. I thank you dearly. I would thumb up both your answers if I could. I miss 5 points. \$\endgroup\$ Feb 10, 2018 at 8:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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