Four-lines script to make an immediate DB and document root zip backup

Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
1
down vote
favorite
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:
#!/bin/bash
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.
bash server wordpress
add a comment |Â
up vote
1
down vote
favorite
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:
#!/bin/bash
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.
bash server wordpress
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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:
#!/bin/bash
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.
bash server wordpress
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:
#!/bin/bash
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.
bash server wordpress
asked Feb 9 at 19:23
user9303970
1456
1456
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
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"
I didn't know how to handle that problem that problem of different timestamps in the files, formysqldump ... | zip $drt/db-$(date +%F-%T).zipVSrm -rf $drt/db-$(date +%F-%T).zip. Thanks for showing a way and for the rest of the answer. I am fully aware thatrfis 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.
â user9303970
Feb 10 at 8:41
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
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"
I didn't know how to handle that problem that problem of different timestamps in the files, formysqldump ... | zip $drt/db-$(date +%F-%T).zipVSrm -rf $drt/db-$(date +%F-%T).zip. Thanks for showing a way and for the rest of the answer. I am fully aware thatrfis 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.
â user9303970
Feb 10 at 8:41
add a comment |Â
up vote
1
down vote
accepted
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"
I didn't know how to handle that problem that problem of different timestamps in the files, formysqldump ... | zip $drt/db-$(date +%F-%T).zipVSrm -rf $drt/db-$(date +%F-%T).zip. Thanks for showing a way and for the rest of the answer. I am fully aware thatrfis 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.
â user9303970
Feb 10 at 8:41
add a comment |Â
up vote
1
down vote
accepted
up vote
1
down vote
accepted
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"
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"
edited Feb 10 at 9:14
user9303970
1456
1456
answered Feb 10 at 5:13
janos
95.6k12120343
95.6k12120343
I didn't know how to handle that problem that problem of different timestamps in the files, formysqldump ... | zip $drt/db-$(date +%F-%T).zipVSrm -rf $drt/db-$(date +%F-%T).zip. Thanks for showing a way and for the rest of the answer. I am fully aware thatrfis 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.
â user9303970
Feb 10 at 8:41
add a comment |Â
I didn't know how to handle that problem that problem of different timestamps in the files, formysqldump ... | zip $drt/db-$(date +%F-%T).zipVSrm -rf $drt/db-$(date +%F-%T).zip. Thanks for showing a way and for the rest of the answer. I am fully aware thatrfis 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.
â user9303970
Feb 10 at 8:41
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.â user9303970
Feb 10 at 8:41
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.â user9303970
Feb 10 at 8:41
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f187211%2ffour-lines-script-to-make-an-immediate-db-and-document-root-zip-backup%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password