示例#1
0
 function post_backup($fail_mode = false, $cancel_backup = false)
 {
     pb_backupbuddy::status('message', __('Cleaning up after backup.', 'it-l10n-backupbuddy'));
     // Delete temporary data directory.
     if (file_exists($this->_backup['temp_directory'])) {
         pb_backupbuddy::status('details', __('Removing temp data directory.', 'it-l10n-backupbuddy'));
         pb_backupbuddy::$filesystem->unlink_recursive($this->_backup['temp_directory']);
     }
     // Delete temporary ZIP directory.
     if (file_exists(backupbuddy_core::getBackupDirectory() . 'temp_zip_' . $this->_backup['serial'] . '/')) {
         pb_backupbuddy::status('details', __('Removing temp zip directory.', 'it-l10n-backupbuddy'));
         pb_backupbuddy::$filesystem->unlink_recursive(backupbuddy_core::getBackupDirectory() . 'temp_zip_' . $this->_backup['serial'] . '/');
     }
     if (true === $fail_mode) {
         pb_backupbuddy::status('warning', 'Backup archive limiting has been skipped since there was an error to avoid deleting potentially good backups to make room for a potentially bad backup.');
     } else {
         $this->trim_old_archives();
         // Clean up any old excess archives pushing us over defined limits in settings.
     }
     if (true === $cancel_backup) {
         pb_backupbuddy::status('details', 'Backup stopped so deleting backup ZIP file.');
         $unlink_result = @unlink($this->_backup['archive_file']);
         if (true === $unlink_result) {
             pb_backupbuddy::status('details', 'Deleted stopped backup file.');
         } else {
             pb_backupbuddy::status('error', 'Unable to delete stopped backup file. You should delete it manually as it may be damaged from stopping mid-backup. File to delete: `' . $this->_backup['archive_file'] . '`.');
         }
         $this->_backup['finish_time'] = -1;
         //pb_backupbuddy::save();
         $this->_backup_options->save();
     } else {
         // Not cancelled.
         $this->_backup['archive_size'] = filesize($this->_backup['archive_file']);
         pb_backupbuddy::status('details', __('Final ZIP file size', 'it-l10n-backupbuddy') . ': ' . pb_backupbuddy::$format->file_size($this->_backup['archive_size']));
         pb_backupbuddy::status('action', 'archive_size^' . pb_backupbuddy::$format->file_size($this->_backup['archive_size']));
         if ($fail_mode === false) {
             // Not cancelled and did not fail so mark finish time.
             pb_backupbuddy::$options['last_backup_finish'] = time();
             pb_backupbuddy::save();
         }
     }
     if ($this->_backup['trigger'] == 'manual') {
         // No more manual notifications. Removed Feb 2012 before 3.0.
     } elseif ($this->_backup['trigger'] == 'scheduled') {
         if (false === $fail_mode && false === $cancel_backup) {
             pb_backupbuddy::status('details', __('Sending scheduled backup complete email notification.', 'it-l10n-backupbuddy'));
             $message = 'completed successfully in ' . pb_backupbuddy::$format->time_duration(time() - $this->_backup['start_time']) . ".\n";
             backupbuddy_core::mail_notify_scheduled($this->_backup['serial'], 'complete', __('Scheduled backup', 'it-l10n-backupbuddy') . ' "' . $this->_backup['schedule_title'] . '" ' . $message);
         }
     } else {
         pb_backupbuddy::status('error', 'Error #4343434. Unknown backup trigger.');
     }
     pb_backupbuddy::status('message', __('Finished cleaning up.', 'it-l10n-backupbuddy'));
     if (true === $cancel_backup) {
         pb_backupbuddy::status('details', 'Backup cancellation complete.');
         return false;
     } else {
         if (true === $fail_mode) {
             pb_backupbuddy::status('details', __('As this backup did not pass the integrity check you should verify it manually or re-scan. Integrity checks can fail on good backups due to permissions, large file size exceeding memory limits, etc. You may manually disable integrity check on the Settings page but you will no longer be notified of potentially bad backups.', 'it-l10n-backupbuddy'));
         } else {
             pb_backupbuddy::status('action', 'archive_url^' . basename($this->_backup['archive_file']));
         }
     }
     return true;
 }
 function post_backup($fail_mode = false, $cancel_backup = false)
 {
     pb_backupbuddy::status('message', __('Cleaning up after backup.', 'it-l10n-backupbuddy'));
     // Delete temporary data directory.
     if (file_exists($this->_backup['temp_directory'])) {
         pb_backupbuddy::status('details', __('Removing temp data directory.', 'it-l10n-backupbuddy'));
         pb_backupbuddy::$filesystem->unlink_recursive($this->_backup['temp_directory']);
     }
     // Delete temporary ZIP directory.
     if (file_exists(backupbuddy_core::getBackupDirectory() . 'temp_zip_' . $this->_backup['serial'] . '/')) {
         pb_backupbuddy::status('details', __('Removing temp zip directory.', 'it-l10n-backupbuddy'));
         pb_backupbuddy::$filesystem->unlink_recursive(backupbuddy_core::getBackupDirectory() . 'temp_zip_' . $this->_backup['serial'] . '/');
     }
     if (true === $fail_mode) {
         pb_backupbuddy::status('warning', 'Backup archive limiting has been skipped since there was an error to avoid deleting potentially good backups to make room for a potentially bad backup.');
     } else {
         $this->trim_old_archives();
         // Clean up any old excess archives pushing us over defined limits in settings.
     }
     if (true === $cancel_backup) {
         pb_backupbuddy::status('details', 'Backup stopped so deleting backup ZIP file.');
         $unlink_result = @unlink($this->_backup['archive_file']);
         if (true === $unlink_result) {
             pb_backupbuddy::status('details', 'Deleted stopped backup file.');
         } else {
             pb_backupbuddy::status('error', 'Unable to delete stopped backup file. You should delete it manually as it may be damaged from stopping mid-backup. File to delete: `' . $this->_backup['archive_file'] . '`.');
         }
         $this->_backup['finish_time'] = -1;
         //pb_backupbuddy::save();
         $this->_backup_options->save();
     } else {
         // Not cancelled.
         $this->_backup['archive_size'] = @filesize($this->_backup['archive_file']);
         pb_backupbuddy::status('details', __('Final ZIP file size', 'it-l10n-backupbuddy') . ': ' . pb_backupbuddy::$format->file_size($this->_backup['archive_size']));
         pb_backupbuddy::status('archiveSize', pb_backupbuddy::$format->file_size($this->_backup['archive_size']));
         if ($fail_mode === false) {
             // Not cancelled and did not fail so mark finish time.
             //error_log( print_r( $this->_backup_options->options, true ) );
             $archiveFile = basename($this->_backup_options->options['archive_file']);
             // Calculate backup download URL, if any.
             //$downloadURL = pb_backupbuddy::ajax_url( 'download_archive' ) . '&backupbuddy_backup=' . $archiveFile;
             $downloadURL = '';
             $abspath = str_replace('\\', '/', ABSPATH);
             // Change slashes to handle Windows as we store backup_directory with Linux-style slashes even on Windows.
             $backup_dir = str_replace('\\', '/', backupbuddy_core::getBackupDirectory());
             if (FALSE !== stristr($backup_dir, $abspath)) {
                 // Make sure file to download is in a publicly accessible location (beneath WP web root technically).
                 //pb_backupbuddy::status( 'details', 'mydir: `' . $backup_dir . '`, abs: `' . $abspath . '`.');
                 $sitepath = str_replace($abspath, '', $backup_dir);
                 $downloadURL = rtrim(site_url(), '/\\') . '/' . trim($sitepath, '/\\') . '/' . $archiveFile;
             }
             $integrityIsOK = '-1';
             if (isset($this->_backup_options->options['integrity']['is_ok'])) {
                 $integrityIsOK = $this->_backup_options->options['integrity']['is_ok'];
             }
             $destinations = array();
             foreach ($this->_backup_options->options['steps'] as $step) {
                 if ('send_remote_destination' == $step['function']) {
                     $destinations[] = array('id' => $step['args'][0], 'title' => pb_backupbuddy::$options['remote_destinations'][$step['args'][0]]['title'], 'type' => pb_backupbuddy::$options['remote_destinations'][$step['args'][0]]['type']);
                 }
             }
             pb_backupbuddy::status('details', 'Updating statistics for last backup completed and number of edits since last backup.');
             $finishTime = time();
             pb_backupbuddy::$options['last_backup_finish'] = $finishTime;
             pb_backupbuddy::$options['last_backup_stats'] = array('archiveFile' => $archiveFile, 'archiveURL' => $downloadURL, 'archiveSize' => $this->_backup['archive_size'], 'start' => pb_backupbuddy::$options['last_backup_start'], 'finish' => $finishTime, 'type' => $this->_backup_options->options['profile']['type'], 'profileTitle' => htmlentities($this->_backup_options->options['profile']['title']), 'scheduleTitle' => $this->_backup_options->options['schedule_title'], 'integrityStatus' => $integrityIsOK, 'destinations' => $destinations);
             //error_log( print_r( pb_backupbuddy::$options['last_backup_stats'], true ) );
             pb_backupbuddy::$options['edits_since_last'] = 0;
             // Reset edit stats for notifying user of how many posts/pages edited since last backup happened.
             pb_backupbuddy::save();
         }
     }
     backupbuddy_core::cleanTempDir();
     if ($this->_backup['trigger'] == 'manual') {
         // Do nothing. No notifications as of pre-3.0 2012.
     } elseif ($this->_backup['trigger'] == 'deployment') {
         // Do nothing. No notifications.
     } elseif ($this->_backup['trigger'] == 'deployment_pulling') {
         // Do nothing.
     } elseif ($this->_backup['trigger'] == 'scheduled') {
         if (false === $fail_mode && false === $cancel_backup) {
             pb_backupbuddy::status('details', __('Sending scheduled backup complete email notification.', 'it-l10n-backupbuddy'));
             $message = 'completed successfully in ' . pb_backupbuddy::$format->time_duration(time() - $this->_backup['start_time']) . ".\n";
             backupbuddy_core::mail_notify_scheduled($this->_backup['serial'], 'complete', __('Scheduled backup', 'it-l10n-backupbuddy') . ' "' . $this->_backup['schedule_title'] . '" ' . $message);
         }
     } else {
         pb_backupbuddy::status('error', 'Error #4343434. Unknown backup trigger `' . $this->_backup['trigger'] . '`.');
     }
     pb_backupbuddy::status('message', __('Finished cleaning up.', 'it-l10n-backupbuddy'));
     if (true === $cancel_backup) {
         pb_backupbuddy::status('details', 'Backup cancellation complete.');
         return false;
     } else {
         if (true === $fail_mode) {
             pb_backupbuddy::status('details', __('As this backup did not pass the integrity check you should verify it manually or re-scan. Integrity checks can fail on good backups due to permissions, large file size exceeding memory limits, etc. You may manually disable integrity check on the Settings page but you will no longer be notified of potentially bad backups.', 'it-l10n-backupbuddy'));
         } else {
             if ($this->_backup['trigger'] != 'deployment' && $this->_backup['trigger'] != 'deployment_pulling') {
                 //$stats = stat( $this->_backup['archive_file'] );
                 //$sizeFormatted = pb_backupbuddy::$format->file_size( $stats['size'] );
                 pb_backupbuddy::status('archiveInfo', json_encode(array('file' => basename($this->_backup['archive_file']), 'url' => pb_backupbuddy::ajax_url('download_archive') . '&backupbuddy_backup=' . basename($this->_backup['archive_file']))));
             }
         }
     }
     return true;
 }
示例#3
0
 public static function send($destination_settings, $file, $send_id = '', $delete_after = false)
 {
     if (is_array($file)) {
         // As of v6.1.0.1 no longer accepting multiple files to send.
         $file = $file[0];
     }
     if ('' != $send_id) {
         pb_backupbuddy::add_status_serial('remote_send-' . $send_id);
         pb_backupbuddy::status('details', '----- Initiating master send function for BackupBuddy v' . pb_backupbuddy::settings('version') . '. Post-send deletion: ' . $delete_after);
         require_once pb_backupbuddy::plugin_path() . '/classes/fileoptions.php';
         $fileoptions_file = backupbuddy_core::getLogDirectory() . 'fileoptions/send-' . $send_id . '.txt';
         if (!file_exists($fileoptions_file)) {
             //pb_backupbuddy::status( 'details', 'Fileoptions file `' . $fileoptions_file . '` does not exist yet; creating.' );
             //pb_backupbuddy::status( 'details', 'Fileoptions instance #19.' );
             $fileoptions_obj = new pb_backupbuddy_fileoptions($fileoptions_file, $read_only = false, $ignore_lock = true, $create_file = true);
         } else {
             //pb_backupbuddy::status( 'details', 'Fileoptions file exists; loading.' );
             //pb_backupbuddy::status( 'details', 'Fileoptions instance #18.' );
             $fileoptions_obj = new pb_backupbuddy_fileoptions($fileoptions_file, $read_only = false, $ignore_lock = false, $create_file = false);
         }
         if (true !== ($result = $fileoptions_obj->is_ok())) {
             pb_backupbuddy::status('error', __('Fatal Error #9034.2344848. Unable to access fileoptions data.', 'it-l10n-backupbuddy') . ' Error: ' . $result);
             return false;
         }
         //pb_backupbuddy::status( 'details', 'Fileoptions data loaded.' );
         $fileoptions =& $fileoptions_obj->options;
         if ('' == $fileoptions) {
             // Set defaults.
             $fileoptions = backupbuddy_core::get_remote_send_defaults();
             $fileoptions['type'] = $destination_settings['type'];
             $fileoptions['file'] = $file;
             $fileoptions['retries'] = 0;
         }
         $fileoptions['sendID'] = $send_id;
         $fileoptions['destinationSettings'] = $destination_settings;
         // always store the LATEST settings for resume info and retry function.
         $fileoptions['update_time'] = time();
         $fileoptions['deleteAfter'] = $delete_after;
         $fileoptions_obj->save();
         if (isset($fileoptions['status']) && 'aborted' == $fileoptions['status']) {
             pb_backupbuddy::status('warning', 'Destination send triggered on an ABORTED transfer. Ending send function.');
             return false;
         }
         unset($fileoptions_obj);
     }
     if (false === ($destination = self::_init_destination($destination_settings))) {
         echo '{Error #546893498a. Destination configuration file missing.}';
         if ('' != $send_id) {
             pb_backupbuddy::remove_status_serial('remote_send-' . $send_id);
         }
         return false;
     }
     $destination_settings = $destination['settings'];
     // Settings with defaults applied, normalized, etc.
     if (!file_exists($file)) {
         pb_backupbuddy::status('error', 'Error #58459458743. The file that was attempted to be sent to a remote destination, `' . $file . '`, was not found. It either does not exist or permissions prevent accessing it. Check that local backup limits are not causing it to be deleted.');
         if ('' != $send_id) {
             pb_backupbuddy::remove_status_serial('remote_send-' . $send_id);
         }
         return false;
     }
     if (!method_exists($destination['class'], 'send')) {
         pb_backupbuddy::status('error', 'Destination class `' . $destination['class'] . '` does not support send operation -- missing function.');
         if ('' != $send_id) {
             pb_backupbuddy::remove_status_serial('remote_send-' . $send_id);
         }
         return false;
     }
     global $pb_backupbuddy_destination_errors;
     $pb_backupbuddy_destination_errors = array();
     $result = call_user_func_array("{$destination['class']}::send", array($destination_settings, $file, $send_id, $delete_after));
     /* $result values:
      *		false		Transfer FAILED.
      *		true		Non-chunked transfer succeeded.
      *		array()		array(
      *						multipart_id,				// Unique string ID for multipart send. Empty string if last chunk finished sending successfully.
      *						multipart_status_message
      *					)
      */
     if ($result === false) {
         $error_details = implode('; ', $pb_backupbuddy_destination_errors);
         if ('' != $error_details) {
             $error_details = ' Details: ' . $error_details;
         }
         $log_directory = backupbuddy_core::getLogDirectory();
         $preError = 'There was an error sending to the remote destination titled `' . $destination_settings['title'] . '` of type `' . backupbuddy_core::pretty_destination_type($destination_settings['type']) . '`. One or more files may have not been fully transferred. Please see error details for additional information. If the error persists, enable full error logging and try again for full details and troubleshooting. Details: ' . "\n\n";
         $logFile = $log_directory . 'status-remote_send-' . $send_id . '_' . pb_backupbuddy::$options['log_serial'] . '.txt';
         pb_backupbuddy::status('details', 'Looking for remote send log file to send in error email: `' . $logFile . '`.');
         if (!file_exists($logFile)) {
             pb_backupbuddy::status('details', 'Remote send log file not found.');
             backupbuddy_core::mail_error($preError . $error_details);
         } else {
             // Log exists. Attach.
             pb_backupbuddy::status('details', 'Remote send log file found. Attaching to error email.');
             backupbuddy_core::mail_error($preError . $error_details . "\n\nSee the attached log for details.", '', array($logFile));
         }
         // Save error details into fileoptions for this send.
         //pb_backupbuddy::status( 'details', 'About to load fileoptions data.' );
         require_once pb_backupbuddy::plugin_path() . '/classes/fileoptions.php';
         pb_backupbuddy::status('details', 'Fileoptions instance #45.');
         $fileoptions_obj = new pb_backupbuddy_fileoptions(backupbuddy_core::getLogDirectory() . 'fileoptions/send-' . $send_id . '.txt', $read_only = false, $ignore_lock = false, $create_file = false);
         if (true !== ($fileoptions_result = $fileoptions_obj->is_ok())) {
             pb_backupbuddy::status('error', __('Error #9034.32731. Unable to access fileoptions data.', 'it-l10n-backupbuddy') . ' Error: ' . $fileoptions_result);
         }
         //pb_backupbuddy::status( 'details', 'Fileoptions data loaded.' );
         $fileoptions =& $fileoptions_obj->options;
         $fileoptions['status'] = 'failed';
         $fileoptions['error'] = 'Error sending.' . $error_details;
         $fileoptions['updated_time'] = time();
         $fileoptions_obj->save();
         unset($fileoptions_obj);
     }
     if (is_array($result)) {
         // Send is multipart.
         pb_backupbuddy::status('details', 'Multipart chunk mode completed a pass of the send function. Resuming will be needed. Result: `' . print_r($result, true) . '`.');
         if ('' != $send_id) {
             //pb_backupbuddy::status( 'details', 'About to load fileoptions data.' );
             require_once pb_backupbuddy::plugin_path() . '/classes/fileoptions.php';
             //pb_backupbuddy::status( 'details', 'Fileoptions instance #17.' );
             $fileoptions_obj = new pb_backupbuddy_fileoptions(backupbuddy_core::getLogDirectory() . 'fileoptions/send-' . $send_id . '.txt', $read_only = false, $ignore_lock = false, $create_file = false);
             if (true !== ($fileoptions_result = $fileoptions_obj->is_ok())) {
                 pb_backupbuddy::status('error', __('Fatal Error #9034.387462. Unable to access fileoptions data.', 'it-l10n-backupbuddy') . ' Error: ' . $fileoptions_result);
                 return false;
             }
             //pb_backupbuddy::status( 'details', 'Fileoptions data loaded.' );
             $fileoptions =& $fileoptions_obj->options;
             $fileoptions['_multipart_status'] = $result[1];
             $fileoptions['updated_time'] = time();
             pb_backupbuddy::status('details', 'Destination debugging details: `' . print_r($fileoptions, true) . '`.');
             $fileoptions_obj->save();
             unset($fileoptions_obj);
             pb_backupbuddy::status('details', 'Next multipart chunk will be processed shortly. Now waiting on its cron...');
         }
     } else {
         // Single all-at-once send.
         if (false === $result) {
             pb_backupbuddy::status('details', 'Completed send function. Failure. Post-send deletion will be skipped if enabled.');
         } elseif (true === $result) {
             pb_backupbuddy::status('details', 'Completed send function. Success.');
         } else {
             pb_backupbuddy::status('warning', 'Completed send function. Unknown result: `' . $result . '`.');
         }
         pb_backupbuddy::status('details', 'About to load fileoptions data.');
         require_once pb_backupbuddy::plugin_path() . '/classes/fileoptions.php';
         pb_backupbuddy::status('details', 'Fileoptions instance #16.');
         $fileoptions_obj = new pb_backupbuddy_fileoptions(backupbuddy_core::getLogDirectory() . 'fileoptions/send-' . $send_id . '.txt', $read_only = false, $ignore_lock = false, $create_file = false);
         if (true !== ($fileoptions_result = $fileoptions_obj->is_ok())) {
             pb_backupbuddy::status('error', __('Error #9034.387462. Unable to access fileoptions data.', 'it-l10n-backupbuddy') . ' Error: ' . $fileoptions_result);
         }
         pb_backupbuddy::status('details', 'Fileoptions data loaded.');
         $fileoptions =& $fileoptions_obj->options;
         $fileoptions['updated_time'] = time();
         unset($fileoptions_obj);
     }
     // File transfer completely finished successfully.
     if (true === $result) {
         $fileSize = filesize($file);
         $serial = backupbuddy_core::get_serial_from_file($file);
         // Handle deletion of send file if enabled.
         if (true === $delete_after && false !== $result) {
             pb_backupbuddy::status('details', __('Post-send deletion enabled.', 'it-l10n-backupbuddy'));
             if (false === $result) {
                 pb_backupbuddy::status('details', 'Skipping post-send deletion since transfer failed.');
             } else {
                 pb_backupbuddy::status('details', 'Performing post-send deletion since transfer succeeded.');
                 pb_backupbuddy::status('details', 'Deleting local file `' . $file . '`.');
                 // Handle post-send deletion on success.
                 if (file_exists($file)) {
                     $unlink_result = @unlink($file);
                     if (true !== $unlink_result) {
                         pb_backupbuddy::status('error', 'Unable to unlink local file `' . $file . '`.');
                     }
                 }
                 if (file_exists($file)) {
                     // File still exists.
                     pb_backupbuddy::status('details', __('Error. Unable to delete local file `' . $file . '` after send as set in settings.', 'it-l10n-backupbuddy'));
                     backupbuddy_core::mail_error('BackupBuddy was unable to delete local file `' . $file . '` after successful remove transfer though post-remote send deletion is enabled. You may want to delete it manually. This can be caused by permission problems or improper server configuration.');
                 } else {
                     // Deleted.
                     pb_backupbuddy::status('details', __('Deleted local archive after successful remote destination send based on settings.', 'it-l10n-backupbuddy'));
                     pb_backupbuddy::status('archiveDeleted', '');
                 }
             }
         } else {
             pb_backupbuddy::status('details', 'Post-send deletion not enabled.');
         }
         // Send email notification if enabled.
         if ('' != pb_backupbuddy::$options['email_notify_send_finish']) {
             pb_backupbuddy::status('details', __('Sending finished destination send email notification.', 'it-l10n-backupbuddy'));
             $extraReplacements = array();
             $extraReplacements = array('{backup_file}' => $file, '{backup_size}' => $fileSize, '{backup_serial}' => $serial);
             backupbuddy_core::mail_notify_scheduled($serial, 'destinationComplete', __('Destination send complete to', 'it-l10n-backupbuddy') . ' ' . backupbuddy_core::pretty_destination_type($destination_settings['type']), $extraReplacements);
         } else {
             pb_backupbuddy::status('details', __('Finished sending email NOT enabled. Skipping.', 'it-l10n-backupbuddy'));
         }
         // Save notification of final results.
         $data = array();
         $data['serial'] = $serial;
         $data['file'] = $file;
         $data['size'] = $fileSize;
         $data['pretty_size'] = pb_backupbuddy::$format->file_size($fileSize);
         backupbuddy_core::addNotification('remote_send_success', 'Remote file transfer completed', 'A file has successfully completed sending to a remote location.', $data);
     }
     // NOTE: Call this before removing status serial so it shows in log.
     pb_backupbuddy::status('details', 'Ending send() function pass.');
     // Return logging to normal file.
     if ('' != $send_id) {
         pb_backupbuddy::remove_status_serial('remote_send-' . $send_id);
     }
     return $result;
 }