Skip to content

Instantly share code, notes, and snippets.

@afeijo
Created June 27, 2012 13:17
Show Gist options
  • Save afeijo/3004010 to your computer and use it in GitHub Desktop.
Save afeijo/3004010 to your computer and use it in GitHub Desktop.
Mysql automated backup
#!/bin/bash
##########################################################################
#
# mysql_backups.sh: A shell script to back up all MySQL databases in
# one shot, nightly, and keep a rolling 3 weeks of
# backups hot, online in the backup archive.
#
# Written by David A. Desrosiers
# Contact [email protected]
# Last updated Jun 27 2012 by Alessandro Feijó @afeijo
#
# Copyright 1998-2007. This may be modified and distributed on the same
# terms as the GPL itself. This copyright header
# must remain intact if you use this script.
#
##########################################################################
#####################################
### MySQL Configuration Variables ###
#####################################
#PATH=/usr/local/bin:/usr/bin:/bin
# MySQL hostname
DBHOST='localhost'
# MySQL username
DBUSER='root'
# MySQL password
DBPASSWD=''
# directory for dump files
LOCALDIR=/var/backups/mysql
#####################################
### Edit Below If Necessary #########
#####################################
cd $LOCALDIR
DATE=`eval date +%Y%m%d-%H%M`
case $1 in
chk) mysqlcheck -u$DBUSER -h$DBHOST -oA; exit 0;; #optimize all dbs
all) DBS=`mysql -u$DBUSER -h$DBHOST -e"show databases"`;;
esac
for DATABASE in $DBS
do
if [ $DATABASE != "Database" ]; then
echo "Dumping $DATABASE now..."
mysqldump -u$DBUSER -h$DBHOST $DATABASE --add-drop-table > ${DATE}_${DATABASE}.sql
fi
done
gzip -9 *.sql
chmod 644 *gz
# Delete files older than XX days
find $LOCALDIR -mtime +15 -delete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment