Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export and Import all MySQL databases at one time

I want to keep a backup of all my MySQL databases. I have more than 100 MySQL databases. I want to export all of them at the same time and again import all of them into my MySQL server at one time. How can I do that?

like image 227
NewUser Avatar asked Feb 29 '12 11:02

NewUser


People also ask

How can I get Mysqldump from all databases?

To dump entire databases, do not name any tables following db_name , or use the --databases or --all-databases option. To see a list of the options your version of mysqldump supports, issue the command mysqldump --help .


2 Answers

Export:

mysqldump -u root -p --all-databases > alldb.sql 

Look up the documentation for mysqldump. You may want to use some of the options mentioned in comments:

mysqldump -u root -p --opt --all-databases > alldb.sql mysqldump -u root -p --all-databases --skip-lock-tables > alldb.sql 

Import:

mysql -u root -p < alldb.sql 
like image 76
Shakti Singh Avatar answered Sep 19 '22 16:09

Shakti Singh


Other solution:

It backs up each database into a different file

#!/bin/bash  USER="zend" PASSWORD="" #OUTPUT="/Users/rabino/DBs"  #rm "$OUTPUTDIR/*gz" > /dev/null 2>&1  databases=`mysql -u $USER -p$PASSWORD -e "SHOW DATABASES;" | tr -d "| " | grep -v Database`  for db in $databases; do     if [[ "$db" != "information_schema" ]] && [[ "$db" != "performance_schema" ]] && [[ "$db" != "mysql" ]] && [[ "$db" != _* ]] ; then         echo "Dumping database: $db"         mysqldump -u $USER -p$PASSWORD --databases $db > `date +%Y%m%d`.$db.sql        # gzip $OUTPUT/`date +%Y%m%d`.$db.sql     fi done 
like image 27
jruzafa Avatar answered Sep 20 '22 16:09

jruzafa