/** * Generate some posts. * * ## OPTIONS * * [--files_only] * : Backup files only, default to off * * [--database_only] * : Backup database only, defaults to off * * [--path] * : dir that the backup should be save in, defaults to wp-content/backups/ * * [--root] * : dir that should be backed up, defaults to site root. * * [--zip_command_path] * : path to your zip binary, standard locations are automatically used * * [--mysqldump_command_path] * : path to your mysqldump binary, standard locations are automatically used * * ## Usage * * wp backupwordpress backup [--files_only] [--database_only] [--path<dir>] [--root<dir>] [--zip_command_path=<path>] [--mysqldump_command_path=<path>] */ public function backup($args, $assoc_args) { // Make sure it's possible to do a backup if (HM_Backup::is_safe_mode_active()) { WP_CLI::error(sprintf(__('BackUpWordPress may not work when php is running with %s on', 'hmbkp'), 'safe_mode')); } add_action('hmbkp_mysqldump_started', function () { WP_CLI::line(__('Backup: Dumping database...', 'hmbkp')); }); add_action('hmbkp_archive_started', function () { WP_CLI::line(__('Backup: Zipping everything up...', 'hmbkp')); }); // Clean up any mess left by a previous backup hmbkp_cleanup(); $hm_backup = new HM_Backup(); if (!empty($assoc_args['path'])) { $hm_backup->set_path($assoc_args['path']); } if (!empty($assoc_args['root'])) { $hm_backup->set_root($assoc_args['root']); } if (!is_dir($hm_backup->get_path()) && (!is_writable(dirname($hm_backup->get_path())) || !wp_mkdir_p($hm_backup->get_path())) || !is_writable($hm_backup->get_path())) { WP_CLI::error(__('Invalid backup path', 'hmbkp')); return false; } if (!is_dir($hm_backup->get_root()) || !is_readable($hm_backup->get_root())) { WP_CLI::error(__('Invalid root path', 'hmbkp')); return false; } if (!empty($assoc_args['files_only'])) { $hm_backup->set_type('file'); } if (!empty($assoc_args['database_only'])) { $hm_backup->set_type('database'); } if (isset($assoc_args['mysqldump_command_path'])) { $hm_backup->set_mysqldump_command_path($assoc_args['mysqldump_command_path']); } if (isset($assoc_args['zip_command_path'])) { $hm_backup->set_zip_command_path($assoc_args['zip_command_path']); } if (!empty($assoc_args['excludes'])) { $hm_backup->set_excludes($assoc_args['excludes']); } $hm_backup->backup(); // Delete any old backup files //hmbkp_delete_old_backups(); if (file_exists($hm_backup->get_archive_filepath())) { WP_CLI::success(__('Backup Complete: ', 'hmbkp') . $hm_backup->get_archive_filepath()); } else { WP_CLI::error(__('Backup Failed', 'hmbkp')); } }
/** * Perform a Backup. * * ## OPTIONS * * [--files_only] * : Backup files only, default to off * * [--database_only] * : Backup database only, defaults to off * * [--path] * : dir that the backup should be save in, defaults to your existing backups directory * * [--root] * : dir that should be backed up, defaults to site root. * * [--zip_command_path] * : path to your zip binary, standard locations are automatically used * * [--mysqldump_command_path] * : path to your mysqldump binary, standard locations are automatically used * * ## Usage * * wp backupwordpress backup [--files_only] [--database_only] [--path<dir>] [--root<dir>] [--zip_command_path=<path>] [--mysqldump_command_path=<path>] * */ public function backup($args, $assoc_args) { add_action('hmbkp_mysqldump_started', function () { WP_CLI::line(__('Backup: Dumping database...', 'backupwordpress')); }); add_action('hmbkp_archive_started', function () { WP_CLI::line(__('Backup: Zipping everything up...', 'backupwordpress')); }); $hm_backup = new HM_Backup(); if (!empty($assoc_args['path'])) { HMBKP_Path::get_instance()->set_path($assoc_args['path']); } $hm_backup->set_path(HMBKP_Path::get_instance()->get_path()); HMBKP_Path::get_instance()->cleanup(); if (!empty($assoc_args['root'])) { $hm_backup->set_root($assoc_args['root']); } if (!is_dir($hm_backup->get_path()) && (!is_writable(dirname($hm_backup->get_path())) || !wp_mkdir_p($hm_backup->get_path())) || !is_writable($hm_backup->get_path())) { WP_CLI::error(__('Invalid backup path', 'backupwordpress')); return false; } if (!is_dir($hm_backup->get_root()) || !is_readable($hm_backup->get_root())) { WP_CLI::error(__('Invalid root path', 'backupwordpress')); return false; } if (!empty($assoc_args['files_only'])) { $hm_backup->set_type('file'); } if (!empty($assoc_args['database_only'])) { $hm_backup->set_type('database'); } if (isset($assoc_args['mysqldump_command_path'])) { $hm_backup->set_mysqldump_command_path($assoc_args['mysqldump_command_path']); } if (isset($assoc_args['zip_command_path'])) { $hm_backup->set_zip_command_path($assoc_args['zip_command_path']); } if (!empty($assoc_args['excludes'])) { $hm_backup->set_excludes($assoc_args['excludes']); } $hm_backup->backup(); if (file_exists($hm_backup->get_archive_filepath())) { WP_CLI::success(__('Backup Complete: ', 'backupwordpress') . $hm_backup->get_archive_filepath()); } else { WP_CLI::error(__('Backup Failed', 'backupwordpress')); } }
/** * Handle the backups API calls * * @param string $call * @return mixed */ function _wprp_backups_api_call($action) { if (!class_exists('hm_backup')) { return new WP_Error('Backups module not present'); } switch ($action) { // TODO in the future we should do some check here to make sure they do support backups case 'supports_backups': return true; case 'do_backup': @ignore_user_abort(true); $backup = new HM_Backup(); $upload_dir = wp_upload_dir(); // Store the backup file in the uploads dir $backup->path = $upload_dir['basedir'] . '/_wpremote_backups'; $running_file = $backup->path . '/.backup_running'; // delete the backups folder to cleanup old backups _wprp_backups_rmdirtree($backup->path); if (!@mkdir($backup->path)) { return new WP_Error('unable-to-create-backups-directory', 'Unable to write the .backup_running file - check your permissions on wp-content/uploads'); } // write the backup runing file for tracking... if (!($handle = @fopen($running_file, 'w'))) { return new WP_Error('unable-to-write-backup-running-file'); } fwrite($handle, $backup->archive_filename()); fclose($handle); if (!file_exists($running_file)) { return new WP_Error('backup-running-file-was-not-created'); } // Set a random backup filename $backup->archive_filename = md5(time()) . '.zip'; // Excludes if (!empty($_REQUEST['backup_excludes'])) { $excludes = array_map('urldecode', (array) $_REQUEST['backup_excludes']); $backup->excludes = $excludes; } $backup->backup(); unlink($backup->path . '/.backup_completed'); unlink($backup->path . '/.backup_running'); // write the backup runing file for tracking... $completed_file = $backup->path . '/.backup_completed'; if (!($handle = @fopen($completed_file, 'w'))) { return new WP_Error('unable-to-write-backup-completed-file'); } if ($backup->errors() || $backup->warnings() && !file_exists($backup->archive_filepath())) { $errors = array_merge($backup->errors(), $backup->warnings()); fwrite($handle, json_encode($errors)); } else { fwrite($handle, 'file:' . $backup->archive_filename()); } fclose($handle); return true; case 'get_backup': $upload_dir = wp_upload_dir(); // Store the backup file in the uploads dir $path = $upload_dir['basedir'] . '/_wpremote_backups'; $url = $upload_dir['baseurl'] . '/_wpremote_backups'; if (!is_dir($path)) { return new WP_Error('backups-dir-does-not-exist'); } if (file_exists($path . '/.backup_running')) { return new WP_Error('backup-running'); } if (!file_exists($path . '/.backup_completed')) { return new WP_Error('backup-not-started'); } $file = file_get_contents($path . '/.backup_completed'); if (strpos($file, 'file:') === 0) { return $url . '/' . substr($file, 5); } // must have errored, return errors in a WP_Error return new WP_Error('backup-failed', json_decode($file)); case 'delete_backup': $upload_dir = wp_upload_dir(); _wprp_backups_rmdirtree($upload_dir['basedir'] . '/_wpremote_backups'); break; } }