Skip to content

Instantly share code, notes, and snippets.

@ravibhure
Created October 14, 2014 06:54
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 ravibhure/f4f3fe224a43a8f4e57d to your computer and use it in GitHub Desktop.
Save ravibhure/f4f3fe224a43a8f4e57d to your computer and use it in GitHub Desktop.
Back up, compress and encrypt a small MySQL Database
#!/bin/bash
# Author: Ravi Bhure <ravibhure@gmail.com>
# Date: 13/10/2014
# This script dumps a "orders" database, compresses, encrypts and timestamps it, then saves it to netapp storage local mount. Ideal for a cronjob.
# It uses symmetric encryption, so guard your password carefully.
#
# NOT RECOMMENDED FOR LARGE DATABASES!
# Set your variable..
my_server=db.example.com
storage_server=backups.example.com”
# Exit me if encryption password file not found
if [ ! -f ~/.db_encr_pass.txt ] ; then
echo "db encryption password file not found"
exit 1
fi
# Dump a MySQL database "orders"
mysqldump -h $my_server -u root orders > /tmp/orders.sql
# Compress: -9 means best, slowest encryption.
gzip -N -9 /tmp/orders.sql
# Encrypt
openssl enc -aes-256-cbc -in /tmp/orders.sql.gz -out /tmp/orders.sql.gz.aes256cbc -pass file:~/.db_encr_pass.txt
if [ $? = 0 ] ; then
# Store to storage disk.
rsync -azp /tmp/orders.sql.gz.aes256cbc $storage_server:/home/prod/netapp/db-backups/$(date +%Y%m%d%H%M%S)_orders.sql.gz.aes256cbc
echo "Backup encryption successful" | mail -s "Backup success" systemadmin@internations.org
else
echo "Backup encryption not went good...Exiting"
echo "Backup encryption not went good...Exiting" | mail -s "Backup failed" systemadmin@internations.org
exit 1
fi
# In order to Decrypt the backup
# openssl enc -aes-256-cbc -in 20141013080616_orders.sql.gz.aes256cbc -d -out orders.sql.gz -k <mypassword>
# Clean up
rm /tmp/orders.sql.gz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment