509.949.2162 jeremy@bondbyte.com

Here’s a handy shell script to get the backup part going. We will need to create the script inside the Docker WordPress Front End and another on the WordPress Docker Backend. We will need to use docker cp on the host to copy the backup files out of the container. Then we can ball and zip the backup and transfer to another location, offsite.

https://theme.fm/a-shell-script-for-a-complete-wordpress-backup/

Here’s my example of the script. I changed a few things to make the restore process more predictable.

#Backup Script
#!/bin/bash

# Find and Replace these Values. I will roll this up into a better bash script later. Promise.
#-------------------------------------------#
#-Find Value------Replace Value Sample------#
#-------------------------------------------#
# backupname 	= precisionpaving
# sitename  	= html
# tobackpath  	= /home/sudouser/backup
# dbuser	= root
# dbpass	= password
# dbname	= wp_precisionpaving
# dbfilename	= database


#Script Starts Here
NOW=$(date +"%Y-%m-%d-%H%M")
FILE="backupname.$NOW.tar"
BACKUP_DIR="tobackpath"
WWW_DIR="/var/www/sitename/"

# MySQL database credentials
DB_USER="dbuser"
DB_PASS="dbpass"
DB_NAME="dbname"
DB_FILE="dbfilename.sql"

# Tar transforms for better archive structure.
WWW_TRANSFORM='s,^home/sudouser/www/sitename,www,'
DB_TRANSFORM='s,^home/sudouser/backups,database,'

# Create the archive and the MySQL dump
tar -cvf $BACKUP_DIR/$FILE --transform $WWW_TRANSFORM $WWW_DIR
mysqldump -u$DB_USER -p > $BACKUP_DIR/$DB_FILE

# Append the dump to the archive, remove the dump and compress the whole archive.
tar --append --file=$BACKUP_DIR/$FILE --transform $DB_TRANSFORM $BACKUP_DIR/$DB_FILE
rm $BACKUP_DIR/$DB_FILE
gzip -9 $BACKUP_DIR/$FILE

I use this quick template to spin up some shell scripts. Then the backups can be scheduled with a chron job. More on that later tho.