Skip to content

Instantly share code, notes, and snippets.

@renekreijveld
Last active February 22, 2018 01:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save renekreijveld/6077798 to your computer and use it in GitHub Desktop.
Save renekreijveld/6077798 to your computer and use it in GitHub Desktop.
Create a backup of all your MySQL databases
#!/bin/sh
# dbbackup - Create a backup of all your MySQL databases
#
# Copyright 2011 Rene Kreijveld - r.kreijveld@gakijken.nl
#
# This program is free software; you may redistribute it and/or modify it.
NOW=$(date +"%Y%m%d-%H%M%S")
DBCONF=/usr/local/directadmin/conf/mysql.conf
DBUSER=`grep 'user=' $DBCONF | cut -d = -f 2`
DBHOST=localhost
DBPASS=`grep 'passwd=' $DBCONF | cut -d = -f 2`
BACKUPDIR=/backups/mysql
# Find MySQL Socket
if [ -S /var/lib/mysql/mysql.sock ]
then
MYSOCK=/var/lib/mysql/mysql.sock
elif [ -S /var/run/mysqld/mysqld.sock ]
then
MYSOCK=/var/run/mysqld/mysqld.sock
elif [ -S /tmp/mysql.sock ]
then
MYSOCK=/tmp/mysql.sock
fi
#MYSOCK=/path/to/your/socket/file/here
if [ -z MYSOCK ]
then
echo "No valid MySQL socket file found!"
echo "Either MySQL is not running or it is installed in a custom location."
echo "Manually add it's path in this script on line 28."
exit 1
fi
DBS=`mysql -u$DBUSER -h$DBHOST -p$DBPASS -S$MYSOCK -e"show databases"`
for DATABASE in $DBS
do
case "$DATABASE" in
'Database')
;;
'information_schema')
;;
'performance_schema')
;;
'da_roundcube')
;;
'mysql')
;;
*)
FILENAME="$DATABASE.$NOW.gz"
mysqldump -u$DBUSER -h$DBHOST -p$DBPASS -S$MYSOCK $DATABASE | gzip --best > $BACKUPDIR/$FILENAME
;;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment