/** * Perform a Backup. * * ## OPTIONS * * [--files_only] * : Backup files only, default to off * * [--database_only] * : Backup database only, defaults to off * * [--destination] * : 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 * * [--archive_filename] * : filename for the resulting zip file * * [--excludes] * : list of paths you'd like to exclude * * ## Usage * * wp backupwordpress backup [--files_only] [--database_only] [--path<dir>] [--root<dir>] [--zip_command_path=<path>] [--mysqldump_command_path=<path>] * * @todo errors should be bubbled from Backup, Scheduled_Backup and the like instead of being repeated. */ 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\BackUpWordPress\Backup(); if (!empty($assoc_args['destination'])) { Path::get_instance()->set_path($assoc_args['destination']); } HM\BackUpWordPress\Path::get_instance()->cleanup(); if (!empty($assoc_args['root'])) { $hm_backup->set_root($assoc_args['root']); } if (!is_dir(hmbkp_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 (isset($assoc_args['archive_filename'])) { $hm_backup->set_archive_filename($assoc_args['archive_filename']); } 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')); } }
function hmbkp_set_server_config_notices() { $notices = HM\BackUpWordPress\Notices::get_instance(); $messages = array(); if (!HM\BackUpWordPress\Backup::is_shell_exec_available()) { $php_user = '******'; $php_group = '<PHP GROUP>'; } else { $php_user = shell_exec('whoami'); $groups = explode(' ', shell_exec('groups')); $php_group = reset($groups); } if (!is_dir(hmbkp_path())) { $messages[] = sprintf(__('The backups directory can\'t be created because your %1$s directory isn\'t writable, run %2$s or %3$s or create the folder yourself.', 'backupwordpress'), '<code>' . esc_html(dirname(hmbkp_path())) . '</code>', '<code>chown ' . esc_html($php_user) . ':' . esc_html($php_group) . ' ' . esc_html(dirname(hmbkp_path())) . '</code>', '<code>chmod 777 ' . esc_html(dirname(hmbkp_path())) . '</code>'); } if (is_dir(hmbkp_path()) && !wp_is_writable(hmbkp_path())) { $messages[] = sprintf(__('Your backups directory isn\'t writable, run %1$s or %2$s or set the permissions yourself.', 'backupwordpress'), '<code>chown -R ' . esc_html($php_user) . ':' . esc_html($php_group) . ' ' . esc_html(hmbkp_path()) . '</code>', '<code>chmod -R 777 ' . esc_html(hmbkp_path()) . '</code>'); } if (HM\BackUpWordPress\Backup::is_safe_mode_active()) { $messages[] = sprintf(__('%1$s is running in %2$s, please contact your host and ask them to disable it. BackUpWordPress may not work correctly whilst %3$s is on.', 'backupwordpress'), '<code>PHP</code>', sprintf('<a href="%1$s">%2$s</a>', __('http://php.net/manual/en/features.safe-mode.php', 'backupwordpress'), __('Safe Mode', 'backupwordpress')), '<code>' . __('Safe Mode', 'backupwordpress') . '</code>'); } if (defined('HMBKP_PATH') && HMBKP_PATH) { // Suppress open_basedir warning https://bugs.php.net/bug.php?id=53041 if (!@file_exists(HMBKP_PATH)) { $messages[] = sprintf(__('Your custom path does not exist', 'backupwordpress')); } elseif (hmbkp_is_restricted_custom_path()) { $messages[] = sprintf(__('Your custom path is unreachable due to a restriction set in your PHP configuration (open_basedir)', 'backupwordpress')); } else { if (!@is_dir(HMBKP_PATH)) { $messages[] = sprintf(__('Your custom backups directory %1$s doesn\'t exist and can\'t be created, your backups will be saved to %2$s instead.', 'backupwordpress'), '<code>' . esc_html(HMBKP_PATH) . '</code>', '<code>' . esc_html(hmbkp_path()) . '</code>'); } if (@is_dir(HMBKP_PATH) && !wp_is_writable(HMBKP_PATH)) { $messages[] = sprintf(__('Your custom backups directory %1$s isn\'t writable, new backups will be saved to %2$s instead.', 'backupwordpress'), '<code>' . esc_html(HMBKP_PATH) . '</code>', '<code>' . esc_html(hmbkp_path()) . '</code>'); } } } $test_backup = new HM\BackUpWordPress\Backup(); if (!is_readable($test_backup->get_root())) { $messages[] = sprintf(__('Your site root path %s isn\'t readable.', 'backupwordpress'), '<code>' . $test_backup->get_root() . '</code>'); } if (count($messages) > 0) { $notices->set_notices('server_config', $messages, false); } }
/** * Check if a backup is possible with regards to file * permissions etc. * * @return bool */ function hmbkp_possible() { if (!wp_is_writable(hmbkp_path()) || !is_dir(hmbkp_path())) { return false; } $test_backup = new HM\BackUpWordPress\Backup(); if (!is_readable($test_backup->get_root())) { return false; } return true; }