This simple script will search for all wp-config.php files in a directory, pull the database name and create a backup of it.
for wpinstall in $(find "$(pwd)"/ -type f -name wp-config.php | sed 's/wp-config.php//g')
do
echo "$wpinstall"
dbname=$(grep DB_NAME "$wpinstall"/wp-config.php | grep -v WP_CACHE_KEY_SALT | cut -d \' -f 4)
echo "Database Name: $dbname"
echo "Creating backup folder /backup/wpdb"
mkdir -p /backup/wpdb
echo "Backing up databases to /backup/wpdb"
/usr/bin/mysqldump "$dbname" | gzip > /backup/wpdb/"$dbname".sql.gz
done