Skip to content

Instantly share code, notes, and snippets.

@chrisgrabinski
Last active June 7, 2022 08:12
Show Gist options
  • Save chrisgrabinski/37d6ed3dac344fe6b673dcfc8669544c to your computer and use it in GitHub Desktop.
Save chrisgrabinski/37d6ed3dac344fe6b673dcfc8669544c to your computer and use it in GitHub Desktop.
WordPress S3 Backup Bash Script
# WordPress S3 Backup Script
# ---------------------------------------------------------------------------------
#!/bin/bash
bucket="s3://" # Your S3 Bucket
if [ -z "$1" ]
then
path=$PWD
else
path=$1
cd $path
fi
if $(wp core is-installed);
then
archive="$(date +%Y%m%d-%H%M%S)-${PWD##*/}.tar.gz"
dbname=$(wp eval 'echo DB_NAME;')
# Create database dump for backup
echo "🗃 Creating database dump…"
wp db export $dbname.sql --quiet
# Create tar archive for backup
echo "📦 Packaging backup files…"
tar czf $archive $dbname.sql wp-content/themes/ wp-content/plugins/ wp-content/uploads/
# Send tar archive to S3
echo "🛰 Sending backup to S3…"
aws s3 cp $archive $bucket/${PWD##*/}/$archive --quiet
# Remove backup files from local machine
echo "🗑 Deleting backup files from local machine…"
rm $archive $dbname.sql
echo "🚀 Backup successful."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment