Skip to main content
That was by changing an old command or a confusion from much stress... I am fully aware -rf is redundant for files...
Source Link

Bug?

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. Understand the meaning of every flag you use, and only use a flag if it's really needed.

Alternative solution

I suggest to write like this:

#!/bin/bash

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"

Bug?

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. Understand the meaning of every flag you use, and only use a flag if it's really needed.

Alternative solution

I suggest to write like this:

#!/bin/bash

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"

Bug?

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:

#!/bin/bash

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"
Source Link
janos
  • 111.7k
  • 15
  • 152
  • 391

Bug?

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. Understand the meaning of every flag you use, and only use a flag if it's really needed.

Alternative solution

I suggest to write like this:

#!/bin/bash

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"