Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I need to export around 20 databases in a MySql server which contain around 100 different databases, is it possible to export a list of database names into separate files using a console command? (under Linux of course).

share|improve this question

3 Answers 3

echo "show databases" | mysql -N | while read db
do
  mysqldump ${db} > ${db}.sql
done

You could use grep to remove unwanted databases like mysql and information_schema

share|improve this answer

Create a text file with the name of each database you want to backup on it's own line

e.g.

db1
reports

Then use a script like this

#!/bin/bash
ERRORS=0

while read db
do
    echo "Processing $db"
    mysqldump -u root -p365576dx "$db" >"$db".sql
    EXITSTATUS=$?
    if [ "$ERRORS" -eq "0" -a "$EXITSTATUS" -ne "0" ]
    then
       ERRORS="$EXITSTATUS"
    fi

done<dbstodump

if [ "$ERRORS" -ne "0" ]
then
    echo "Errors occurred see above for details"
    exit 1
fi
share|improve this answer

The easiest solution would be a simple for loop right on the command line. In my examples I want to export the databases cars, animals and computers.

for db in cars animals computers ; do mysqldump -uuser -ppassword "$db" > "$db.sql" ; done
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.