Skip to content

Instantly share code, notes, and snippets.

@rosswintle
Created January 10, 2018 10:38
Show Gist options
  • Save rosswintle/aa07de0bbf981d56b4401a1e288fd17c to your computer and use it in GitHub Desktop.
Save rosswintle/aa07de0bbf981d56b4401a1e288fd17c to your computer and use it in GitHub Desktop.
MySQL backup all database script
#!/bin/sh
#
# This dumps all MySQL databases if you give it a root database login. You can specify the directory.
# It creates a date-and-timestamped sub-directory and individual files in there for each database.
#
HOSTNAME='localhost'
MYSQLUSER='root'
MYSQLPASS='password'
TIMESTAMP=`date +%F-%H%M%S`
BACKUPDIR=<directory>
mkdir ${BACKUPDIR}/${TIMESTAMP}
for DB in $(mysql -h $HOSTNAME -u $MYSQLUSER --password=${MYSQLPASS} --batch --skip-column-names --execute="show databases");
do
FILENAME=${BACKUPDIR}/${TIMESTAMP}/${HOSTNAME}_Database_${DB}.sql.gz
mysqldump $DB -h $HOSTNAME -u $MYSQLUSER --password=${MYSQLPASS} --triggers --routines --events | gzip -c > $FILENAME
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment