Last active
May 21, 2026 22:27
-
-
Save mattbloomfield/26ec87c8068c6a33b62d86125255d86c to your computer and use it in GitHub Desktop.
Requires our custom .helpers.sh file: https://gist.github.com/mattbloomfield/da096ba4b90e77d26a79cae0b5caad2f
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| ## Description: Sync remote database and/or files with local environment | |
| ## Usage: sync | |
| ## Example: "ddev sync production db -files" | |
| source "$(dirname "$0")/.helpers.sh" | |
| ddev_setup_error_handling | |
| BUCKET_PREFIX="mysite-assets" # change this to whatever | |
| # ─── Help ───────────────────────────────────────────────────────────────────── | |
| if ddev_check_help "$@"; then | |
| ddev_print_help \ | |
| "Usage: ddev sync [environment] [db|-db] [files|assets|-files|-assets]" \ | |
| "" \ | |
| "Sync remote database and/or files with your local environment." \ | |
| "" \ | |
| "Arguments:" \ | |
| " environment master, staging, or production (default: production)" \ | |
| " db Pull the database" \ | |
| " -db Skip the database" \ | |
| " files, assets Pull assets" \ | |
| " -files, -assets Skip assets" \ | |
| "" \ | |
| "Examples:" \ | |
| " ddev sync # Interactive prompts" \ | |
| " ddev sync production # Prompted for db/files choices" \ | |
| " ddev sync production db # Pull db only, skip files" \ | |
| " ddev sync production db files # Pull both db and files" \ | |
| " ddev sync staging -db files # Pull files only from staging" | |
| fi | |
| # ─── Parse arguments ────────────────────────────────────────────────────────── | |
| REMOTE_ENV="" | |
| PULL_DB="" | |
| PULL_FILES="" | |
| for arg in "$@"; do | |
| case $arg in | |
| development|master|staging|production) REMOTE_ENV=$arg ;; | |
| db) PULL_DB="y" ;; | |
| -db) PULL_DB="n" ;; | |
| files|assets) PULL_FILES="y" ;; | |
| -files|-assets) PULL_FILES="n" ;; | |
| esac | |
| done | |
| # ─── Prompt for missing choices ─────────────────────────────────────────────── | |
| if [[ -z "$REMOTE_ENV" ]]; then | |
| ddev_prompt_choice "Environment" "production" "master staging production" | |
| REMOTE_ENV="$REPLY" | |
| fi | |
| if [[ -z "$PULL_DB" ]]; then | |
| ddev_prompt_yn "Pull database from ${YELLOW}${REMOTE_ENV}${RESET}?" "y" | |
| PULL_DB="$REPLY" | |
| fi | |
| if [[ -z "$PULL_FILES" ]]; then | |
| ddev_prompt_yn "Pull assets from ${YELLOW}${REMOTE_ENV}${RESET}?" "n" | |
| PULL_FILES="$REPLY" | |
| fi | |
| # ─── Summary + Confirm ─────────────────────────────────────────────────────── | |
| ddev_header "Sync Summary" | |
| ddev_summary_line "Environment" "$REMOTE_ENV" | |
| ddev_summary_line "Pull database" "$( [[ "$PULL_DB" == "y" ]] && echo "yes" || echo "no" )" | |
| ddev_summary_line "Pull assets" "$( [[ "$PULL_FILES" == "y" ]] && echo "yes" || echo "no" )" | |
| ddev_confirm_or_abort "Proceed with sync?" | |
| # ─── Sync database ─────────────────────────────────────────────────────────── | |
| if [[ "$PULL_DB" == "y" ]]; then | |
| ddev_step "Syncing database from ${YELLOW}${REMOTE_ENV}${RESET}" | |
| mysqldump --add-drop-table -u root -proot db | grep 'DROP TABLE' >> "/tmp/drop_db_$(date +%Y-%m-%d_%H-%M-%S).sql" | |
| ./craft servd-asset-storage/local/pull-database --from="${REMOTE_ENV}" --skipBackup=1 --interactive=0 | |
| ddev_success "Database synced from ${REMOTE_ENV}" | |
| fi | |
| # ─── Sync files ────────────────────────────────────────────────────────────── | |
| if [[ "$PULL_FILES" == "y" ]]; then | |
| ddev_step "Syncing assets from ${YELLOW}${REMOTE_ENV}${RESET} to ${YELLOW}development S3${RESET}" | |
| case $REMOTE_ENV in | |
| development|master) | |
| ddev_warn "Assets are already on development — nothing to sync" | |
| ;; | |
| staging) | |
| aws s3 sync --acl public-read "s3://${BUCKET_PREFIX}-staging/" "s3://${BUCKET_PREFIX}-development/" | |
| ddev_success "Assets synced from ${REMOTE_ENV}" | |
| ;; | |
| production) | |
| aws s3 sync --acl public-read "s3://${BUCKET_PREFIX}/" "s3://${BUCKET_PREFIX}-development/" | |
| ddev_success "Assets synced from ${REMOTE_ENV}" | |
| ;; | |
| esac | |
| fi | |
| ddev_success "Sync complete" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment