step( 30 ); $line = sprintf( '%5.1f%% %s', $s['percent'], $s['message'] ); if ( $line !== $last ) { WP_CLI::log( $line ); $last = $line; } if ( 'done' === $s['status'] ) { foreach ( $s['warnings'] as $w ) { WP_CLI::warning( $w ); } return $s; } if ( 'paused' === $s['status'] ) { $e = $s['error']; WP_CLI::error( $e['title'] . ': ' . $e['message'] . "\n How to fix:\n - " . implode( "\n - ", $e['fix'] ) . ( $e['details'] ? "\n Details: " . $e['details'] : '' ) ); } } } /** * Create a full .spinix backup. * * ## OPTIONS * * [--password=] * : Encrypt the backup. * * [--exclude-media] * [--exclude-plugins] * [--exclude-themes] * [--exclude-db] * [--exclude-cache] * [--note=] * * @param array $args Args. * @param array $assoc Options. */ public function backup( $args, $assoc ) { $opt = Spinix_Backup_Job::sanitize_options( array( 'password' => isset( $assoc['password'] ) ? $assoc['password'] : '', 'exclude_media' => isset( $assoc['exclude-media'] ), 'exclude_plugins' => isset( $assoc['exclude-plugins'] ), 'exclude_themes' => isset( $assoc['exclude-themes'] ), 'exclude_db' => isset( $assoc['exclude-db'] ), 'exclude_cache' => isset( $assoc['exclude-cache'] ), 'note' => isset( $assoc['note'] ) ? $assoc['note'] : 'WP-CLI', ) ); try { $s = $this->drive( Spinix_Backup_Job::start( $opt ) ); } catch ( Spinix_Error $e ) { WP_CLI::error( $e->getMessage() ); return; } WP_CLI::success( 'Backup created: ' . Spinix_Storage::base_dir() . '/' . $s['result']['file'] . ' (' . $s['result']['size_h'] . ')' ); } /** * Restore a backup from the Spinix folder (or a path, which is copied there). * * ## OPTIONS * * * : Backup file name or path. * * [--password=] * [--yes] * * @param array $args Args. * @param array $assoc Options. */ public function restore( $args, $assoc ) { $file = $args[0]; if ( file_exists( $file ) && basename( $file ) !== $file ) { Spinix_Storage::ensure(); $dest = Spinix_Storage::base_dir() . '/' . basename( $file ); if ( realpath( $file ) !== realpath( $dest ) && ! copy( $file, $dest ) ) { WP_CLI::error( 'Could not copy the backup into ' . Spinix_Storage::base_dir() ); } $file = basename( $file ); } WP_CLI::confirm( 'This will replace this website with the backup. Continue?', $assoc ); $opt = Spinix_Restore_Job::sanitize_options( array( 'file' => $file, 'password' => isset( $assoc['password'] ) ? $assoc['password'] : '', ) ); try { $r = new Spinix_Archive_Reader( Spinix_Storage::backup_path( $opt['file'] ) ); if ( $r->encrypted() ) { $r->unlock( $opt['password'] ); } $r->close(); $s = $this->drive( Spinix_Restore_Job::start( $opt ) ); } catch ( Spinix_Error $e ) { WP_CLI::error( $e->getMessage() ); return; } WP_CLI::success( $s['result']['summary'] ); } /** * Export the database as a .sql file (like mysqldump, but needs no shell access). * * ## OPTIONS * * [--gzip] * * @subcommand export-db * @param array $args Args. * @param array $assoc Options. */ public function export_db( $args, $assoc ) { try { $s = $this->drive( Spinix_Export_Job::start( Spinix_Export_Job::sanitize_options( array( 'kind' => 'database', 'gzip' => isset( $assoc['gzip'] ), ) ) ) ); } catch ( Spinix_Error $e ) { WP_CLI::error( $e->getMessage() ); return; } WP_CLI::success( 'Database exported: ' . Spinix_Storage::exports_dir() . '/' . $s['result']['download'] ); } /** * List backups. * * @subcommand list */ public function list_() { $rows = array(); foreach ( Spinix_Storage::list_backups() as $b ) { $rows[] = array( 'name' => $b['name'], 'size' => $b['size_h'], 'created' => gmdate( 'Y-m-d H:i', $b['created'] ), 'source' => $b['source'], 'encrypted' => $b['encrypted'] ? 'yes' : 'no', 'status' => $b['status'], ); } WP_CLI\Utils\format_items( 'table', $rows, array( 'name', 'size', 'created', 'source', 'encrypted', 'status' ) ); } /** * Verify a backup's integrity. * * * : Backup file name. * * [--password=] * * @param array $args Args. * @param array $assoc Options. */ public function verify( $args, $assoc ) { try { $s = $this->drive( Spinix_Verify_Job::start( array( 'file' => $args[0], 'password' => isset( $assoc['password'] ) ? $assoc['password'] : '', ) ) ); } catch ( Spinix_Error $e ) { WP_CLI::error( $e->getMessage() ); return; } WP_CLI::success( $s['result']['summary'] ); } } WP_CLI::add_command( 'spinix', 'Spinix_CLI' );