Export All Databases In MySQL At Once

While this might not be something you would need to do every day, it could come in handy to know how to export all databases in your MySQL instance at once. The implementation makes use of a shell script which is an essential component of Linux process automation. Shell scripts make it easier to write and execute commands.

Let’s jump to the code.

#!/bin/bash

# Your MySQL credentials
USER="username"
PASSWORD="password"

# Retrieve all databases in your MySQL instance
DATABASES=$(mysql -u $USER -p$PASSWORD -e "SHOW DATABASES;" | grep -Ev "(Database|information_schema|performance_schema|mysql)")

# Export as you iterate through the databases in your MySQL instance 
for DB in $DATABASES; do
    mysqldump -u $USER -p$PASSWORD --databases $DB > $DB.sql
done

 

Executing Shell Scripts

Assumption: You are running a computer that uses Linux or a Unix equivalent.

  1. Create the shell script (as  above)
  2. Set the right file permission to the script  chmod +x name_of_the_script.sh
  3. Run the script on your Terminal ./name_of_the_script.sh

 

Symatech Labs is a Software Development company based in Nairobi, Kenya that specializes in Software Development, Mobile App Development, Web Application Development, Integrations, USSD and Consultancy.