Using WordShell as a backup/recovery tool

WordShell has various features that mean it can be used as a backup and recovery tool. Future versions will complete this support – backup/recovery will be a first-class feature of WordShell.

Mirroring files

Whenever WordShell performs an operation, it first synchronises its local cache for the files in question, if it has not been done in the last 3 hours. i.e. When you perform a plugin operation (whether installing, deleting, listing, updating or any other operation), all your plugins are synchronised to WordShell’s cache. WordShell’s cache lives by default in ~/.wordshell/current/<sitename>

The command-line switch –sync can be used to force a synchronisation. Therefore, the following command will synchronise the local mirror for all plugins for a given site:

wordshell mysite --plugin --sync

Or back up your uploaded files (wp-content/uploads):

wordshell mysite --content --sync

By the way, UpdraftPlus is a well-featured backup plugin which we maintain, free, which can schedule backups for you into Amazon S3, Google Drive or FTP.

And the following command will synchronise the local mirror for all plugins, theme, core and uploads for a given site:

wordshell all --everything --sync

You can add the above commands into cron jobs to make sure that your site is backed up regularly. Note, of course, that since the above operation is a mirror, that if your website is hacked, then the mirror will then be a hacked version. You should additionally use something like rdiff-backup or duplicity to keep versioned backups of WordShell’s mirror (which is by default in ~/.wordshell/current).

Note that “plugins, themes, core and uploads” does not include all your WordPress files – it excludes any directories in wp-content which third-party plugins may have created. It also excludes the uploaded content for WordPress Network (multi-site) blogs. For now, you need to back up those directories separately.

Restoring your files from the mirror and cloning a site

To copy the mirrored files into a particular directory, you can use normal POSIX filesystem commands, using the directory ~/.wordshell/current/<site>, or you can use WordShell’s –restore  feature, specifying a directory to copy to:

wordshell mysite --core --restore=/path/to/directory

The above command will first install a pristine copy of the relevant version of WordPress into the directory, and then add in any modifications or changes you have made (including your wp-config.php file).

The path specified is the base of the WordPress installation; so, if you are recovering plugins, then you do not include wp-content/plugins. e.g. to copy a single plugin:

wordshell mysite akismet --plugin --restore=/path/to/directory

That command will leave you with a copy of akismet in  /path/to/directory/wp-content/plugins/akismet

To restore all the files, use this:

wordshell mysite --restore=/path/to/directory --everything

You can also specify an FTP or SFTP path, e.g.:

wordshell mysite --restore=ftp://myuser:[email protected]/path/to/directory --everything

Note that by default, –restore does not synchronise the local mirror first. This is in case your site was corrupt (which is a common reason for a –restore operation). If your site is not corrupt and you want to first ensure that the local mirror is up to date, then add –sync. This command has the effect of cloning the files of the indicated site:

wordshell mysite --sync  --restore=/path/to/directory --everything

The above commands all deal with the current mirror of a site. If you want to deal with previous files/versions that were later replaced, then use the –rollback option (see the relevant manual page). Rollbacks can presently only be unpacked into the live site (not into custom directories as with –restore); however, you can go into ~/.wordpress/old* to access the raw backup sets, which are stored simply as plain zip files and can be manipulated using standard tools (zip, unzip).

Backing up your database (some options below are since version 1.3.50):

 See the full manual page for dealing with your WordPress database here.

Backing up your WordPress database is simple. The following command will make sure WordShell’s internal copy of your database is up to date (which means, that it has been sync-ed within the last 3 hours):

wordshell mysite --database --refreshcache

Add –sync to that command line  if you wish to enforce a new sync, even if the present copy was up to date.

When a new copy is saved, then the previous copy will be stored as a “rollback”. Thus you can keep versions of your database going back as far and at whatever intervals you like. You can list them like so:

wordshell mysite --database --listrollbacks

If you have a need, then database backups must be restored to a site by hand; WordShell does not yet have an option to restore to a live site. That is not hard – just take one of the rollbacks (from ~/.wordshell/old.database) or the most recent version (from ~/.wordshell/current/<site>/database) and import it using PHPMyAdmin, Adminer or the mysql command-line.

If you wish to write out your database to a file, then use this:

wordshell mysite --remotecli=dbdump >mysite-sql-28Sep2012.sql

You can use standard shell piping to immediately clone your database into a new database as follows:

echo "CREATE DATABASE mynewdatabase" | mysql

wordshell mysite --remotecli=dbdump | mysql mynewdatabase

You can easily use a cron job to schedule your backups, e.g.

wordshell all --database --refreshcache

Or an alternative approach: this command will output a quasi-SQL file, where each valid SQL file (one for each site) is separated by a “Site: (name)” header:

wordshell all --remotecli=dbdump > alldatabases-sql-28Sep2012.sql

 

Restoring your database

Presently the easiest way to restore your database is to import the SQL file using mysql (if you have access to the command line), or by installing something like Adminer or PHPMyAdmin if not. e.g.

mysql -h mysql.example.com -u myuser -p mydatabase < mysite-sql-28Sep2012.sql

Your backed up databases which WordShell has stored internally, if you have ever made any (using –database –refreshcache) are found in ~/.wordshell/old.database and ~/.wordshell/current/<site>/database.

Cloning a WordPress site

Therefore, if you want to quickly clone a site, then WordShell can make it a lot quicker. This is great for duplicating sites to make a testing environment from a live site:

# Copy (most of) the files

wordshell mysite --sync --refreshcache --everything
wordshell mysite --restore=/new/path --everything

# Copy the database

wordshell mysite --remotecli=dbdump | mysql mynewdatabase

Remember that the above commands omit the files stored in wp-content (other than the plugins and themes – wp-content/uploads is the main one, as that contains all your uploaded images etc.) and you will need to copy those separately. Furthermore, the above commands will not adjust any URLs stored in the database for you (e.g. images in stories) – to do that, see the relevant manual page – and you will also need to tweak wp-config.php to match your new database settings.

Next manual chapter: Debugging / developing / internals

Previous manual chapter: Working with the WordPress database

Back to manual index

 

Leave a Reply