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).
3 Answers
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
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
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