Backing Up WordPress in 3 Simple Steps

Backing up your WordPress site and its database is critically important, yet it is very often completely overlooked. Here is a simple way to back up your WordPress site and database in 3 easy steps, with no additional plugins required.

Most hosting providers offer backup and archiving services, but if your site is self-hosted and self-managed, you will need to set up a backup mechanism yourself.

If you search Google for “how to back up a WordPress site”, you will be overwhelmed by articles and so-called tutorials suggesting you use some kind of WordPress plugin. Most plugins are slow because they run in PHP rather than relying on system-level services. Many of them are also unnecessary, heavy and at times unreliable. This article explains how to create a backup solution with a simple bash script and a crontab entry.

1 – Create the bash script

First, create a simple bash script in the /home/pi/ directory that will do two things:

  1. Compress the entire site using tar and save a copy;
  2. Run a mysqldump of the WordPress database.

Both the tar.gz file and the database.sql file will be saved with a timestamp appended to the file name.
Create the bash script using your favourite editor and call it backupmysite.sh

sudo nano /home/pi/backupmysite.sh

inside the file, enter the following:

#!/bin/bash
# Backup my website data
#START
  tar -czf /home/pi/wordpress-$(date +%Y%m%d).tar.gz /var/www/html/*
  mysqldump -u username -ppassword database_name > database_name_$(date +'%Y%m%d').sql
#END

Of course, replace username and password with your WordPress database credentials, and database_name with the name of your WordPress database.
the backup will be saved to /home/pi; change this if you prefer to save it elsewhere (for example on a NAS).

Save the file with CTRL+X and press ENTER to confirm.
Now make the script executable with the following command:

sudo chmod +x /home/pi/backupmysite.sh

Now all that’s left is to test that it works: simply run the script and check that it creates the backup files.

Next, we need to set up a schedule so that the backup runs automatically.

2 – Schedule it with cron

Cron is a utility for configuring scheduled tasks on Unix systems.
Here you can set up schedules to run commands or scripts periodically at fixed intervals.
Run the following command to edit the cron table:

crontab-e

If this is the first time you have run crontab, you will be asked to select a text editor; personally, I prefer nano.

A cron entry consists of six parts: minute, hour, day of month, month of year, day of week and the command to run.

# m h  dom mon dow   command
# * * * * *  command to execute
# ┬ ┬ ┬ ┬ ┬
# │ │ │ │ │
# │ │ │ │ │
# │ │ │ │ └───── day of week (0 - 7) (0 to 6 are Sunday to Saturday, Sunday is 0 or 7)
# │ │ │ └────────── month (1 - 12)
# │ │ └─────────────── day of month (1 - 31)
# │ └──────────────────── hour (0 - 23)
# └───────────────────────── min (0 - 59)

Move the cursor to the end of the file to add a cron entry as shown below:

0 3 * * 0 /home/pi/backupmysite.sh

This cron entry runs the /home/pi/backupmysite.sh script every Sunday at 03:00. You can adjust the backup frequency to suit your needs, but once a week is enough for my site. Save the file and close the editor with CTRL+X, then press ENTER to confirm.

3 – Download the backup from another computer

You now have a working backup solution that runs once a week and saves the files in the /home/pi/ directory. You can download the backup files regularly (weekly, or monthly if you prefer) to your local computer using any SFTP or FTP client.

You can skip this last step if you have set the backup file to be saved to a network location.
I’d also like to point you to another article where I explain how to automatically map a network drive on a Raspberry Pi.

Enjoy!

Leave a Comment

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

Scroll to Top