Skip to content

Instantly share code, notes, and snippets.

@sethta
Last active January 6, 2017 17:31
Show Gist options
  • Save sethta/0e2333d4203145a0737d2ff94f70e846 to your computer and use it in GitHub Desktop.
Save sethta/0e2333d4203145a0737d2ff94f70e846 to your computer and use it in GitHub Desktop.
WordPress mu-plugin to check if Stash has been stuck at Error #5002 for more than 24 hours, and then deletes catalog and state
<?php
/*
* Plugin Name: Backupbuddy Stash Fix
* Description: Deletes catalog and state if error #5002 has occurred
* Version: 1.0
* Author: Seth Alling
* Author URI: https://sethalling.com
* Requires at least: 4.7
* Tested up to: 4.7
*/
function sta_backupbuddy_stash_fix() {
// If logged in and in admin area and BackupBuddy classes exist
if ( is_admin() && is_user_logged_in() && class_exists( 'backupbuddy_core' ) && class_exists( 'pb_backupbuddy' ) ) {
// Only run if no transient set ( hasn't run in 12 hours )
if ( false === ( get_transient( 'backupbuddy_stash_check' ) ) ) {
// Get most recent notification (not sure which is most recent as I've seen both)
$last_notification = array_pop( get_option( 'pb_backupbuddy_notifications' ) );
$first_notification = array_shift( get_option( 'pb_backupbuddy_notifications' ) );
// Get 24 hours ago
$yesterday = date( 'U', strtotime( '-1 days' ) );
// Check most recent notifications if came after yesterday
if ( ( $first_notification['time'] > $yesterday ) || ( $last_notification['time'] > $yesterday ) ) {
// Set correct notification as recent notification
if ( $first_notification['time'] > $yesterday ) {
$recent_notification = $first_notification;
} else if ( $last_notification['time'] > $yesterday ) {
$recent_notification = $last_notification;
}
// Check if notification is error
if ( 'live_error' == $recent_notification['slug'] ) {
// Check if Error #5002
if ( preg_match( '/5002/', $recent_notification['message'] ) ) {
// Delete catalog
$catalogFile = backupbuddy_core::getLogDirectory() . 'live/catalog-' . pb_backupbuddy::$options['log_serial'] . '.txt';
@unlink( $catalogFile );
sleep( 1 );
@unlink( $catalogFile );
sleep( 1 );
@unlink( $catalogFile );
if ( file_exists( $catalogFile ) ) {
pb_backupbuddy::alert( 'Error #3927273: Unable to delete catalog file `' . $catalogFile . '`. Check permissions or manually delete.' );
} else {
pb_backupbuddy::alert( 'Catalog deleted.' );
}
// Delete state
$stateFile = backupbuddy_core::getLogDirectory() . 'live/state-' . pb_backupbuddy::$options['log_serial'] . '.txt';
@unlink( $stateFile );
sleep( 1 );
@unlink( $stateFile );
sleep( 1 );
@unlink( $stateFile );
if ( file_exists( $stateFile ) ) {
pb_backupbuddy::alert( 'Error #434554: Unable to delete state file `' . $stateFile . '`. Check permissions or manually delete.' );
} else {
pb_backupbuddy::alert( 'State file deleted.' );
}
}
}
}
// Set transient so next check won't happen for 12 hours
set_transient( 'backupbuddy_stash_check', true, 12 * HOUR_IN_SECONDS );
}
}
}
add_action( 'plugins_loaded', 'sta_backupbuddy_stash_fix' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment