postcpbackup – auto cpbackup rotation

by brian on January 31, 2010

This script that rotates cpanel backups after they are completed. You can set it to keep N copies of your daily, weekly and monthly backups. It has been in production use since Sept 2008 on multiple servers, and has been in use around the net since it was published early in 2009.

The script is run after a cpbackup run completes successfully. The script rotates daily, weekly and monthly copies keeping N copies of each. You can specify N separately for daily, weekly and monthly through keepdaily/keepweekly/keepmonthly variables, and no copies are made if those variables are set to empty or not set. For instance, the following would keep two copies of daily backups and 3 weeks of weekly and no monthly backups:

keepdaily=2
keepweekly=3
keepmonthly=

The settings shown here keep 2 copies of the daily backup, 3 copies of the weekly backup, and no monthly backups (ie just the last one).

You can put these settings in the script itself (not recommended) or in a file /backup/cpbackup/.postcpbackup – we recommend putting the settings in the setting file so you can keep a single copy of the script the same across multiple servers.

The script is run from /scripts by cpbackup (the cPanel backup program) itself when it completes, so there is little risk of it rotating a backup unless backups have completed. [One day I should add a further sanity check that the backups were done OK to prevent false rotations, but haven’t yet]

To install the script, simply copy it to /scripts/postcpbackup and make it mode 755 – it will then be called when cPanel’s cpbackup process completes. Since it is run from cpbackup, adding it to crontab explicitly will cause a world of problems – just putting it in /scripts is sufficient for it to run.  Note – there is no need to edit cpanel scripts, as the backup script looks for a file called  “/scripts/postcpbackup” and, if it finds it exists, runs it at completion of the backup.

cPanel now requires you to go into WHM –> “Configure Backups” and down the bottom under “Execute Pre/Post Backup Script” tick the “postcpbackup” checkbox.

 

Note: This solution rotates your backups and thus won’t keep old account backup data, as cPanel does.  cPanel’s backup strategy only keeps one monthly, weekly and daily backup but doesn’t delete old backups, in case you need them.  We recommend you use our prekillacct script in tandem with this script, as that backs up an account as it is deleted and keeps the backup indefinitely in a separate directory away from backup rotation.

This has been posted at forums.cpanel.net a few times (first being early 2009):  post1  post2

[download id=”4″]

This script works nicely with the cprevert command (one command file restore).

Originally released: 12 Feb 2009

Next post: