Skip to content

Instantly share code, notes, and snippets.

@mattradford
Created April 27, 2018 15:49
Show Gist options
  • Save mattradford/111b4bac047bbcc3981b28d9519258e4 to your computer and use it in GitHub Desktop.
Save mattradford/111b4bac047bbcc3981b28d9519258e4 to your computer and use it in GitHub Desktop.
Pull a WP Engine install via SSH. File and DB.
    #!/bin/bash
    # Pull an install's files and DB from WP Engine
    # Variables
    lpath='app/public/'
    rpath='~/sites/'$install
    #User input
    read -p 'Repo to clone (blank if none): ' repo
    read -p 'Install to pull: ' install
    read -r -p 'Include uploads? (y/N) ' response
    read -p 'Local MySQL port: ' port
    # Move Local's wp-config.php out of the way
    mv app/public/wp-config.php app/wp-config.php
    if [ -z "$repo" ]
    then
            echo "$(tput setaf 1)No repo specified - skipping$(tput sgr 0)"
    else
            echo "$(tput setaf 2)'Cloning $repo$(tput sgr 0)"
        # Remove current WP install and clone repo
        rm -rf app/public
        git clone $repo app/public/
        cd app/public
        git checkout -b local
        rm -r *
        cd ../..
    fi
    # Rsync the remote files
    echo $(tput setaf 2)'Attempting to pull '$install$(tput sgr 0)
    remoteurl=$(wp --ssh=$install@$install.ssh.wpengine.net --quiet option get siteurl)
    echo $(tput setaf 2)'Syncing files from '$remoteurl$(tput sgr 0)
    case "$response" in
        [yY][eE][sS]|[yY])
            rsync -arvK --human-readable --include wp-content/uploads --exclude-from ~/scripts/.rsyncignore --delete-after $install@$install.ssh.wpengine.net:~/sites/$install/* ./$lpath
            ;;
        *)
            rsync -arvK --human-readable --exclude-from ~/scripts/.rsyncignore --delete-after $install@$install.ssh.wpengine.net:~/sites/$install/* ./$lpath
        ;;
    esac
    # Move wp-config back (otherwise WP CLI fails)
    mv app/wp-config.php app/public/wp-config.php
    # Export and import the DB
    echo $(tput setaf 2)'Exporting remote DB'$(tput sgr 0)
    wp --ssh=$install@$install.ssh.wpengine.net --path=sites/$install db export - > local.sql
    echo $(tput setaf 2)'Importing '$install' DB to Local'$(tput sgr 0)
    wp config set DB_HOST 192.168.75.100:$port --path=$lpath --quiet
    localurl=$(wp --path='app/public' option get siteurl)
    wp db reset --yes --quiet --path=$lpath
    wp db import local.sql --path=$lpath
    echo $(tput setaf 2)'Running search and replace from '$remoteurl' to '$localurl$(tput sgr 0)
    wp search-replace $remoteurl $localurl --path=$lpath
    wp config set DB_HOST localhost --path=$lpath --quiet
    rm local.sql
    # Best WP_DEBUG
    wp config set WP_DEBUG true --type=constant --raw --path=$lpath --quiet
    wp config set WP_DEBUG_LOG true --type=constant --raw --path=$lpath --quiet
    wp config set WP_DEBUG_DISPLAY --type=constant true --raw --path=$lpath --quiet
    if [ -z "$repo" ]
    then
        echo $(tput setaf 2)'All done'$(tput sgr 0)
    else
        # Commit files
        cd app/public
        git add .
        git commit -m "Pulled from Live"
        cd ../..
        echo $(tput setaf 2)'All done'$(tput sgr 0)
    fi
    # Open default browser
    open $localurl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment