Skip to content

Instantly share code, notes, and snippets.

@getsource
Created October 6, 2012 18:37
Show Gist options
  • Save getsource/3845744 to your computer and use it in GitHub Desktop.
Save getsource/3845744 to your computer and use it in GitHub Desktop.
WordCamp Vegas Simple-Sample wp-cli Backup Plugin
<?php
// Let WP_CLI know we exist!
// Earlier versions of wp-cli used WP_CLI::addCommand()
WP_CLI::add_command( 'wclv', 'WCLV_Backup_Command' );
/**
* The WCLV Backup Plugin
*
* @package WCLV_Backup
* @subpackage commands/community
* @maintainer Mike Schroder
*/
class WCLV_Backup_Command extends WP_CLI_Command {
/**
* Backup your WordPress install.
*
* @param array $args
* @param array $assoc_args
*/
function backup( $args, $assoc_args ) {
$filename = $dbname = null;
// If a filename isn't specified, default to "Site's Title.tar.gz".
if ( empty( $args ) )
$filename = '../' . escapeshellarg( get_bloginfo() ) . '.tar.gz';
else
$filename = $args[0];
// If --no-db is specified, don't include the database in backup
if ( ! isset( $assoc_args['no-db'] ) ) {
$dbname = '../database_temp.sql';
// This is cheating a bit, since wp-cli doesn't currently support
// running commands within commands without re-launching itself.
WP_CLI::run_command( array( 'db', 'export', $dbname ), array() );
}
// GZ/Tar and Backup the install!
WP_CLI::line( "Backing up to '$filename' ..." );
$result = WP_CLI::launch( "tar -zcvf $filename . $dbname", false );
// If we created a database backup, remove the temp file.
if ( $dbname && ! unlink( $dbname ) )
WP_CLI::warning( "Couldn't remove temporary database backup, '$dbname'." );
// Will automatically exit on WP_CLI::error, but not WP_CLI::success.
if ( 0 == $result ) {
WP_CLI::success( "Backup Complete." );
} else {
WP_CLI::error( "Backup Failed." );
}
}
/**
* Output syntax for command
*/
public static function help() {
WP_CLI::line( "usage: wp wclv backup [--no-db] [path/to/file]" );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment