Keep only a certain amount of backups (tarballs) in a directory

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












On my debian 9.4 machine I have a script that automatically compresses & backups up all my files in a srv/ with the date in the name xx-xx-xxxx.tar. It is moved to my backups/ directory.



I would like to limit the amount of .tar files (backups) to 10 in the folder, and remove the oldest .tar file every time a new tar is created.



What would be the best way of going about this? Here is my script:



#!/bin/bash
#Purpose = Backup of Important Data
#Created on 17-1-2012
#Author = Hafiz Haider
#Version 1.0
#START
TIME=`date +%b-%d-%y` # This Command will add date in Backup File Na$
FILENAME=backup-$TIME.tar.gz # Here i define Backup file name format.
SRCDIR=/srv/daemon-data # Location of Important Data Directo$
DESDIR=/backups # Destination of backup file.
tar -cpzf $DESDIR/$FILENAME $SRCDIR
#END









share|improve this question

























    up vote
    0
    down vote

    favorite












    On my debian 9.4 machine I have a script that automatically compresses & backups up all my files in a srv/ with the date in the name xx-xx-xxxx.tar. It is moved to my backups/ directory.



    I would like to limit the amount of .tar files (backups) to 10 in the folder, and remove the oldest .tar file every time a new tar is created.



    What would be the best way of going about this? Here is my script:



    #!/bin/bash
    #Purpose = Backup of Important Data
    #Created on 17-1-2012
    #Author = Hafiz Haider
    #Version 1.0
    #START
    TIME=`date +%b-%d-%y` # This Command will add date in Backup File Na$
    FILENAME=backup-$TIME.tar.gz # Here i define Backup file name format.
    SRCDIR=/srv/daemon-data # Location of Important Data Directo$
    DESDIR=/backups # Destination of backup file.
    tar -cpzf $DESDIR/$FILENAME $SRCDIR
    #END









    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      On my debian 9.4 machine I have a script that automatically compresses & backups up all my files in a srv/ with the date in the name xx-xx-xxxx.tar. It is moved to my backups/ directory.



      I would like to limit the amount of .tar files (backups) to 10 in the folder, and remove the oldest .tar file every time a new tar is created.



      What would be the best way of going about this? Here is my script:



      #!/bin/bash
      #Purpose = Backup of Important Data
      #Created on 17-1-2012
      #Author = Hafiz Haider
      #Version 1.0
      #START
      TIME=`date +%b-%d-%y` # This Command will add date in Backup File Na$
      FILENAME=backup-$TIME.tar.gz # Here i define Backup file name format.
      SRCDIR=/srv/daemon-data # Location of Important Data Directo$
      DESDIR=/backups # Destination of backup file.
      tar -cpzf $DESDIR/$FILENAME $SRCDIR
      #END









      share|improve this question













      On my debian 9.4 machine I have a script that automatically compresses & backups up all my files in a srv/ with the date in the name xx-xx-xxxx.tar. It is moved to my backups/ directory.



      I would like to limit the amount of .tar files (backups) to 10 in the folder, and remove the oldest .tar file every time a new tar is created.



      What would be the best way of going about this? Here is my script:



      #!/bin/bash
      #Purpose = Backup of Important Data
      #Created on 17-1-2012
      #Author = Hafiz Haider
      #Version 1.0
      #START
      TIME=`date +%b-%d-%y` # This Command will add date in Backup File Na$
      FILENAME=backup-$TIME.tar.gz # Here i define Backup file name format.
      SRCDIR=/srv/daemon-data # Location of Important Data Directo$
      DESDIR=/backups # Destination of backup file.
      tar -cpzf $DESDIR/$FILENAME $SRCDIR
      #END






      scripting backup tar






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 2 hours ago









      coolman5594

      172




      172




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          Add the following in the script towards the end:



          find "$DESDIR/$FILENAME" -type f -mtime +10 -delete


          This will find your backup file (tar.gz) in the directory and delete any backup that is more than 10 days older.






          share|improve this answer



























            up vote
            0
            down vote













            You want the same logic that is used by the system utility logrotate(8), which can be configured to keep up to a maximum number of files. In user space, there are a number of logrotate-like utilities, as described in this answer: https://superuser.com/a/868519



            That being said, a simple configuration file like



            "/home/coolman/backups" 
            rotate 10



            should work with the command line



            /usr/sbin/logrotate -s $HOME/backups.state -f $HOME/backups.config





            share|improve this answer








            New contributor




            djs is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.

















              Your Answer








              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "106"
              ;
              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: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













               

              draft saved


              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f480898%2fkeep-only-a-certain-amount-of-backups-tarballs-in-a-directory%23new-answer', 'question_page');

              );

              Post as a guest






























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              0
              down vote



              accepted










              Add the following in the script towards the end:



              find "$DESDIR/$FILENAME" -type f -mtime +10 -delete


              This will find your backup file (tar.gz) in the directory and delete any backup that is more than 10 days older.






              share|improve this answer
























                up vote
                0
                down vote



                accepted










                Add the following in the script towards the end:



                find "$DESDIR/$FILENAME" -type f -mtime +10 -delete


                This will find your backup file (tar.gz) in the directory and delete any backup that is more than 10 days older.






                share|improve this answer






















                  up vote
                  0
                  down vote



                  accepted







                  up vote
                  0
                  down vote



                  accepted






                  Add the following in the script towards the end:



                  find "$DESDIR/$FILENAME" -type f -mtime +10 -delete


                  This will find your backup file (tar.gz) in the directory and delete any backup that is more than 10 days older.






                  share|improve this answer












                  Add the following in the script towards the end:



                  find "$DESDIR/$FILENAME" -type f -mtime +10 -delete


                  This will find your backup file (tar.gz) in the directory and delete any backup that is more than 10 days older.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 2 hours ago









                  sla3k

                  1864




                  1864






















                      up vote
                      0
                      down vote













                      You want the same logic that is used by the system utility logrotate(8), which can be configured to keep up to a maximum number of files. In user space, there are a number of logrotate-like utilities, as described in this answer: https://superuser.com/a/868519



                      That being said, a simple configuration file like



                      "/home/coolman/backups" 
                      rotate 10



                      should work with the command line



                      /usr/sbin/logrotate -s $HOME/backups.state -f $HOME/backups.config





                      share|improve this answer








                      New contributor




                      djs is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.





















                        up vote
                        0
                        down vote













                        You want the same logic that is used by the system utility logrotate(8), which can be configured to keep up to a maximum number of files. In user space, there are a number of logrotate-like utilities, as described in this answer: https://superuser.com/a/868519



                        That being said, a simple configuration file like



                        "/home/coolman/backups" 
                        rotate 10



                        should work with the command line



                        /usr/sbin/logrotate -s $HOME/backups.state -f $HOME/backups.config





                        share|improve this answer








                        New contributor




                        djs is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                        Check out our Code of Conduct.



















                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          You want the same logic that is used by the system utility logrotate(8), which can be configured to keep up to a maximum number of files. In user space, there are a number of logrotate-like utilities, as described in this answer: https://superuser.com/a/868519



                          That being said, a simple configuration file like



                          "/home/coolman/backups" 
                          rotate 10



                          should work with the command line



                          /usr/sbin/logrotate -s $HOME/backups.state -f $HOME/backups.config





                          share|improve this answer








                          New contributor




                          djs is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.









                          You want the same logic that is used by the system utility logrotate(8), which can be configured to keep up to a maximum number of files. In user space, there are a number of logrotate-like utilities, as described in this answer: https://superuser.com/a/868519



                          That being said, a simple configuration file like



                          "/home/coolman/backups" 
                          rotate 10



                          should work with the command line



                          /usr/sbin/logrotate -s $HOME/backups.state -f $HOME/backups.config






                          share|improve this answer








                          New contributor




                          djs is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.









                          share|improve this answer



                          share|improve this answer






                          New contributor




                          djs is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.









                          answered 49 mins ago









                          djs

                          1




                          1




                          New contributor




                          djs is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.





                          New contributor





                          djs is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.






                          djs is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.



























                               

                              draft saved


                              draft discarded















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f480898%2fkeep-only-a-certain-amount-of-backups-tarballs-in-a-directory%23new-answer', 'question_page');

                              );

                              Post as a guest













































































                              Popular posts from this blog

                              How to check contact read email or not when send email to Individual?

                              Displaying single band from multi-band raster using QGIS

                              How many registers does an x86_64 CPU actually have?