From c57136fcd2781e2b39a1c19cce0862ee1fd56aa8 Mon Sep 17 00:00:00 2001 From: Mirco Babini Date: Wed, 25 Aug 2021 16:00:21 +0200 Subject: [PATCH] new command archive --- command.php | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/command.php b/command.php index dcc35b5..a801dfc 100644 --- a/command.php +++ b/command.php @@ -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. *