wunderio / ddev-drupal

Work in progress of template for DDEV Drupal projects
0 stars 0 forks source link

Allow syncdb to be used to sync db from all existing envs #24

Open hkirsman opened 5 months ago

hkirsman commented 5 months ago

This is my current new code:

#!/bin/bash

#
# Synchronise local database with the production environment.
#
# Based on https://github.com/wunderio/unisport/blob/master/.lando/syncdb.sh
#

set -eu
if [[ -n "${WUNDERIO_DEBUG:-}" ]]; then
    set -x
fi
export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin

source .ddev/wunderio/core/_helpers.sh

# Pass drush alias as argument and sync the database
# from the remote environment to the local environment.
sync_database_from() {
    alias=$1

    sql_file="$alias-syncdb-$(date +'%Y-%m-%d').sql"

    alias_entry=$(ddev drush sa @$alias)
    ssh_user=$(echo "$alias_entry" | ddev yq '.\"@self.'$alias'\".user' )
    ssh_options=$(echo "$alias_entry" | ddev yq '.\"@self.'$alias'\".ssh.options' )
    ssh_host=$(echo "$alias_entry" | ddev yq '.\"@self.'$alias'\".host' )

    set -x
    # For some reason "ddev drush sql-sync @your-alias @local -y" does not work and times out
    # so we have to do this with sql-dump.
    ssh "$ssh_user@$ssh_host" "$ssh_options" "drush sql-dump --structure-tables-list=cache,cache_*,history,search_*,sessions" > "$sql_file"
    ddev import-db --file="$sql_file"
    #rm "$sql_file"
    { set +x; } 2>/dev/null

    display_status_message "Sync complete!"
}

sync_database_from $1