Shuttling code between development, staging, and production environments is one of the most boring tasks web developers have to face. Not only is it tedious, but it’s error prone if you miss a file. A CMS like Drupal or WordPress can have environment specific assets in its directories that you don’t want to overwrite or delete. Fortunately, this is a highly automatable task and being good lazy programmers, we find a way to script our deployments. Some people deploy automatically using continuous integration, others leverage hooks in git or subversion, and even plain old rsync can be sufficient.
What do you do when do you don’t have shell access? We’re using zippykid.com to host a WordPress site for a client. ZippyKid provides SFTP access to let you upload your theme and plugins for your site. Luckily, LFTP, a highly scriptable FTP client also supports SFTP. The script below uses it’s mirroring capabilities to update changed files automatically.
# Deploys theme and selected plugins.
# @author firstname.lastname@example.org
# SFTP access credentials
# script assumes it lives in the directory above your web document root
# local theme directory
# remote theme directory
# local plugin directory
# remote plugin directory
echo "Uploading files to zippykid";
# call lftp, upload theme and selected plugins
lftp -c "open sftp://$HOST
user $USER $PASS
mirror --only-newer --reverse --delete --verbose $THEME $TG_THEME
mirror --only-newer --reverse --delete --verbose $PLUGINS/key-issues $TG_PLUGINS/key-issues
mirror --only-newer --reverse --delete --verbose $PLUGINS/board-members $TG_PLUGINS/board-members
Since we’re putting the username and password in the script, you’ll want to ensure that only trusted users can access and execute it. You could also create a user just for the script to use.
The magic happens when LFTP is invoked. The -c switch tells it to execute the commands that follow as if you had typed them into the FTP client. First we connect (open) using the sftp protocol to our server and then authenticate. The mirror lines with the –reverse swtich instruct LFTP to mirror our local path to the remote path. The –delete swtich tells it to remove remote files that are not present locally. The –only-newer switch speeds things up by only updating changed files while the –verbose switch will provide progress feedback.
This script only uploads our custom theme and two custom plugins. ZippyKid manages the install and you can upgrade WordPress itself using the built in upgrade tool.