Skip to content

Instantly share code, notes, and snippets.

@mattbloomfield
Created April 2, 2021 12:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattbloomfield/2162d281063a72afa2942e35e88f04a6 to your computer and use it in GitHub Desktop.
Save mattbloomfield/2162d281063a72afa2942e35e88f04a6 to your computer and use it in GitHub Desktop.
A shell script for automating backups from CraftCMS to Amazon S3
#!/bin/sh
set -e
SITE_ALIAS="arbitrary_name" # in case of multiple
# The amazon S3 bucket to save the backups to (must already exist)
S3BUCKET="my-craftcms-backups"
# Optionally specify bucket region
S3BUCKETREGION="us-east-1"
# Local backup directory (must exist, requires trailing slash)
BACKUPDIR="/mnt/overflow/craft_cms_backups/${SITE_ALIAS}/"
# Add a date and unique string to the filename
BACKUPDATE=$(date +%Y%m%d%s)
# This sets the proper file extension
EXTENSION="sql.gz"
# Credentials for Platform.sh & Database
SSH_USER=mysshuser
SSH_HOST=myhost.com
USER=xxxx
PW=xxxx
HOST=127.0.0.1
DB_NAME=db_name
mkdir $BACKUPDIR
echo "Pulling the backup"
ssh ${SSH_USER}@${SSH_HOST} "mysqldump -h ${HOST} -u ${USER} -p${PW} ${DB_NAME} | gzip -9" > $BACKUPDIR$SITE_ALIAS.$BACKUPDATE.$EXTENSION
echo "Syncing to S3"
if [ -z "${S3BUCKETREGION}" ]; then
aws s3 sync $BACKUPDIR s3://$S3BUCKET
else
aws s3 sync $BACKUPDIR s3://$S3BUCKET --region $S3BUCKETREGION
fi
rm -rf $BACKUPDIR/*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment