Skip to content

Instantly share code, notes, and snippets.

@xavez
Last active December 30, 2015 13:59
Show Gist options
  • Save xavez/7839100 to your computer and use it in GitHub Desktop.
Save xavez/7839100 to your computer and use it in GitHub Desktop.
Backup local mysql databases. Symlink to latest backup. Run with launchd or cron at regular intervals.
#!/bin/bash
# Path to where you want to backup mysql databases.
opath=/Users/username/Sites/Backups/mysql/
# Local mysql details. Make a username with only read access. Allow SELECT, LOCK TABLES.
mysqlhost=127.0.0.1
username=read_only_user
password=read_only_password
# Get current date and temporary directory.
date=$(date "+%Y-%m-%d-%H%M%S")
cpath=$opath/incomplete_back-${date}
if [ -d $cpath ]
then
filler="just some action to prevent syntax error"
else
echo Creating $cpath
mkdir -p $cpath
fi
#
# Make backups. Adjust paths to binaries if necessary.
#
/usr/local/mysql/bin/mysql -s -r -u${username} -p${password} -e 'show databases' | while read db; do /usr/local/mysql/bin/mysqldump -u${username} -p${password} $db -r ${cpath}/${db}.sql; [[ $? -eq 0 ]] && /usr/bin/gzip ${cpath}/${db}.sql; done
#
# Symlink to latest backup.
#
mv $opath/incomplete_back-$date $opath/back-$date \
&& rm -f $opath/current \
&& ln -s back-$date $opath/current
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment