1 backup.inc backup_execute($backup_target_name, $backup_target, array $backup_settings)

Run a backup process.

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 settings that are read by the Backup class, modifying its behavior. Each Backup class may have different settings. Check the defaultSettings() method within each class for a full list.

Return value

BackupFile|false: The BackupFile referencing the newly created backup.

File

core/includes/backup.inc, line 515
Contains functionality related to creating and restoring site backups.

Code

function backup_execute($backup_target_name, $backup_target, array $backup_settings) {
  // Safety check that backup target has an associate backup handler.
  $backup_class = backup_get_handler_name($backup_target);
  if (!is_subclass_of($backup_class, 'Backup')) {
    return FALSE;
  }

  // If no filepath is set, a backup cannot be created.
  if (empty($backup_settings['backup_directory'])) {
    return FALSE;
  }
  $backup_directory = $backup_settings['backup_directory'];

  // Create the BackupFile instance to hold the backup output.
  $file = new BackupFile($backup_target_name, $backup_directory);

  /** @var Backup $backup_instance */
  $backup_instance = new $backup_class($backup_target_name, $backup_target, $backup_settings);

  // Pre and post steps are separate to allow offloading compression and
  // decompression into separate requests (eventually).
  $backup_instance->preBackup($file);
  $success = $backup_instance->backup($file);
  $backup_instance->postBackup($file);

  // Set the "completed" flag indicating this backup was successful.
  if ($success) {
    $directory_name = basename($backup_directory);
    $backup_info_file = $backup_directory . '/' . $directory_name . '.backup.json';
    $backup_info = backdrop_json_decode(file_get_contents($backup_info_file));
    $backup_info['targets'][$backup_target_name]['completed'] = TRUE;
    file_put_contents($backup_info_file, backdrop_json_encode($backup_info, TRUE));
  }

  if (!$success) {
    return FALSE;
  }

  return $file;
}