44#
55# Synchronise local database with a remote environment.
66#
7- # Based on https://github.com/wunderio/unisport/blob/master/.lando/syncdb.sh
8- #
97
108set -eu
119
@@ -20,11 +18,11 @@ source "$WUNDERIO_GLOBAL_SCRIPT_ROOT/_helpers.sh"
2018# Check if an alias was provided as an argument.
2119if [[ -z " ${1:- } " ]]; then
2220 display_error_message " Error: No site alias name provided."
23- display_warning_message " Usage: ddev syncdb <alias> [--keep-dump] [--backup] [--deploy ]"
21+ display_warning_message " Usage: ddev syncdb <alias> [--keep-dump] [--backup] [--skip-hooks ]"
2422 display_warning_message " Example: ddev syncdb prod"
25- display_warning_message " --keep-dump Keep the downloaded dump file after import"
26- display_warning_message " --backup Create a local database backup before overwriting"
27- display_warning_message " --deploy Run drush deploy and drush uli after import"
23+ display_warning_message " --keep-dump Keep the downloaded dump file after import"
24+ display_warning_message " --backup Create a local database backup before overwriting"
25+ display_warning_message " --skip-hooks Skip DDEV post-import hooks during database import"
2826 exit 1
2927fi
3028
@@ -38,12 +36,12 @@ shift 1
3836# Parse flags
3937KEEP_DUMP=false
4038BACKUP=false
41- DEPLOY =false
39+ SKIP_HOOKS =false
4240for arg in " $@ " ; do
4341 case " $arg " in
44- --keep-dump) KEEP_DUMP=true ;;
45- --backup) BACKUP=true ;;
46- --deploy ) DEPLOY =true ;;
42+ --keep-dump) KEEP_DUMP=true ;;
43+ --backup) BACKUP=true ;;
44+ --skip-hooks ) SKIP_HOOKS =true ;;
4745 esac
4846done
4947
@@ -65,10 +63,9 @@ if ! grep -q "^${ALIAS_KEY}:" "$SITE_YML"; then
6563 exit 1
6664fi
6765
68- # --- 3. Warn about overwrite ---
66+ # --- 3. Prepare dumps directory and warn about overwrite ---
6967display_warning_message " This will overwrite your local database with data from '$SITE_ALIAS '."
7068
71- # --- 4. Prepare dumps directory ---
7269DUMPS_DIR=" $PROJECT_ROOT /database_dumps"
7370
7471if [ ! -d " $DUMPS_DIR " ]; then
8178# Use .sql.gz extension for compressed dump.
8279sql_file=" $DUMPS_DIR /${ALIAS_KEY} -syncdb-$( date +' %Y-%m-%d' ) .sql.gz"
8380
84- # --- 5 . Create local backup (if --backup) ---
81+ # --- 4 . Create local backup (if --backup) ---
8582if [[ " $BACKUP " == " true" ]]; then
8683 backup_file=" $DUMPS_DIR /backup-$( date +' %Y-%m-%d-%H%M%S' ) .sql.gz"
8784 display_status_message " Creating local database backup: $backup_file "
8885 ddev export-db --gzip --file=" $backup_file "
8986 display_status_message " Backup saved."
9087fi
9188
92- # --- 6 . Read remote alias details from Drush ---
93- if ! alias_details=$( ddev drush sa " $SITE_ALIAS " 2>&1 ) ; then
89+ # --- 5 . Read remote alias details from Drush ---
90+ if ! alias_details=$( ddev drush sa " $SITE_ALIAS " --format=yaml 2>&1 ) ; then
9491 display_error_message " Drush command failed."
9592 echo " --------------------------------------------------"
9693 echo " $alias_details "
103100# DDEV might be injecting some messages to output so clean the output.
104101alias_details_clean=$( echo " $alias_details " | sed -n ' /@self/,$p' )
105102
106- eval " $( ddev yq ' ."@self.' " $ALIAS_KEY " ' " | "remote_ssh_user=\"" + .user + "\" remote_ssh_host=\"" + .host + "\" remote_ssh_options=\"" + .ssh.options + "\""' <<< " $alias_details_clean" ) "
103+ # Dynamically build the yq query path using the alias key ("main").
104+ alias_full=" @self.${ALIAS_KEY} "
105+ read -r remote_ssh_user remote_ssh_host remote_ssh_options < <(
106+ ddev exec -- yq -r " .\" $alias_full \" | [.user, .host, .ssh.options] | @tsv" <<< " $alias_details_clean"
107+ )
107108
108- # --- Validate parsed SSH details ---
109+ # --- 6. Validate parsed SSH details ---
109110if [[ -z " $remote_ssh_user " || " $remote_ssh_user " == " null" ]]; then
110111 display_error_message " Missing or invalid SSH user for alias '$ALIAS_KEY '."
111112 display_warning_message " Check your drush/sites/self.site.yml configuration for the 'user' field."
@@ -139,21 +140,24 @@ display_status_message "Dumping database from '$SITE_ALIAS' (gzip compressed)...
139140
140141display_status_message " Dump complete, starting import!"
141142
143+ # Build import-db command with conditional flags.
142144# ddev import-db natively handles .gz files.
143- ddev import-db --file=" $sql_file "
145+ import_cmd=(ddev import-db --file=" $sql_file " )
146+
147+ # Full deployment steps can be ran seperatly.
148+ if [[ " $SKIP_HOOKS " == " true" ]]; then
149+ import_cmd+=(--skip-hooks)
150+ fi
151+
152+ " ${import_cmd[@]} "
153+
144154if [[ " $KEEP_DUMP " != " true" ]]; then
145155 rm " $sql_file "
146156fi
147- { set +x; } 2> /dev/null
148157
149- if [[ " $DEPLOY " == " true" ]]; then
150- display_status_message " Running drush deploy..."
151- ddev drush deploy -y || { display_error_message " drush deploy failed" ; exit 1; }
152- display_status_message " One-time login link: $( ddev drush uli) "
153- fi
158+ # Sanitize imported database (remove sensitive data).
159+ ddev drush sqlsan -y || { display_error_message " Database sanitization failed" ; exit 1; }
160+
161+ { set +x; } 2> /dev/null
154162
155163display_status_message " Sync with '$SITE_ALIAS ' complete!"
156- if [[ " $DEPLOY " != " true" ]]; then
157- display_warning_message " Run 'ddev drush deploy' to apply database updates, import config, and rebuild caches."
158- display_warning_message " Run 'ddev drush uli' to generate a one-time login link."
159- fi
0 commit comments