Last active
August 29, 2015 14:03
-
-
Save bradt/54f97d4d669826c8d11d to your computer and use it in GitHub Desktop.
Painfully simple database snapshot shell script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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