Please whitelist cPanel in your adblocker so that you’re able to see our version release promotions, thanks!

The Community Forums

Interact with an entire community of cPanel & WHM users!

Auto Delete Backup Directory Before New One Generated

Discussion in 'Data Protection' started by Dave Peterston, Oct 17, 2015.

  1. Dave Peterston

    Dave Peterston Registered

    Oct 17, 2015
    Likes Received:
    Trophy Points:
    cPanel Access Level:
    Root Administrator
    Hi cPanelers,

    I hope I am not bothering anyone by asking this.

    I need to delete a backup directory older than 2 days before a new one is generated. I run a small business transporting PDF files and need to get the backup directory deleted off the server after 2 days.

    Running with the default cpanel system means I need space to hold an additional backup directory which is only going to be deleted anyway after the new one is saved. That's a huge waste of space. I need the oldest backup directory deleted just before the new one is generated.

    I have this in the crontab on my server:

    30 2 * * * find /backup -type d -ctime +2 -exec rm -rf {} \;

    This deletes the daily backup directory older than 2 days at 2:30am. It seems to work OK.

    However, recently the backup process got stuck resulting in the backups not working for about four days I think. The result was the cronjob kept on deleting backup directories older than 2 days so I was left with no backups, and to top is off it ended up deleting the main "/backup" directory too!! Doh!!!

    I am after something like this:

    ... At 2:30am delete the daily backup directory older than 2 days old but only if there is a daily backup directory in there older than 1 day old.

    I have done a heap of research but I have not yet found anything.

    If anybody knows the right code that would be awesome or is there is a better way.

    Other threads on this subject seem to tell people to look at the cpanel 'hooks' which is way beyond me but a cronjob code which can be pasted in to the crontab is super easy.

    Any help would be totally awesome.

    Thanks heaps
  2. cPanelMichael

    cPanelMichael Technical Support Community Manager
    Staff Member

    Apr 11, 2011
    Likes Received:
    Trophy Points:
    cPanel Access Level:
    Root Administrator
    Hello :)

    Have you considered using incremental backups? You can choose this setting to store only one uncompressed backup file. The system saves updated account information to the existing backup file, and removes the old information that these updates replace. This setting limits your restoration settings, but runs faster and uses less disk space than other backup types.

    Thank you.
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. chalupa

    chalupa Member

    Jul 15, 2014
    Likes Received:
    Trophy Points:
    cPanel Access Level:
    DataCenter Provider
    Would be better to just remove the .tar.gz files and let cPanel remove the directory itself. Most of the space is taken up by them anyways

    30 2 * * * find /backup -type f -ctime +2 -name '*.tar.gz' -exec rm -rf {} \;

    But the cPanel backups will run after this by default. 1am for legacy and 2am for new backups. So you want that to run earlier than the backups fire? Maybe

    30 1 * * * find /backup -type f -ctime +2 -name '*.tar.gz' -exec rm -rf {} \;

    or if you use legacy backups

    30 0 * * * find /backup -type f -ctime +2 -name '*.tar.gz' -exec rm -rf {} \;

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice