A
A
Alexey Yarkov2016-06-12 18:46:34
MySQL
Alexey Yarkov, 2016-06-12 18:46:34

How to get database names excluding system ones?

Wrote a MySQL dump script to files for myself. I know it's a bike, but for me to train. And here is the result on the screen:
525e3343d36842fdab07c815e7befb4d.png
Here is the script itself:

#!/bin/bash
#

DUMP_FOLDER=${PWD}/dump
DB_USER=root
DB_PASSWORD=root
DB_HOST=localhost

[ -d ${DUMP_FOLDER} ] || mkdir ${DUMP_FOLDER}

DB_LIST=$(echo "SHOW DATABASES;" | mysql --host=${DB_HOST} --user=${DB_USER} --password=${DB_PASSWORD})

for DB_NAME in $DB_LIST
do
    DB_FOLDER=${DUMP_FOLDER}/${DB_NAME}
    [ -d ${DB_FOLDER} ] || mkdir ${DB_FOLDER}
    TABLE_LIST=$(echo "USE ${DB_NAME};SHOW TABLES;" | mysql --host=${DB_HOST} --user=${DB_USER} --password=${DB_PASSWORD})
    for TABLE_NAME in $TABLE_LIST
    do
        mysqldump --host=${DB_HOST} --user=${DB_USER} --password=${DB_PASSWORD} -B ${DB_NAME} --tables ${TABLE_NAME} > ${DB_FOLDER}/${TABLE_NAME}.sql
    done
done

Actually, I only created the bashorg database (dump of quotes made using grab in python).
Service data, such as information_schema is not needed. The names of the required databases will be unknown in advance. How to exclude them?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Savostin, 2016-06-12
@yarkov

DB_LIST=$(echo "SHOW DATABASES where \`database\` != 'mysql' AND \`database\` not like '%_schema';" | mysql --host=${DB_HOST} --user=${DB_USER} --password=${DB_PASSWORD})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question