Backing up WordPress has become something of a niche market in the plugin realm of the WP ecosystem. But there’s no need to bog down your Raspberry Pi even more when you can back up your site and database natively (for Ghost too).

First – Create a backup bash script

First, create a bash script (.sh) in /home/user/ (i.e. /home/pi/) directory that will do two things:

  1. Compressed the entire site or directory using tar and save a copy; and
  2. Execute an mysqldump of the WordPress or Ghost database.

Both the tar.gz file and the database.sql output will be saved with a timestamp appended to the filename. Create the bash script using your preferred editor (I use nano for my examples) and named it as backupmysite.sh:

sudo nano backupmysite.sh

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

You need to remember to replace username and password with your actual database access credentials, and replace database_name with actual database name you used. Your /home/user/ with the actual user directory you used (e.g. mine is /home/pi since I’m useing pi as my user).

Save the file with a Control+X, Y, and Enter and make the file executable with:

chmod +x /home/user/backupmysite.sh

You may or may not have to change the ownership of the file depending on your user permission.

You can execute /home/user/backupmysite.sh to ensure the script will be run properly with:

./backupmysite.sh

We’ll now setup a schedule so that it will backup the data automatically according to our preset schedule.

Step 2 – Setup a cron entry

Cron is one of several Linux tools for configuring scheduled tasks. Run the following command to edit the cron table:

crontab -e

The first time you run crontab you’ll be prompted to select a text editor; choose nano.

A cron entry consists of six parts: minute, hour, day of month, month of year, day of week, and the command to be executed. Here’s a little graph:

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 input a cron entry as show below:

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

This cron entry would run our backupmysite.sh script every Sunday at 0300. You can adjust the frequency of backup based on your needs, but once a week is sufficient enough for my little sites. Save the file and quit the editor (Control + X, Y, and Enter).

You can view your currently saved schedule(s) with:

crontab -l

Step 3 – Download the backup to another computer

By now you have a working backup solution that run once a week and save the files at you /home/user/ directory. You could download the backup files regularly (weekly, monthly at your wish) to your local computer using any SFTP or FTP client. I use Syncthing to back mine up to another sever.

There you have it, a simple WordPress backup solution in 3 steps. And thanks to e-tinkers for the original post that I’ve updated for my own use (at the time of writing this his site was down).

Categories: Blogging

Leave a Reply

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