Delete old backups
Our database and files backup scripts write a new set of archives every time they run, and nothing ever removes the old ones. In this lesson we'll apply a retention policy and write a small script to delete everything older.
We'll look at how much disk space our backups take up, then use find to remove
the ones we no longer need to keep.
Disk space
A site with a 200 MB archive and a 50 MB database dump adds about a quarter of a gigabyte of backups every day. Multiply that by the number of sites on your server and the number of days you'd like to keep, and it becomes clear why a retention policy is needed early on.
Running out of disk space causes all sorts of problems on a WordPress server. Uploads stop working, MariaDB can refuse writes, and logging stops. It's also an annoying problem to fix remotely, so it's much better to avoid it.
Retention
We settled on 14 days of retention in the backup strategy lesson, so anything older than that can go. The find utility can take care of this in a single command:
find /backups -type f -name '*.gz' -mtime +14 -delete
The -type f option limits this to files, -name '*.gz' matches both our
.sql.gz dumps and our .tar.gz archives, and -mtime +14 selects the ones
that were last modified more than 14 days ago. We never touch our backups after
writing them, so their modification time is also the time the backup was taken.
Note that this walks the whole /backups directory, so it covers every site at
once, and there's no need to loop through them like we did in the previous two
lessons.
The prune script
There isn't much to this one:
KEEP=14
echo "Removing backups older than $KEEP days"
find /backups -type f -name '*.gz' -mtime +$KEEP -print -delete
The -print option lists each file as it's removed, which will give us
something useful to look at once these scripts are running from cron.
I called this script prune-backups.sh and placed it in the /config/bin/
directory next to the other two. You can also find it in the config
repository on GitHub.
The script needs to run as root to delete files in /backups, so to invoke it
as my karl user I'll need sudo:
sudo /config/bin/prune-backups.sh
ls -lh /backups/uncached.org/
I prefer to keep this separate from the backup scripts. Retention lives in one place instead of two, and the script can be tweaked and run on its own whenever disk space is running low.
Testing before deleting
Before running this for the first time, leave out the -delete and have a look
at what find picks up:
find /backups -type f -name '*.gz' -mtime +14
Everything listed here is what the script would remove. If the list is longer than you expect, or contains files you don't recognize, it's worth understanding why before running the script itself.
When backups stop
Deleting by age assumes that new backups keep arriving. If our backup scripts stop running for some reason, this script will carry on deleting until there's nothing left.
This article is for premium members only. One-time payment of $196 unlocks lifetime access to all existing and future content on wpshell.com, and many other perks.