ataylorme / Advanced-WordPress-on-Pantheon

MIT License
65 stars 32 forks source link

Create a sync-local.sh script #43

Closed tobeycodes closed 5 years ago

tobeycodes commented 7 years ago

We can pull a copy of the database and files from Terminus. Then import with wp db import and rsync files

tobeycodes commented 7 years ago

I hacked this together for another project but I am not quite happy with it so would like to leave this here while I think of alternative/better ways to do it

#!/bin/bash

DATA_DIRECTORY="./data"
PANTHEON_SITE_UUID=ENTER-THIS-HERE

# Methods
terminusInstalled()
{
    command -v terminus >/dev/null 2>&1
}

terminusLoginCheck()
{
    USER_ID=$(terminus auth:whoami --field=id 2>&1)
    if [[ ! $USER_ID =~ ^[A-Za-z0-9-]+$ ]]; then
        echo "Please log into Terminus with terminus auth:login"
        exit 0
    fi
}

terminusGetDatabase()
{
    terminus backup:create $PANTHEON_SITE_UUID.dev --element=db
    DATABASE_URL=$(terminus backup:get $PANTHEON_SITE_UUID.dev --element=db)
    FILENAME=$(basename "$DATABASE_URL")
    FILENAME="${FILENAME%?Sig*}"
    curl -s -o $DATA_DIRECTORY/sql/$FILENAME $DATABASE_URL
    gunzip $DATA_DIRECTORY/sql/$FILENAME --force
    wp db import $DATA_DIRECTORY/sql/${FILENAME%.*}
}

terminusGetFiles()
{
    terminus backup:create $PANTHEON_SITE_UUID.dev --element=files
    FILES_URL=$(terminus backup:get $PANTHEON_SITE_UUID.dev --element=files)
    FILENAME=$(basename "$FILES_URL")
    FILENAME="${FILENAME%?Sig*}"
    curl -s -o $DATA_DIRECTORY/files/$FILENAME $FILES_URL
    tar -xzf $DATA_DIRECTORY/files/$FILENAME -C $DATA_DIRECTORY/files
    NEW_FOLDER=$DATA_DIRECTORY/files/${FILENAME%.*.*}
    mv $DATA_DIRECTORY/files/files_dev $NEW_FOLDER
    rm -rf $NEW_FOLDER/.htaccess $NEW_FOLDER/private
    rsync --ignore-existing --recursive $NEW_FOLDER/. ./web
}

# Check if Terminus is installed
terminusInstalled || { echo  "Terminus is not installed."; exit 1; }

# Check we are logged into Terminus
terminusLoginCheck

if [ ! -d "$DATA_DIRECTORY" ]; then
    mkdir -p $DATA_DIRECTORY
    mkdir -p $DATA_DIRECTORY/sql
    mkdir -p $DATA_DIRECTORY/files
fi

terminusGetDatabase
terminusGetFiles