Skip to content

Instantly share code, notes, and snippets.

@aarongustafson
Created January 22, 2014 19:43
Show Gist options
  • Save aarongustafson/8565953 to your computer and use it in GitHub Desktop.
Save aarongustafson/8565953 to your computer and use it in GitHub Desktop.
Backup DB & files to Git You want these in a folder that is part of a sparse checkout.
#!/bin/bash
##
# Backup Folders to Git
#
# Creates a local cached backup of the source folder using rsync and then
# synchronises that and the database with Git.
#
# It is assumed you have rsync installed on the server.
#
# Author: Aaron Gustafson (@aarongustafson)
#
# 0.1 | 2014-01-06 | Internal Development Version
##
# Setup procedure
# 1. Just fill in the variables in the configuration section below
# 2. run the sync script using ./run.sh
## CONFIGURATION
REPO_FOLDER="/PATH/TO/BACKUP/FOLDER"
REPO_BRANCH="master"
# database settings
DB_NAME="DB_NAME"
DB_USER="DB_USER"
DB_PASS="DB_PASS"
DB_FILENAME="${REPO_FOLDER}/sql/${DB_NAME}.sql"
# RSYNC SETTINGS
LOCAL_BACKUP_PATH="${REPO_FOLDER}/DESTINATION_FOLDER/"
SOURCE_PATH="/PATH/TO/SOURCE_FOLDER"
RSYNC_DELETE_REMOVED="ON"
## STOP EDITING
# build the rsync command
rsync_cmd="rsync -a ${SOURCE_PATH} ${LOCAL_BACKUP_PATH}"
rsync_msg="Rsyncing ${SOURCE_PATH} to ${LOCAL_BACKUP_PATH}."
if [[ "$RSYNC_DELETE_REMOVED" == "ON" ]]; then
rsync_cmd="${rsync_cmd} --delete"
rsync_msg="${rsync_msg} Removed files will be deleted."
fi
echo "$(date -u): Updating from Git"
cd $REPO_FOLDER
git pull --quiet origin $REPO_BRANCH
echo "$(date -u): ${rsync_msg}"
# run rsync
eval $rsync_cmd
echo "$(date -u): RSync Complete"
echo "$(date -u): Backing up DB"
# dump the database using the mysql administrator - so we can see all dbs
mysqldump -u$DB_USER -p$DB_PASS --opt --routines --compact --force "${DB_NAME}" > "${DB_FILENAME}"
echo "$(date -u): DB Backup Complete"
echo "$(date -u): Sending to Git"
# add everything we have - will throw a warning but its fine
git add .
# commit
git commit --quiet -m "Nightly Backup"
# push
git push --quiet origin $REPO_BRANCH
echo "$(date -u): Done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment