Rotating backup my self hosted wordpress sites with a bash script and a cron job

Featured Snippet: Automating your self-hosted WordPress backups is crucial for disaster recovery. By combining a simple Bash script that runs database dumps and file compression with a Linux cron job, you can create a reliable, rotating weekly backup system. This guarantees that your latest data is always safe without requiring manual intervention, protecting your platform from hacks and server crashes.

Getting hacked sucks! I have never been able to recover from the last hack that I suffered. It is an extremely frustrating experience that results in lost traffic, broken trust, and vanished content. So Disaster Recovery is a priority for any serious website administrator. The last couple of days I've completely rebuilt my blog on my own dedicated Linux server using a high-performance stack with NGINX, PHPFPM, and MySQL.

Rather than relying on bloated third-party plugins that can slow down my site or lock my archives behind expensive subscription paywalls, I decided to handle everything natively at the server level. Today I went ahead and created 4 folders, one for each week of the month, assuming roughly 4 weeks per month. This allows me to maintain a historical archive of my entire web presence without consuming infinite disk space.

How to Create the Rotating Bash Script for WordPress Backups

A bash script allows us to execute a sequence of terminal commands in a single automated step. In this setup, we compress our web directories and dump the MySQL database into designated weekly folders. This ensures that both the physical media files and the dynamic database content are perfectly synchronized in time.

I wrote a quick backup.sh script, and set up the logic to cycle through four weeks. This prevents disk space exhaustion while giving us multiple restoration points.

cd "$(dirname "$0")"
value=$(cat count)
value=$(($value%4 + 1))
echo $"tar -czf /opt/backups/week$value/www.tgz /var/www"
tar -czf /opt/backups/week$value/www.tgz /var/www
echo $"mysqldump --all-databases | gzip > /opt/backups/week$value/data.gz"
mysqldump --all-databases -u root -p$MYSQL_PASSWORD | gzip > /opt/backups/week$value/data.gz
echo $value > count

This script reads from a simple text file in order to keep count of which week it currently is. So you will need to initialize that count file in the same directory by creating a file named 'count' containing the number 0. Once running, the script automatically increments this value and loops back around after week four.

Scheduling the Automation with a Linux Cron Job

With the Bash script properly configured, we need a mechanism to execute it automatically. Linux provides a highly reliable built-in scheduler known as a cron job. Setting it up is straightforward and eliminates the risk of human error from forgetting to run manual backups.

First, I used this command to install the cron job and open the configuration editor:

crontab -e

Then, I used this specific syntax to make it run every Thursday at 2 am, which is typically a low-traffic period for my server. Scheduling heavy tarball compression and database dumps during off-peak hours ensures that visitors do not experience performance degradation.

# m h dom mon dow command
0 2 * * 4 /opt/backups/backup.sh

Validating Your Disaster Recovery Plan

The next step is actually testing the Disaster Recovery (DR) process by simulating a full server disaster and recovery scenario. A backup is only as good as its restore capability. Always extract your archives periodically and restore the MySQL database on a local staging environment to verify data integrity. Far too many administrators assume their bash script is working perfectly only to discover corrupted archives months later.

Watch the full video tutorial below on testing the restoration process and ensuring your WordPress blog is fully protected from unexpected hardware failures, malicious injections, and accidental deletions:

Watch WordPress Backup Strategy Video

Frequently Asked Questions

Why do I need a rotating backup strategy for my WordPress site?

A rotating backup strategy ensures you have multiple points in time to recover from. If your site gets hacked or a file becomes corrupted, you can easily restore an earlier version of your WordPress installation and MySQL database without losing everything. Overwriting a single backup file every day is dangerous because you might accidentally back up a hacked site.

What is a cron job and how does it help with WordPress backups?

A cron job is a time-based task scheduler in Unix-like operating systems. It allows you to automate repetitive tasks, such as executing a Bash script to archive your WordPress files and database automatically at scheduled intervals, eliminating the need for manual backups and saving significant time.

Is backing up the database enough for WordPress?

No, backing up just the MySQL database is not sufficient. You must also back up your physical web directory which contains your customized theme files, uploaded media images, and installed plugins. The customized Bash script provided in this guide handles both the database dump and file compression seamlessly in one execution block.