Importing Your Site to Pantheon with Drush + Amazon S3 + Terminus

Moving a website can be tricky business! There are a lot of things you need to worry about just to even start moving. Each site is made up of PHP code, a MySQL database, and a variety of uploaded files which each need special treatment to be properly exported from an existing host. Pantheon tries to makes it easy to get running by providing Site Import Tools as part of our graphical user experience and while these tools work reasonably well, they still require a fair bit of clicking in the UI and require you to manage the various artifacts which each needed to be exported properly and then transferred to Pantheon.

Enter the Drupal Site Archive Format + Terminus

As Josh previously blogged, there is an easier way to import your site to Pantheon! You will need to register for a free Pantheon account on our website, but afterwards you can do the entire import process from the command line using three key technologies:

  • Drush - There is a standard Drupal Site Archive format for backing up a Drupal site that will bundle your code, files, and database into a single file! This format is supported by drush using the archive-dump option which will do all the heavy lifting for you and create one archive to rule them all.

  • Amazon S3 - This is a best of breed cloud file storage system which has a command line interface and can generate tokenized web accessible links. [Note - This is different then the official Amazon CLI, but works way better for large files.]

  • Terminus - Terminus is Pantheon’s Command Line Interface which allows developers command line access (via drush) to do anything you can do in the Pantheon dashboard and a few more things too. You will need to install Terminus, but afterwards you will be able to access Pantheon’s import functionality using thepantheon-site-import option.

drush archive-dump && aws s3 && terminus psite-import

Assuming you have Drush access to your website, have signed up for a Pantheon account, and you have Terminus installed, we are good to go. Here are the explicit steps on how to migrate your site to Pantheon:

1.) Export Your Existing Site Using Drush


$ drush archive-dump

Using the power of Drush, this will automagically package your code, database, and files into a standard format that will be placed in your drush-backups folder or anywhere you specify as the --destination in the command line parameters. After the archive is dumped, drush will let you know where it is by displayed with a message that looks like this: Archive saved to /tmp/getpantheon.20140224_225147.tar.gz.

2.) Put Your Archive on a Web Accessible URL Using Amazon S3

There are many ways to get a file on the internet, but one of my favorite ways is Amazon S3 which can be used for free and has both a web interface and command line options. The installation for the Amazon S3 tools is pretty easy and after some basic AWS configuration (secret key, region) uploading your site archive is a snap:


 

# File Path to Drupal Site Archive
ARCHIVEPATH=/tmp/mysite.20140225_034418.tar.gz
ACCESSKEY=MRRAJ5B7SFRYIEB4LERA
SECRETKEY=fJ34Y+3HsXE06VfGg4ffMqHJF5867YQQt/FgrRRaq

# Generated Variables
ARCHIVE=$(basename ${ARCHIVEPATH})
BUCKET="pantheon-$(date +%s)"  
EXPIRATION="$(($(date +%s) + 1000))" 
TOKEN="$(echo -en "GET\n\n\n${EXPIRATION}\n/${BUCKET}/${ARCHIVE}" | openssl dgst -sha1 -binary -hmac "${SECRETKEY}" | openssl base64 | xargs echo -n | perl -pe's/([^-_.~A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg')" 

# S3 Operatios: Create Bucket and Upload Drupal Site Archive
s3cmd mb s3://${BUCKET} 
s3cmd put ${ARCHIVEPATH} s3://${BUCKET}/${ARCHIVE} 

# Show URL of Uploaded Drupal Site Archive
echo "https://${BUCKET}.s3.amazonaws.com/${ARCHIVE}?AWSAccessKeyId=${ACCESSKEY}&Expires=${EXPIRATION}&Signature=${TOKEN}" 

This might take a little while depending on size of archive and speed of connection, but once the upload completes you will have a tokenized accessible web URL you can use to import your site to Pantheon. Always be sure to double check the URL, to make sure things worked and the URL is properly encoded.]

3.) Import Your Site to Pantheon Using Terminus

Importing your site into Pantheon is super simple using Terminus’ built in pantheon-site-import functionality. Simply authenticate into Terminus using the pantheon-auth function and then go to town: 


 

$ drush pantheon-site-import

Human readable label for the site []: My Great Import Site Machine
name of the site; used as part of the default URL [my-great-import-site]:
URL containing Drush archive: https://pantheon-1393301883.s3.amazonaws.com/getpantheon.20140225_041520.tar.gz?AWSAccessKeyId=AKIAd4353gdfdgKLTA&Expires=1393302883&Signature=lL2244n3ughfhgf28hhmmnGlc=

After a few minutes of processing, the import job should complete and Pantheon will let you know that your imported site is now ready to be managed in the dashboard and away you go experiencing the power of Pantheon.

Props to https://github.com/gdbtek/aws-tools for leveling up the URL tokenization code.

Topics Development, Education

Let’s get in touch

855-927-9387