/** * Run HM Backup * * @return null */ function hmbkp_do_backup() { // Make sure it's possible to do a backup if (!hmbkp_possible()) { return; } // Clean up any mess left by a previous backup hmbkp_cleanup(); HM_Backup::get_instance()->backup(); hmbkp_set_status(__('Removing old backups', 'hmbkp')); // Delete any old backup files hmbkp_delete_old_backups(); if (file_exists(hmbkp_path() . '/.backup_running')) { unlink(hmbkp_path() . '/.backup_running'); } if (file_exists(HM_Backup::get_instance()->archive_filepath())) { $file = hmbkp_path() . '/.backup_complete'; if (!($handle = @fopen($file, 'w'))) { return; } fwrite($handle, HM_Backup::get_instance()->archive_filename()); fclose($handle); } hmbkp_email_backup(); }
function hmbkp_backup_complete($backup) { if ($backup->errors()) { hmbkp_cleanup(); $file = hmbkp_path() . '/.backup_errors'; if (file_exists($file)) { unlink($file); } if (!($handle = @fopen($file, 'w'))) { return; } fwrite($handle, json_encode($backup->errors())); fclose($handle); } elseif ($backup->warnings()) { $file = hmbkp_path() . '/.backup_warnings'; if (file_exists($file)) { unlink($file); } if (!($handle = @fopen($file, 'w'))) { return; } fwrite($handle, json_encode($backup->warnings())); fclose($handle); } }
/** * 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')); } }
/** * Move the backup directory and all existing backup files to a new * location * * @param string $from path to move the backups dir from * @param string $to path to move the backups dir to * @return void */ function hmbkp_path_move($from, $to) { if (!trim(untrailingslashit(trim($from))) || !trim(untrailingslashit(trim($to)))) { return; } // Create the new directory if it doesn't exist if (is_writable(dirname($to)) && !is_dir($to)) { wp_mkdir_p($to); } // Bail if we couldn't if (!is_dir($to) || !is_writable($to)) { return false; } update_option('hmbkp_path', $to); // Bail if the old directory doesn't exist if (!is_dir($from)) { return false; } // Cleanup before we start moving things hmbkp_cleanup(); // Move any existing backups if ($handle = opendir($from)) { while (false !== ($file = readdir($handle))) { if (pathinfo($file, PATHINFO_EXTENSION) === 'zip') { if (!@rename(trailingslashit($from) . $file, trailingslashit($to) . $file)) { copy(trailingslashit($from) . $file, trailingslashit($to) . $file); } } } closedir($handle); } // Only delete the old directory if it's inside WP_CONTENT_DIR if (strpos($from, WP_CONTENT_DIR) !== false) { hmbkp_rmdirtree($from); } }
/** * Dismiss an error and then redirect * back to the backups page */ function hmbkp_dismiss_error() { if (empty($_GET['action']) || $_GET['action'] !== 'hmbkp_dismiss_error') { return; } hmbkp_cleanup(); wp_redirect(remove_query_arg('action'), 303); die; }
function __construct($args, $assoc_args) { // Make sure it's possible to do a backup if (hmbkp_is_safe_mode_active()) { WP_CLI::error('Backup not possible when php is running safe_mode on'); return false; } remove_action('hmbkp_backup_started', 'hmbkp_set_status', 10, 0); remove_action('hmbkp_mysqldump_started', 'hmbkp_set_status_dumping_database'); remove_action('hmbkp_archive_started', 'hmbkp_set_status_archiving'); add_action('hmbkp_mysqldump_started', function () { WP_CLI::line('Backup: Dumping database...'); }); add_action('hmbkp_archive_started', function () { WP_CLI::line('Backup: Zipping everything up...'); }); // Clean up any mess left by a previous backup hmbkp_cleanup(); $hm_backup = HM_Backup::get_instance(); if (!empty($assoc_args['path'])) { $hm_backup->path = $assoc_args['path']; } if (!empty($assoc_args['root'])) { $hm_backup->root = $assoc_args['root']; } if (!is_dir($hm_backup->path()) && (!is_writable(dirname($hm_backup->path())) || !mkdir($hm_backup->path())) || !is_writable($hm_backup->path())) { WP_CLI::error('Invalid backup path'); return false; } if (!is_dir($hm_backup->root()) || !is_readable($hm_backup->root())) { WP_CLI::error('Invalid root path'); return false; } // Default to both $hm_backup->files_only = false; $hm_backup->database_only = false; if (!empty($assoc_args['files_only'])) { $hm_backup->files_only = true; } if (!empty($assoc_args['database_only'])) { $hm_backup->database_only = true; } if (!empty($assoc_args['mysqldump_command_path'])) { $hm_backup->mysqldump_command_path = empty($assoc_args['mysqldump_command_path']) || $assoc_args['mysqldump_command_path'] === 'false' ? false : true; } if (!empty($assoc_args['zip_command_path'])) { $hm_backup->zip_command_path = empty($assoc_args['zip_command_path']) || $assoc_args['zip_command_path'] === 'false' ? false : true; } if (!empty($assoc_args['excludes'])) { $hm_backup->excludes = $valid_rules = array_filter(array_map('trim', explode(',', $assoc_args['excludes']))); } $hm_backup->backup(); WP_CLI::line('Backup: Deleting old backups...'); // Delete any old backup files hmbkp_delete_old_backups(); if (file_exists(HM_Backup::get_instance()->archive_filepath())) { WP_CLI::success('Backup Complete: ' . HM_Backup::get_instance()->archive_filepath()); } else { WP_CLI::error('Backup Failed'); } }
/** * Dismiss an error and then redirect back to the backups page */ function hmbkp_dismiss_error() { // TODO Should really be nonced if (empty($_GET['action']) || $_GET['action'] !== 'hmbkp_dismiss_error') { return; } hmbkp_cleanup(); wp_safe_redirect(hmbkp_get_settings_url(), 303); die; }