Skip to content

Add aliases-based ddev syncfiles #54

@hkirsman

Description

@hkirsman

We have project wehre we are using ddev command to sync files. Let's implement same or similar also in this project.

Here's the current implementation:

#!/bin/bash

## Description: Sync private files from remote Silta environment to local
## Usage: syncfiles [-y|--yes] <environment>
## Example: ddev syncfiles prod
## Example: ddev syncfiles -y master

set -e

# Get available aliases from drush (excluding local)
get_aliases() {
  drush site:alias --format=list 2>/dev/null | grep -v 'local$' | sed 's/@self\.//' | sed 's/@//'
}

show_help() {
  ALIASES_LIST=$(get_aliases)
  ALIASES=$(echo "$ALIASES_LIST" | paste -sd ',' - | sed 's/,/, /g')
  FIRST_ALIAS=$(echo "$ALIASES_LIST" | head -1)
  SECOND_ALIAS=$(echo "$ALIASES_LIST" | sed -n '2p')

  echo "Usage: ddev syncfiles [-y|--yes] <environment>"
  echo ""
  echo "Sync private files from a remote Silta environment to local."
  echo ""
  echo "Options:"
  echo "  -y, --yes     Skip confirmation prompt (passed to drush)"
  echo ""
  echo "Arguments:"
  echo "  environment   Remote environment alias (required)"
  echo "                Available: ${ALIASES:-see 'drush site:alias'}"
  echo ""
  echo "Examples:"
  [[ -n "$FIRST_ALIAS" ]] && echo "  ddev syncfiles $FIRST_ALIAS"
  [[ -n "$SECOND_ALIAS" ]] && echo "  ddev syncfiles -y $SECOND_ALIAS"
  echo ""
  echo "Note: Requires SSH access to Silta environments via jump host."
  echo "      Uses --delete flag which removes local files not on remote."
}

if [[ "$1" == "-h" || "$1" == "--help" ]]; then
  show_help
  exit 0
fi

# Parse arguments - separate drush flags from environment
DRUSH_FLAGS=""
ENV=""

for arg in "$@"; do
  case "$arg" in
    -y|--yes)
      DRUSH_FLAGS="$DRUSH_FLAGS -y"
      ;;
    -*)
      # Unknown flag - ignore or pass through
      ;;
    *)
      # Non-flag argument is the environment
      ENV="$arg"
      ;;
  esac
done

# Require environment argument
if [[ -z "$ENV" ]]; then
  echo "ERROR: Environment argument required."
  echo ""
  echo "Available environments:"
  get_aliases | sed 's/^/  /'
  echo ""
  echo "Usage: ddev syncfiles <environment>"
  echo "       ddev syncfiles --help"
  exit 1
fi

# Check if SSH agent has keys loaded
if ! ssh-add -l &>/dev/null; then
  echo "ERROR: No SSH keys available in the container."
  echo ""
  echo "Run this command on your host machine first:"
  echo "  ddev auth ssh"
  echo ""
  echo "This forwards your SSH agent to the DDEV container."
  exit 1
fi

echo "Syncing private files from @${ENV}..."

# Use DDEV_APPROOT for project path (defaults to /var/www/html in web container)
PRIVATE_DIR="${DDEV_APPROOT:-/var/www/html}/private"

# Ensure local private directory exists
mkdir -p "$PRIVATE_DIR"

# Use drush rsync to sync private files directory
# Pass through any additional arguments (e.g., -y for non-interactive)
drush $DRUSH_FLAGS rsync "@${ENV}:%private/" "$PRIVATE_DIR/" -- --archive --compress --progress --delete

echo "Private files synced from @${ENV}"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions