Last active
December 30, 2015 13:59
-
-
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.
This file contains 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 | |
# 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