jplew / SyncDB

Bash script meant to take the tedium out of deploying and updating database-driven (eg Wordpress) websites. It rapidly synchronizes local and remote versions of a MySQL database, performs the necessary search and replace queries, then synchronizes all your uploads/binaries.
MIT License
234 stars 41 forks source link

SyncDB v0.3.1

SyncDB is bash deploy script meant to take the tedium out of synchronizing local and remote versions of a Wordpress site. It allows developers working in a local environment (eg. MAMP) to rapidly "push" or "pull" changes to or from their production server with a single terminal command.

SyncDB synchronizes your sites by executing a sequence of shell commands. It uses mysql and mysqldump for your database, rsync for your uploads folder, and leaves it up to you to synchronize your themes, plugins, etc (I use git for this).

Search and replacing the site URLs in the database is handled by the CLI version of InterConnect IT's invaluable Wordpress Search and Replace Tool.

After initial script configuration, you'll be able to migrate your entire Wordpress database and uploads folder by running SyncDB from the command line like so:

./syncdb

No more FTP programs, no more PHPMyAdmin, no more tears. SyncDB don't mess around.

Usage

  1. Copy syncdb to the root of your site and make sure it's executable (chmod +x syncdb).

    • This is important, as the script must be located in the same folder as your wp-config.php (and your local-config.php, if you're doing it the smart way (Mark Jaquith's WP-Skeleton).
  2. Copy syncdb-config to the same location and configure it.

    • This step involves manually editing the variables in the config file using your favorite text editor. This has to be done once for each local/remote site pair. All the settings are documented via comments.
  3. Run syncdb command from the command line: ./syncdb [command]

SyncDB works in either direction. It can "push" your changes to the remote server, or "pull" updates from it.

To push, run:

./syncdb

or

./syncdb push

To pull, run:

./syncdb pull

For finer control, you can tell SyncDB to execute only the command of your choosing by passing the function name as an argument. See List of Commands below.

List of Commands

Below is a list of all of SyncDB's commands. They are meant to be passed one at a time like so:

./syncdb backup_local_db

All commands are meant to be executed locally, unless noted with (remote only).

Complete workflow

I wrote this script due to my recurring need to migrate Wordpress sites. I required a workflow which would automate the process as much as possible, with minimal intervention. This script, therefore, plays one part in my overall Wordpress development workflow. Below is my set-up:

Local

Remote

Common

The Model

This workflow is based on the following model. Generally speaking, there are three categories of content which make a database-driven website tick:

  1. "Code"

    • Contains: themes, scripts, plugins, config files etc...
    • Location: FTP
    • GUI Management tool: FTP program
    • Command-line Management tool: git
  2. "Media"

    • Contains: whatever is stored in your uploads directory: images, audio, video, docs etc...
    • Location: FTP
    • GUI Management tool: FTP program
    • Command-line Management tool: rsync
  3. "SQL data"

    • Contains: whatever is stored in your database: posts, pages, options, users, etc...
    • Location: MySQL database
    • GUI Management tool: PHPMyAdmin
    • Command-line Management tool: syncdb (this script)

This script handles the migration of content categories 2) and 3). Part 1), the code, is managed in a Git repository on the remote server. Once this workflow has been enabled, the complete migration process looks like this:

Pushing:

 ./syncdb
 git push hub master

Pulling:

 ./syncdb pull
 git pull hub/master

Easy!

Changelog

v0.3.1 (May 27, 2014)

v0.3 (May 22, 2014)

v0.22 (May 2, 2014)

v0.21 (Apr 4, 2014)

v0.2 (Mar 16, 2014)

v0.1 (Oct 2013)

Feedback & Bugs

SyncDB may be a little edgy—it has not seen a broad range of use cases. There are billions of possible configurations—depending on your OS, your Wordpress configuration, your hosting company etc...—SyncDB tries to accommodate the most common ones. We're committed to making it as robust as possible, so if something's not working please submit an issue.

SyncDB is presently compatible with Wordpress, but in principle should work with Drupal, Joomla, etc... Right now it scrapes the wp-config.php file for database login info. All that would be required to support those other CMS's would be to specify the config file and grep it accordingly.

Suggestions and pull-requests are welcome.

Disclaimer

We accept no responsibility for any data loss suffered from the use of SyncDB. Use at your own risk. SyncDB explicitly backs up your SQL databases before making any changes, but you'll need basic familiarity with the shell in order to revert things should anything go awry.