Skip to content

Instantly share code, notes, and snippets.

@obrassard
Last active March 17, 2020 03:58
Show Gist options
  • Save obrassard/708ee07cc15161c91069680dbbd3aa36 to your computer and use it in GitHub Desktop.
Save obrassard/708ee07cc15161c91069680dbbd3aa36 to your computer and use it in GitHub Desktop.
Simple script to backup a MySQL database to an S3 bucket

Introduction

This is a simple script to backup a MySQL database to an Amazon S3 bucket or Digital Ocean Space.

It uses mysqldump alongside with s3cmd.

Installation

  1. Setup s3cmd before running the script

To use DigitalOcean Spaces check this : https://www.digitalocean.com/docs/spaces/resources/s3cmd/

  1. Copy the script to your server
wget https://gist.github.com/obrassard/708ee07cc15161c91069680dbbd3aa36/raw/backup-s3-db.sh
  1. Make it executable
cmod +x backup-s3-db.sh
  1. Run it
./backup-s3-db.sh <database-name> <s3-bucket-name>
#!/bin/bash
db=$1
s3bucket=$2
filename=$db.$(date +%F.%H%M%S).sql.gz
filepath=~/backups/$filename
echo "Backuping MySQL Database $db ..."
mkdir -p ~/backups
/usr/bin/mysqldump -u root $db | gzip > $filepath
echo "Uploading backup to S3 Storage ..."
s3cmd put $filepath s3://$s3bucket/$db/$filename
echo "Cleaning temporary files..."
rm $filepath
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment