Skip to content

Instantly share code, notes, and snippets.

@lostsnow
Created December 25, 2018 01:54
Show Gist options
  • Save lostsnow/78309b8b6fcf369e83f6e9218205c295 to your computer and use it in GitHub Desktop.
Save lostsnow/78309b8b6fcf369e83f6e9218205c295 to your computer and use it in GitHub Desktop.
Backup all mysql data
#!/bin/bash
MYSQL_HOST=127.0.0.1
MYSQL_USER=root
MYSQL_PASS='123456'
MYSQL_CONN="-h${MYSQL_HOST} -u${MYSQL_USER} -p${MYSQL_PASS}"
#
# Collect all database names except for
# mysql, information_schema, and performance_schema
#
SQL="SELECT schema_name FROM information_schema.schemata WHERE schema_name NOT IN"
SQL="${SQL} ('mysql','information_schema','performance_schema')"
DBLISTFILE=/tmp/DatabasesToDump.txt
mysql ${MYSQL_CONN} -ANe"${SQL}" > ${DBLISTFILE}
DBLIST=""
for DB in `cat ${DBLISTFILE}` ; do DBLIST="${DBLIST} ${DB}" ; done
MYSQLDUMP_OPTIONS="--skip-lock-tables --single-transaction --flush-logs --hex-blob --master-data=2"
mysqldump ${MYSQL_CONN} ${MYSQLDUMP_OPTIONS} --databases ${DBLIST} > all-dbs.sql
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment