Skip to content

Instantly share code, notes, and snippets.

@devraj
Last active November 1, 2017 03:19
Show Gist options
  • Save devraj/6d74335a3a5d0f6ccbae44d242f6af04 to your computer and use it in GitHub Desktop.
Save devraj/6d74335a3a5d0f6ccbae44d242f6af04 to your computer and use it in GitHub Desktop.
MySQL data dump script for a Python application running on AWS, appends Python app version number and date stamp to filename
#!/bin/bash
# MySQL configuration
# For the script to run headless without entering a password create ~/.my.cnf and
# [mysqldump]
# user=username
# password=password
#
# This makes mysqldump refer to the configuration for the password
#
USERNAME="username"
DATABASE="database_name"
HOSTNAME="hostname"
# DESTINATION is where backups are written to
DESTINATION=$HOME/backup
# Activate the virtualenv
source /srv/helsal/venv/bin/activate
# Meta information to construct the file name
MYAPP_VERSION=`python -c "import myapp; print myapp.__version__"`
FORMATTED_DATE=`date +"%Y%m%d"`
# Filename myapp-prod-VERSION-20161102.sql
FILENAME=helsal-prod-$MYAPP_VERSION-$FORMATTED_DATE.sql
# Let go of the virtualenv
deactivate
# Remove old backups
if [ -a $DESTINATION/*sql.gz ]; then /bin/rm $DESTINATION/*sql.gz; fi
# Run the backup
/usr/bin/mysqldump -u $USERNAME -h $HOSTNAME --databases $DATABASE --no-create-db --single-transaction > $DESTINATION/$FILENAME
# Gzip and ready for download
/bin/gzip -f $DESTINATION/$FILENAME
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment