The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

delete user made backups at some interval

Discussion in 'Data Protection' started by bhanuprasad1981, May 16, 2010.

  1. bhanuprasad1981

    bhanuprasad1981 Well-Known Member

    Joined:
    Aug 5, 2008
    Messages:
    222
    Likes Received:
    0
    Trophy Points:
    16
    Location:
    India
    is there any option / script which will automatically search / delete user made backups by cpanel after x days ? i see users make backups and forget to remove them wasting server space

    thanks
     
  2. Infopro

    Infopro cPanel Sr. Product Evangelist
    Staff Member

    Joined:
    May 20, 2003
    Messages:
    14,478
    Likes Received:
    203
    Trophy Points:
    63
    Location:
    Pennsylvania
    cPanel Access Level:
    Root Administrator
    Twitter:
    None that I know to remove backups from a users space. Nagging sometimes helps, explaining that when they download a new system generated backup that the old backup is going to be included and result in a much larger backup sometimes helps, explaining they have run out of space and that's why the account was automagically suspended by your system while you were at a long lunch and not getting emails from them complaining that the site is down? Works a treat. ;)

    HTH
     
  3. bhanuprasad1981

    bhanuprasad1981 Well-Known Member

    Joined:
    Aug 5, 2008
    Messages:
    222
    Likes Received:
    0
    Trophy Points:
    16
    Location:
    India
    i have seen at hostmonster cpanels backup wizard option a note was written "backups made by this script will be automatically deleted in 3 days " so i think they made some custom script ?
     
  4. rrwh

    rrwh Well-Known Member

    Joined:
    Oct 2, 2004
    Messages:
    48
    Likes Received:
    0
    Trophy Points:
    6
    This is a trivial matter to deal with.

    A fairly simple shell script could deal with this easily.

    I would suggest reading about the find command.

    the backup files have a specific naming convention that makes it very easy.

    Eg, lets say that the backup file name is

    backup-5.21.2010_10_55-20_username.tar.gz

    All you need to do is with some simple commands is to create the leading part of the filename that includes the date (x number of days ago)

    Then, go ahead and use the find command to look for such files within the /home directory.

    something like
    $daysago=5.21.2010
    find /home -name "backup-$daysago*tar.gz" >logfile

    You could either manually act on it - by informing users or further automate deleting all files that are logged.

    I have given you about 2 parts of a 7 part puzzle, but it demonstrates that what you are wanting to do is trivial and something that I could implement faster than actually writing this response to you.

    To tell the truth, asking such a trivial system admin question means you should not be managing a system yourself - you should be paying someone who is competent to manage it for you.
     
  5. Spiral

    Spiral BANNED

    Joined:
    Jun 24, 2005
    Messages:
    2,023
    Likes Received:
    7
    Trophy Points:
    0
    On the contrary, it is not "trivial" by any means ...

    The problem with blindly using the find command is that if someone were to setup a hard symlink to their account with the name backup-*.tar.gz then you would unknowingly delete whatever they symlinked to using find to locate and remove the files.

    Fortunately, the long directory view will show you which files are stand alone and which files have been symlinked in some manner hard or soft so it is not that difficult to script against this information. (See information on standalone "1" column in long directory listing)

    It won't help in what I just said above, but at barebones minimum I would at least put a "-type f" with that find commend you mentioned and perhaps do something more like the following to match the user's question more specifically:
    Code:
    find /home -maxdepth 2 -type f -mtime +3 -name 'backup-*.tar.gz'
    
    (This returns all backup files explicitly in the root of the user's account or under their /public_html that are more than 3 days old)

    I actually do have a script that takes a lot more of all of this into consideration plus the additional hard link issue I mentioned and can give all of you a copy of that if that would be of some use. I'll have to go dig it up our of my archives and can post it here later maybe after I actually get a cup of coffee and put my contacts back in so I can see. ;)
     
  6. Spiral

    Spiral BANNED

    Joined:
    Jun 24, 2005
    Messages:
    2,023
    Likes Received:
    7
    Trophy Points:
    0
    Hi Everyone:

    Here is a copy of the script I was talking about earlier:

    You set 'KEEP' to the maximum days you want to keep user backups
    and then it wipes out "backup-*.tar.gz" files older than that and also logs the deletions to your server logs.

    Code:
    #!/bin/bash
    IFS="$"
    
    ### SET THE FOLLOWING TO THE MAXIMUM DAYS TO KEEP USER BACKUPS
    typeset -i KEEP=3
    
    ########################################
    ##  Cleanup User Backup Script        ##
    ########################################
    TFILE="/tmp/kill-backup.lst.$$"
    typeset -i NFLAG=0
    
    if [ -e "${TLIST}" ]; then
       /bin/rm -f -- "${TLIST}"
    fi
    
    /bin/touch -- "${TLIST}"
    /bin/chmod 0600 -- "${TLIST}"
    
    cd /home
    
    find /home -maxdepth 2 -type f -mtime +${KEEP} -name 'backup-*.tar.gz' > "${TLIST}"
    
    /bin/cat -- "${TLIST}" | /bin/grep -v "\`" | while read TARGET; do
        if [ -f "${TARGET}" ]; then
           typeset -i NFLAG=$(ls -l -- "${TARGET}" | awk '{print $2}')
           if [ ${NFLAG} -eq 1 ]; then
              /bin/rm -f -- "${TARGET}"
              echo "$(date +"%b %d %T") $(hostname | cut -d'.' -f1) clean[$$]: '${TARGET}' was removed as expired." >> /var/log/messages
           fi
           unset NFLAG
        fi
    done
    
    if [ -e "${TLIST}" ]; then
       /bin/rm -f -- "${TLIST}"
    fi
    
    ## END OF SCRIPT
    
     
  7. Spiral

    Spiral BANNED

    Joined:
    Jun 24, 2005
    Messages:
    2,023
    Likes Received:
    7
    Trophy Points:
    0
    I made a typographical error copying the script above ...

    The following line:
    Code:
    TFILE="/tmp/kill-backup.lst.$$"
    Should actually read:
    Code:
    TLIST="/tmp/kill-backup.lst.$$"
    Enjoy ;)
     
Loading...

Share This Page