You are here

public function BackupMigrate::restore in Backup and Migrate 8.4

Perform the restore to a given source loading it from the given file in the given destination.

Parameters

string $source_id The id of the source to restore:

string $destination_id The id of the destination to read the backup from.:

string $file The ID of the file to be restored. Only optional when the destination: does not store multiple files (like browser upload)

Overrides BackupMigrateInterface::restore

File

lib/backup_migrate_core/src/Main/BackupMigrate.php, line 116

Class

BackupMigrate
The core Backup and Migrate service.

Namespace

BackupMigrate\Core\Main

Code

public function restore($source_id, $destination_id, $file_id = NULL) {
  try {

    // Get the source and the destination to use.
    $source = $this
      ->sources()
      ->get($source_id);
    $destination = $this
      ->destinations()
      ->get($destination_id);
    if (!$source) {
      throw new BackupMigrateException('The source !id does not exist.', [
        '!id' => $source_id,
      ]);
    }
    if (!$destination) {
      throw new BackupMigrateException('The destination !id does not exist.', [
        '!id' => $destination_id,
      ]);
    }

    // Load the file from the destination.
    $file = $destination
      ->getFile($file_id);
    if (!$file) {
      throw new BackupMigrateException('The file !id does not exist.', [
        '!id' => $file_id,
      ]);
    }

    // Prepare the file for reading.
    $file = $destination
      ->loadFileForReading($file);
    if (!$file) {
      throw new BackupMigrateException('The file !id could not be opened for reading.', [
        '!id' => $file_id,
      ]);
    }

    // Run each of the installed plugins which implements the 'backup' operation.
    $file = $this
      ->plugins()
      ->call('beforeRestore', $file);

    // Do the actual source restore.
    $import_result = $source
      ->importFromFile($file);
    if (!$import_result) {
      throw new BackupMigrateException('The file could not be imported.');
    }

    // Run each of the installed plugins which implements the 'beforeBackup' operation.
    $this
      ->plugins()
      ->call('afterRestore');

    // Let plugins react to a successful operation.
    $this
      ->plugins()
      ->call('restoreSucceed', $file);
  } catch (\Exception $e) {

    // Let plugins react to a failed operation.
    $this
      ->plugins()
      ->call('restoreFail', $e);

    // The consuming software needs to deal with this.
    throw $e;
  }
}