Skip to content

Commit

Permalink
new command archive
Browse files Browse the repository at this point in the history
  • Loading branch information
mircobabini committed Aug 25, 2021
1 parent c972501 commit c57136f
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions command.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,46 @@ public function test( array $args = [], array $assoc_args = [] ) {
WP_CLI::success( "Success. Installed version $current_version." );
}

/**
* Create a zip archive for the site, including a sql dump
*
* ## EXAMPLES
*
* wp swim archive
*
* @subcommand archive
*/
public function archive( array $args = [], array $assoc_args = [] ) {
$archive_root = ABSPATH;

$source_domain = parse_url( get_site_url(), PHP_URL_HOST );
$source_domain = ltrim( $source_domain, 'www.' );

$datetime = date( 'YmdHis' );

$target_filepath = "$source_domain-$datetime.zip";

// subcommand options
$options = array(
'return' => true, // Return 'STDOUT'; use 'all' for full object.
'parse' => 'json', // Parse captured STDOUT to JSON array.
'launch' => false, // Reuse the current process.
'exit_error' => true, // Halt script execution on error.
);

// db dump
$database_filename = "database-$datetime.sql";
WP_CLI::runcommand( "wp db export $database_filename", $options );

// create the zip archive
exec( "zip -r $target_filepath $archive_root -x 'wp-content/cache*'" );

// delete db dump
@unlink( "$archive_root/$database_filename" );

WP_CLI::success( "Success. Archive $target_filepath." );
}

/**
* Make the website accessible via www.
*
Expand Down

0 comments on commit c57136f

Please sign in to comment.