Skip to content

Instantly share code, notes, and snippets.

@aheritier
Created August 29, 2012 07:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aheritier/3507734 to your computer and use it in GitHub Desktop.
Save aheritier/3507734 to your computer and use it in GitHub Desktop.
dump mysql databases with some improvements to try to improve the import time
#!/bin/bash -ue
if [ $# -lt 2 ]; then
echo "You must specify the directory where to store dumps"
echo "Eg. $0 PATH DB1 [DB2] ... [DBx]"
exit
fi
BACKUP_DIR="$1"
shift;
DBNAMES="$@"
mkdir -p ${BACKUP_DIR}
#mysql -A --skip-column-names -e"SELECT DISTINCT table_schema FROM information_schema.tables WHERE table_schema NOT IN ('information_schema','mysql')" > ${BACKUP_DIR}/ListOfDatabases.txt
mysql --skip-column-names -A -e"SELECT CONCAT('SHOW GRANTS FOR ''',user,'''@''',host,''';') FROM mysql.user WHERE user<>''" | mysql --skip-column-names -A | sed 's/$/;/g' | gzip > ${BACKUP_DIR}/MySQLGrants.sql.gz
#for DB in `cat ${BACKUP_DIR}/ListOfDatabases.txt`
#do
# time mysqldump --hex-blob --routines --triggers ${DB} | gzip > ${_path}/${DB}.sql.gz &
#done
#wait
#### you can change these values but they are optional....
OPTIONS="--opt --tab --no-create-info --compact -q"
RESTORESCRIPT="$BACKUP_DIR/__restoreAllData.sql"
rm -f $RESTORESCRIPT
#### make no changes after this....
#### start script ####
cd ${BACKUP_DIR}
for DB in ${DBNAMES}
do
echo "=========================================="
echo ${DB}
echo "=========================================="
echo 'SET AUTOCOMMIT = 0;' >> $RESTORESCRIPT
echo 'SET FOREIGN_KEY_CHECKS=0;' >> $RESTORESCRIPT
mysqldump --no-data $DB | gzip > ${BACKUP_DIR}/${DB}-createTables.sql.gz
echo "source ${DB}-createTables.sql;" >> $RESTORESCRIPT
for TABLE in `mysql $DB -e 'show tables' | egrep -v 'Tables_in_' `; do
TABLENAME=$(echo $TABLE|awk '{ printf "%s", $0 }')
FILENAME="${DB}-${TABLENAME}.sql"
echo Dumping $TABLENAME
echo 'source' ${DB}'-'$FILENAME';' >> $RESTORESCRIPT
mysqldump --opt --no-create-info --compact --hex-blob --routines --triggers -q $DB $TABLENAME | gzip > ${BACKUP_DIR}/${FILENAME}.gz
done
echo 'SET FOREIGN_KEY_CHECKS=1;' >> $RESTORESCRIPT
echo 'COMMIT;' >> $RESTORESCRIPT
echo 'SET AUTOCOMMIT = 1;' >> $RESTORESCRIPT
echo "done with " $DB
done
echo "=========================================="
echo " done with all database! "
echo "=========================================="
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment