On this page

Continuing the idea from my last post. Here's my script to sync a Pantheon's project files to a local folder.

#!/bin/bash

# Default values
PANTHEON_SITE=""
LOCAL_PROJECT_FOLDER=""
ENVIRONMENT="live" # Default environment

# Parse command line options
while [ "$#" -gt 0 ]; do
  case "$1" in
    --pantheon-site=*)
      PANTHEON_SITE="${1#*=}"
      ;;
    --local-project-folder=*)
      LOCAL_PROJECT_FOLDER="${1#*=}"
      ;;
    --env=*)
      ENVIRONMENT="${1#*=}"
      ;;
    *)
      echo "Unknown option: $1"
      exit 1
      ;;
  esac
  shift
done

# Check if required options are set
if [ -z "$PANTHEON_SITE" ] || [ -z "$LOCAL_PROJECT_FOLDER" ]; then
  echo "Usage: $0 --pantheon-site=<PANTHEON_SITE> --local-project-folder=<LOCAL_PROJECT_FOLDER> [--env=<ENVIRONMENT>]"
  exit 1
fi

# Create a backup of the files
echo "Creating a backup of the $ENVIRONMENT files..."
terminus backup:create --element=files $PANTHEON_SITE.$ENVIRONMENT

echo "Downloading $ENVIRONMENT files from Pantheon..."
# Download the files from the specified environment
FILES_ZIP_FILE="$HOME/pantheon-local-copies/files/$PANTHEON_SITE-files.tgz"
terminus backup:get --element=files --to=$FILES_ZIP_FILE $PANTHEON_SITE.$ENVIRONMENT

# Check if the files archive exists before extracting
if [ -f "$FILES_ZIP_FILE" ]; then
  echo "Extracting files to the local project folder..."
  gunzip -c $FILES_ZIP_FILE | tar -xv --strip-components=1 -C "$HOME/Documents/Projects/$LOCAL_PROJECT_FOLDER/web/app/uploads"
else
  echo "Error: Files archive not found at $FILES_ZIP_FILE"
  exit 1
fi

echo "Cleaning up..."
# Remove the dump file after import
rm $FILES_ZIP_FILE

echo "Files synchronization complete."

Then I add a command alias to my zsh settings

alias syncpfiles="~/scripts/sync-db.sh"

And I run the command as the following

syncpfiles --pantheon-site=testname --local-project-folder=testname --env=live