Skip to content

Instantly share code, notes, and snippets.

@llipe
Last active September 28, 2021 22:12
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 llipe/fa1ff842520d9d0b1c4382c39405650b to your computer and use it in GitHub Desktop.
Save llipe/fa1ff842520d9d0b1c4382c39405650b to your computer and use it in GitHub Desktop.
Wordpress backup script that stores a snapshot of files & database on S3
#!/bin/bash
# This script requires wp-cli to work and aws cli.
# WP CLI: https://wp-cli.org/
# AWS cli: https://aws.amazon.com/es/cli/
# Usage: wordpress-backup.sh <domain-name> <s3 bucket> <wp basepath>
if [ -z "$1" ]
then
echo "Domain name missing"
echo "Usage: wordpress-backup.sh <domain-name> <s3 bucket> <wp basepath>"
exit 1
fi
wp_url=$1
if [ -z "$2" ]
then
echo "S3 bucket missing"
echo "Usage: wordpress-backup.sh <domain-name> <s3 bucket> <wp basepath>"
exit 1
fi
s3_bucket=$2
if [ -z "$1" ]
then
echo "Wordpress basepath missing (fullpath)"
echo "Usage: wordpress-backup.sh <domain-name> <s3 bucket> <wp basepath>"
exit 1
fi
wp_basepath=$3
cd $wp_basepath
date=$(date '+%Y-%m-%d-%H%M%S')
filesname="wp-backup-files-$date.tar.gz"
sqlname="sql-dump-$date.sql"
backupname="$wp_url-backup-$date.tar.gz"
echo "Compressing files..."
tar -zcf $filesname .
echo "Exporting db..."
wp db export $sqlname
echo "Creating backup file $backupname"
tar -zcvf $backupname $filesname $sqlname
echo "Uploading $backupname to s3://$s3_bucket/"
aws s3 cp $backupname s3://$s3_bucket/
echo "Done. Bye!"
# Cleanup
rm $filesname
rm $sqlname
rm $backupname
@llipe
Copy link
Author

llipe commented Sep 28, 2021

Usage: from crontab
5 9 * * 0 cd /path/to/backupscript/ && ./wordpress-backup.sh <domain.cl> </path/to/wordpress/public/>

Example
5 9 * * 0 cd /home/ubuntu/public_html/domain.cl && ./wordpress-backup.sh domain.cl cl.domain.backups /home/ubuntu/public_html/domain.cl/public/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment