Skip to content

Instantly share code, notes, and snippets.

@jiphex
Created August 3, 2011 09:23
Show Gist options
  • Save jiphex/1122237 to your computer and use it in GitHub Desktop.
Save jiphex/1122237 to your computer and use it in GitHub Desktop.
Remote MySQLdump backup with Rsync driver
#! /bin/bash
# script to backup MySQL data to remote server
# James Hannah - January 2009
# set some variables
FILENAME="`date +mysqldump-%Y-%m-%d-%H:%M:%S`.sql"
LOCALFILE="/home/sql_backup/$FILENAME"
LOCKFILE="/tmp/sqlbackup.lock"
BACKUPSERVER="10.1.1.1"
MYSQLPASS="secretsgohere"
REMOTEPATH="/home/somebackups/"
REMOTEFILE="$REMOTEPATH$FILENAME"
# check the locks
if [ -f $LOCKFILE ]; then
echo "lockfile exists! exiting."
exit 1
else
touch /tmp/sqlbackup.lock
fi
# dump the databases to a file
echo "Dumping the DBs..."
mysqldump -p$MYSQLPASS --single-transaction --quick -A > $LOCALFILE
if [ $? -eq 0 ]; then
echo 'mysqldump was successful'
else
echo 'mysqldump failed'
exit 1
fi
# move that file to the remote machine
echo "Copying them to the remote machine..."
rsync -e "ssh -p 2020" -vazP $LOCALFILE root@$BACKUPSERVER:$REMOTEFILE
RXC="ssh -p 2020 root@$BACKUPSERVER"
echo "Linking the latest file"
#$RXC ln -fs $REMOTEFILE $REMOTEPATH/latest-backup.sql
#$RXC touch $REMOTEPATH/load-latest-dump
#echo "Loading the MySQL dump on the remote machine in the background.."
#$RXC nohup /home/loadmysqldump.sh $REMOTEFILE &
echo "Removing the dump file..."
rm -vf $LOCALFILE # delete the old file
# finally remove the backup lock
rm $LOCKFILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment