You are here

function backup_migrate_destination_nodesquirrel::save_file in Backup and Migrate 6.2

Same name and namespace in other branches
  1. 8.3 includes/destinations.nodesquirrel.inc \backup_migrate_destination_nodesquirrel::save_file()
  2. 6.3 includes/destinations.nodesquirrel.inc \backup_migrate_destination_nodesquirrel::save_file()
  3. 7.2 includes/destinations.nodesquirrel.inc \backup_migrate_destination_nodesquirrel::save_file()

Save to the NodeSquirrel destination.

Overrides backup_migrate_destination::save_file

File

includes/destinations.nodesquirrel.inc, line 166
Functions to handle the NodeSquirrel backup destination.

Class

backup_migrate_destination_nodesquirrel
A destination for sending database backups to the NodeSquirel backup service.

Code

function save_file($file, $settings) {
  if ($destination = $this
    ->_get_destination()) {
    srand((double) microtime() * 1000000);
    $filename = $file
      ->filename();
    $filesize = filesize($file
      ->filepath());
    $ticket = $this
      ->_xmlrpc('backups.getUploadTicket', array(
      $destination,
      $filename,
      $filesize,
      $file->file_info,
    ));
    if ($ticket) {
      $url = $ticket['url'];

      // If the ticket requires authentication add our username/password to the url.
      if (!empty($ticket['auth']) && ($ticket['auth'] = 'basic')) {
        $parts = parse_url($ticket['url']);
        list($parts['user'], $parts['pass']) = $this
          ->get_user_pass();
        $url = $this
          ->glue_url($parts, FALSE);
      }
      $out = $this
        ->_post_file($url, 'POST', $ticket['params'], $file);
      if ($out->code == 200) {

        // Confirm the upload.
        $confirm = $this
          ->_xmlrpc('backups.confirmUpload', array(
          $destination,
          $filename,
          $filesize,
        ));
        if ($confirm['success']) {

          // Set a message with a link to the manage console.
          $url = variable_get('nodesquirrel_manage_url', 'http://manage.nodesquirrel.com') . '/backups/' . $this
            ->_get_destination();
          _backup_migrate_message('Your backup has been saved to your NodeSquirrel account. View it at !account', array(
            '!account' => l($url, $url),
          ));
          return $file;
        }
        else {
          _backup_migrate_message('The backup file never made it to the NodeSquirrel backup server. There may have been a network problem. Please try again later');
        }
      }
      else {
        $error = !empty($out->headers['x-bams-error']) ? $out->headers['x-bams-error'] : $out->error;
        _backup_migrate_message('The NodeSquirrel server returned the following error: %err', array(
          '%err' => $error,
        ), 'error');
      }
    }
    else {
      if ($err = xmlrpc_error()) {

        // XMLRPC errors are already handled by the server function below.
      }
      else {
        _backup_migrate_message('The NodeSquirrel server refused the backup but did not specify why. Maybe the server is down.');
      }
    }
  }
  return NULL;
}