Skip to content

Instantly share code, notes, and snippets.

@andypotanin
Last active August 29, 2015 14:15
Show Gist options
  • Select an option

  • Save andypotanin/b51696ef4716c55bd6d5 to your computer and use it in GitHub Desktop.

Select an option

Save andypotanin/b51696ef4716c55bd6d5 to your computer and use it in GitHub Desktop.
#!/bin/bash
## export DB_USER=root
## export DB_PASSWORD=ktikfyefkieyknwi
## export DB_HOST=colossus.wpcloud.io
## exportDatabases root ktikfyefkieyknwi colossus.wpcloud.io
function exportDatabases {
export _databases=`mysql --user=${1} --pasword=${2} --host=${3} -e "SHOW DATABASES;" | tr -d "| " | grep -v Database`
for db in $_databases; do
if [[ "$db" != "information_schema" ]] && [[ "$db" != "performance_schema" ]] && [[ "$db" != "mysql" ]] && [[ "$db" != _* ]] ; then
export _file="${db}-$(date +%Y-%m-%d-%H).sql";
if [ ! -f ${_file} ]; then
echo " - Exporting database: ${db} to ${_file}"
#mysqldump --host=$DB_HOST -u $USER -p$PASSWORD --databases $db > ${_file}
fi;
fi
done
echo " - Done exporting, starting batch GZIP."
# gzip $(hostname)-*.sql
}
## Export
mysqldump -u root -p mysql > $(hostname)-mysql.sql
gzip $(hostname)-mysql.sql
## Import
gunzip $(hostname)-mysql.sql
mysql -uroot -p mysql < $(hostname)-mysql.sql
mysql -uroot -p -c="FLUSH PRIVILEGES"
# http://stackoverflow.com/questions/597732/backup-mysql-users
## Export User to DB grants
mysql -BNe "select concat('\'',user,'\'@\'',host,'\'') from mysql.user where user != 'root'" | \
while read uh; do mysql -BNe "show grants for $uh" | sed 's/$/;/; s/\\\\/\\/g'; done > $(hostname)-grants.sql
gzip $(hostname)-grants.sql
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment