Remove cPanel users’ backups

The filename format of the backups is backup-date_time_username.tar.gz. Example: backup-6.11.2017_09-52-35_philmorehost.tar.gz
The issue is that these backups take disk space and at some point, you may see that your server is low on disk space. So, it’s a good idea to remove these backups from time to time.

To remove backups for a single account, just navigate to the user’s directory and remove them with the command:

rm /home/username/backup-*.tar.gz

Usage example:

root@web [/]# root@web [/]# cd home/philmorehost root@web [/home/philmorehost]# ls backup-*.tar.gz backup-6.11.2017_09-52-31_philmorehost.tar.gz  backup-6.11.2017_09-52-35_philmorehost.tar.gz root@web [/home/philmorehost]# rm /home/philmorehost/backup-*.tar.gz rm: remove regular file '/home/philmorehost/backup-6.11.2017_09-52-31_philmorehost.tar.gz'? y rm: remove regular file '/home/philmorehost/backup-6.11.2017_09-52-35_philmorehost.tar.gz'? y root@web [/home/philmorehost]# 

To find and list all the backups from users’ directories:

find /home/* -maxdepth 1 -type f -name 'backup-*.tar.gz'

To find and delete all the backups from users’ directories:

find /home/* -maxdepth 1 -type f -name 'backup-*.tar.gz' -delete

To find and list all the backups from users’ directories which are older than 5 days:

find /home/* -maxdepth 1 -type f -mtime +5 -name 'backup-*.tar.gz'

To find and delete all the backups from users’ directories which are older than 5 days:

find /home/* -maxdepth 1 -type f -mtime +5 -name 'backup-*.tar.gz' -delete

You can create a con job for this task.
1.Enter the cron job editor:

crontab -e

2. Add the cron job (this job will run every day at 04:00 and will delete all backup archives older than 5 days)

* 4 * * * find /home/* -maxdepth 1 -type f -mtime +5 -name 'backup-*.tar.gz' -delete

3. Exit (with saving the changes) the cron job editor.

4. Check if the cron job was added successfully with the command (it will list all cron jobs):

crontab -l


Originally posted on January 10, 2019 @ 10:28 am

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.