WordPress backup & sync to Backblaze B2 Cloud Storage Makefile recipe
This recipe is a proof-of-concept to automate backing up and uploading to a remote destination set up as a cronjob. It will create a backup of your WP app, sync it to a Backblaze B2 bucket, add a basic retention policy, and clean any old local backups.
- Root access. You could do it with sudo access, but you won't be able to run this as a cron job.
- LAMP/LEMP stack with Bash shell installed.
- WP CLI is used to export the DB and generate filenames among other actions.
- Backblaze B2 key and bucket(s) set up & configured to sync your files.
Put this in a folder like /root/cron/
or home/user/webapps/
if you just want to run it manually.
$ git clone https://github.com/lopadz/wp-backup-to-b2
The script can be called from anywhere as long as you pass the correct path to where the Makefile is located. See below for instructions.
Duplicate apps/app-example.mk
and configure the settings for the app you want to backup and sync.
- Rename the file to the name of your app to keep things organized.
- Pay attention to the paths and comments. The variables are pretty self explanatory, but see "App Settings & Options" below for more details.
To run the backup.mk
recipe, it requires:
- The absolute path where the makefile is located. This is helpful so that you can run it as a cron job or run it manually from anywhere.
- The
APP
variable with the relative path (relative to thebackup.mk
file) of theapp-name.mk
file with all the configured settings of the app you want to backup. There's no need to add the ".mk" file extension. - The
FREQ
variable which tells the script what kind of frequency the backup will have. See "App Settings & Options" below for more details.
Here's an example of a weekly backup run:
$ make -f /root/cron/backup.mk APP=apps/app-name FREQ=weekly
backup.mk
: Main recipe to start the backup and sync process.apps/app-example.mk
: These are the settings (per app) thebackup.mk
recipe needs in order to create a backup and sync to a B2 bucket.utilities.mk
: Defines colors and variables needed for naming files/directories.
-
FREQ
= It tells the script what kind of backup frequency this will be. This affects the name of the directory where the backup will be saved and the retention policy for local and remote backups. Valid values are 'daily', 'weekly', 'monthly', and 'yearly'. If nothing is set or empty, it will default to 'daily'. -
LOG_DIR
= Name of the directory where the logs are saved. -
DB_PREFIX
&CODE_PREFIX
= Prefix added to the filename to the database and codebase respectively. -
CLEANUP_OLD_BACKUPS
= If set to true, the script will find the backups created between now and:- 8 days in the past for backups with a value of
FREQ=daily
- 31 days in the past for backups with a value of
FREQ=weekly
- 91 days in the past for backups with a value of
FREQ=monthly
- 366 days in the past for backups with a value of
FREQ=yearly
Once found, it will delete the local backups before syncing to the B2 bucket for a simple retention policy. You can customize these values by commenting out the
DELETE_...
variables and updating as needed. - 8 days in the past for backups with a value of
-
B2_SYNC
= If set to false, this won't sync the backups to the B2 bucket. It defaults totrue
.s -
B2_KEEP_...
= These are the days that are passed in the --keepDays flag when syncing to the B2 bucket. -
DELETE_...
vsB2_KEEP_...
= The main difference is one affects local backups, while the other one affects the retention policy of the B2 bucket.
Paths in the apps/app-example.mk
file resemble an app running on:
- Bedrock by Roots
- RunCloud.io server control management
If you are not running these, simply update the path structure to match your host and app paths.