Skip to content

Instantly share code, notes, and snippets.

@bradt
Last active August 29, 2015 14:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bradt/54f97d4d669826c8d11d to your computer and use it in GitHub Desktop.
Save bradt/54f97d4d669826c8d11d to your computer and use it in GitHub Desktop.
Painfully simple database snapshot shell script
#!/bin/bash
cd ~/db-backups
# Configure your database info
DB_NAME=sample_db
DB_USERNAME=sample_user
DB_PASSWORD=sample_pw
# Configure the name of the file
FILENAME=dbrains_wp_
SQL_FILE=$FILENAME`date +%Y%m%d%H%M%S`.sql
# Create a database dump file
mysqldump -u $DB_USERNAME --password=$DB_PASSWORD -h localhost $DB_NAME > $SQL_FILE 2>&1
# Compress the database dump file
gzip $SQL_FILE
# Remove database dump files that are a month old
rm -f $FILENAME`date +%Y%m%d* --date='1 month ago'`.sql.gz
# Create folder ~/db-backups and install the following cron to save a snapshot every 2 hours:
# 10 */2 * * * ~/db-backup.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment