Exporting all of the databases in your homestead server to your local machine is a bit of a boring task.  As I’m decommissioning a Macbook (and moving to Valet instead) I wanted to find a way to make copies of these old testing databases “Just In Case”

So, a bit of googling and some stitching of answers together and I’ve come up with this.  It would also work if you wanted to run it against your local mysql and so on. The beauty of this script is each database is a SEPARATE MYSQL SCRIPT instead of everything inside a single file such as you would get from the mysqldump command

Remember putting passwords on the command line is generally a bad idea so be very careful where you use this mysql script


for DB in $(mysql -e 'show databases' -s --skip-column-names); do
    mysqldump --opt --lock-tables=true -u homestead -psecret $DB > "$DB.sql";
done

If you need to run it locally against a mysql server on your mac try (assuming user root and no password)


for DB in $(mysql -e 'show databases' -s -u root --skip-column-names); do
    mysqldump --opt --lock-tables=true -u root $DB > "$DB.sql";
done