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

The name of the pictureThe name of the pictureThe name of the pictureClash 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.







share|improve this question

























    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.







    share|improve this question





















      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.







      share|improve this question











      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.









      share|improve this question










      share|improve this question




      share|improve this question









      asked Feb 9 at 19:23









      user9303970

      1456




      1456




















          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"





          share|improve this answer























          • 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










          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "196"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          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






























          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"





          share|improve this answer























          • 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














          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"





          share|improve this answer























          • 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












          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"





          share|improve this answer















          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"






          share|improve this answer















          share|improve this answer



          share|improve this answer








          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, 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















          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












           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          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













































































          Popular posts from this blog

          Python Lists

          Aion

          JavaScript Array Iteration Methods