1 backup.inc | backup_prepare($backup_target_name, $backup_target, array &$backup_settings, array &$errors) |
Prepare for a single backup process, ensuring requirements are met.
Parameters
string $backup_target_name: The name of the backup target, used as the base name of the created backup file, such as "db_default" or "config_active".
string $backup_target: The target to be backed up (the source). This is usually an indicator such as "db:default" (for the database) or "config:active" (for config).
array $backup_settings: An array of backup settings. This value is modified by reference.
array $errors: Any errors that were encountered while preparing for the backup. This value is modified by reference.
Return value
boolean: TRUE if the backup is ready to run, FALSE if errors were encountered.
File
- core/
includes/ backup.inc, line 421 - Contains functionality related to creating and restoring site backups.
Code
function backup_prepare($backup_target_name, $backup_target, array &$backup_settings, array &$errors) {
// Populate the destination directory if not specified.
$default_directory = backup_get_backup_directory();
$timestamp = isset($backup_settings['timestamp']) ? $backup_settings['timestamp'] : REQUEST_TIME;
$date = date('Y-m-d\TH-i-s', $timestamp);
// Ensure required properties are set and at the top of the settings.
$backup = array(
'name' => $backup_target_name,
'target' => $backup_target,
// Set to TRUE when the backup is finished.
'completed' => FALSE,
'settings' => $backup_settings,
);
// Set the backup directory if needed.
if (!isset($backup_settings['backup_directory'])) {
// This should never occur as the Backup step is disabled if the
// $settings['backup_directory'] variable is empty.
if (empty($default_directory)) {
$errors[] = t('No backup can be created because the !variable variable is not set in !file.', array(
'!variable' => '<code>$settings[\'backup_directory\']</code>',
'!file' => '<code>settings.php</code>',
));
return FALSE;
}
$backup_settings['backup_directory'] = $default_directory . '/' . $date;
}
$backup_directory = $backup_settings['backup_directory'];
$directory_name = basename($backup_directory);
// Ensure the destination backup directory exists and is writable.
$directory_writeable = file_prepare_directory($backup_directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
if (!$directory_writeable) {
$errors[] = t('The destination backup directory %directory is not writable.', array(
'%directory' => $backup_directory,
));
return FALSE;
}
// Multiple backups may be placed in the same directory, such as multiple
// databases or the database + config. Check if this is adding to an existing
// backup directory.
$backup_info_file = $backup_directory . '/' . $directory_name . '.backup.json';
if (file_exists($backup_info_file)) {
$backup_info = backdrop_json_decode(file_get_contents($backup_info_file));
}
else {
$backup_info = array(
'timestamp' => $timestamp,
'name' => $directory_name,
'label' => isset($backup_settings['label']) ? $backup_settings['label'] : $directory_name,
'description' => isset($backup_settings['description']) ? $backup_settings['description'] : '',
'backup_directory' => $backup_directory,
'targets' => array(),
);
}
// Add information about this specific backup to the directory, excluding the
// values that are shared.
$backup['settings'] = array_diff_key($backup['settings'], $backup_info);
$backup_info['targets'][$backup_target_name] = $backup;
// Save the backup settings as a JSON file for restore.php to read later.
// This also guarantees that the destination is writable by attempting to
// put a file in it before the real backup.
$json_success = file_put_contents($backup_info_file, backdrop_json_encode($backup_info, TRUE));
if (!$json_success) {
$errors[] = t('The backup settings file could not be saved into the %directory backup directory.', array(
'%directory' => $backup_directory,
));
return FALSE;
}
return TRUE;
}