-
-
Save jjvillavicencio/70e2d63f94866fe986cd25a97d7f8869 to your computer and use it in GitHub Desktop.
Automatic MySQL dump and backup to Git repo cron job
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/sh | |
# | |
# @author: Jabran Rafique <[email protected]> | |
# @link: http://jabran.me/articles/automatic-database-backup-using-git-hosting/ | |
# Set variables | |
FULLDATE = $(date +"%Y-%d-%m %H:%M") | |
NOW = $(date +"%Y-%m-%d-%H-%M") | |
MYSQL_DUMP = `which mysqldump` | |
GIT = `which git` | |
DB_NAME = "foo" | |
CRON_USER = "bar" | |
TEMP_BACKUP = "latest_backup.sql" | |
BACKUP_DIR = $(date +"%Y/%m") | |
# Check current Git status and update | |
${GIT} status | |
${GIT} pull origin HEAD | |
# Dump database | |
${MYSQL_DUMP} -u "$CRON_USER" $DB_NAME > $TEMP_BACKUP & | |
wait | |
# Make backup directory if not exists (format: {year}/{month}/) | |
if [ ! -d "$BACKUP_DIR" ]; then | |
mkdir -p $BACKUP_DIR | |
fi | |
# Compress SQL dump | |
tar -cvzf $BACKUP_DIR/$DB_NAME-$NOW-sql.tar.gz $TEMP_BACKUP | |
# Remove original SQL dump | |
rm -f $TEMP_BACKUP | |
# Add to Git and commit | |
${GIT} add -A | |
${GIT} commit -m "Automatic backup - $FULLDATE" | |
${GIT} push origin HEAD |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment