Created
December 31, 2013 03:18
-
-
Save koehn/8192013 to your computer and use it in GitHub Desktop.
A script to backup MySQL databases. If you're using InnoDB and MariaDB 5.3 or higher, --single-transaction will give you a dump that is consistent without locking the tables. I use this as part of my rsnapshot backups. The use of --single-transaction and the echo "" > /dev/null (to avoid returning non-zero) were the only changes I made.
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 | |
# A UNIX / Linux shell script to backup mysql server database using rsnapshot utility. | |
# ------------------------------------------------------------------------- | |
# Copyright (c) 2007 Vivek Gite <vivek@nixcraft.com> | |
# This script is licensed under GNU GPL version 2.0 or above | |
# ------------------------------------------------------------------------- | |
# This script is part of nixCraft shell script collection (NSSC) | |
# Visit http://bash.cyberciti.biz/ for more information. | |
# ------------------------------------------------------------------------- | |
# Tested under RHEL / Debian / CentOS / FreeBSD oses | |
# Must be Installed on remote MySQL Server | |
# ------------------------------------------------------------------------- | |
# Last update: Sun Jul 5 2009 : Added mysql ping support and binary checking | |
# ------------------------------------------------------------------------- | |
### SETUP MYSQL LOGIN ### | |
MUSER='root' | |
MPASS='' | |
MHOST="127.0.0.1" | |
### Set to 1 if you need to see progress while dumping dbs ### | |
VERBOSE=0 | |
### Set bins path ### | |
GZIP=/bin/gzip | |
MYSQL=/usr/bin/mysql | |
MYSQLDUMP=/usr/bin/mysqldump | |
RM=/bin/rm | |
MKDIR=/bin/mkdir | |
MYSQLADMIN=/usr/bin/mysqladmin | |
GREP=/bin/grep | |
### Setup dump directory ### | |
BAKRSNROOT=/tmp/rsnapshot/mysql | |
##################################### | |
### ----[ No Editing below ]------### | |
##################################### | |
### Default time format ### | |
TIME_FORMAT='%H_%M_%S%P' | |
### Make a backup ### | |
backup_mysql_rsnapshot(){ | |
local DBS="$($MYSQL -Bse 'show databases')" | |
local db=""; | |
[ ! -d $BAKRSNROOT ] && ${MKDIR} -p $BAKRSNROOT | |
${RM} -f $BAKRSNROOT >/dev/null 2>&1 | |
[ $VERBOSE -eq 1 ] && echo "*** Dumping MySQL Database ***" | |
[ $VERBOSE -eq 1 ] && echo -n "Database> " | |
for db in $DBS | |
do | |
local tTime=$(date +"${TIME_FORMAT}") | |
local FILE="${BAKRSNROOT}/${db}.${tTime}.gz" | |
[ $VERBOSE -eq 1 ] && echo -n "$db.." | |
${MYSQLDUMP} --single_transaction $db | ${GZIP} -9 > $FILE | |
done | |
[ $VERBOSE -eq 1 ] && echo "" | |
[ $VERBOSE -eq 1 ] && echo "*** Backup done [ files wrote to $BAKRSNROOT] ***" | |
echo "" > /dev/null | |
} | |
### Die on demand with message ### | |
die(){ | |
echo "$@" | |
exit 999 | |
} | |
### Make sure bins exists.. else die | |
verify_bins(){ | |
[ ! -x $GZIP ] && die "File $GZIP does not exists. Make sure correct path is set in $0." | |
[ ! -x $MYSQL ] && die "File $MYSQL does not exists. Make sure correct path is set in $0." | |
[ ! -x $MYSQLDUMP ] && die "File $MYSQLDUMP does not exists. Make sure correct path is set in $0." | |
[ ! -x $RM ] && die "File $RM does not exists. Make sure correct path is set in $0." | |
[ ! -x $MKDIR ] && die "File $MKDIR does not exists. Make sure correct path is set in $0." | |
[ ! -x $MYSQLADMIN ] && die "File $MYSQLADMIN does not exists. Make sure correct path is set in $0." | |
[ ! -x $GREP ] && die "File $GREP does not exists. Make sure correct path is set in $0." | |
} | |
### Make sure we can connect to server ... else die | |
verify_mysql_connection(){ | |
$MYSQLADMIN ping | $GREP 'alive'>/dev/null | |
[ $? -eq 0 ] || die "Error: Cannot connect to MySQL Server. Make sure username and password are set correctly in $0" | |
} | |
### main #### | |
verify_bins | |
verify_mysql_connection | |
backup_mysql_rsnapshot |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment