| 1 |
<?php |
| 2 |
/** |
| 3 |
* Destination BlackBlaze. |
| 4 |
* |
| 5 |
* @package wpdbbkp |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; // Exit if accessed directly. |
| 10 |
} |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
add_action( 'wp_db_backup_completed', array( 'WPDatabaseBackupBB', 'wp_db_backup_completed' ) ); |
| 16 |
|
| 17 |
/** |
| 18 |
* WPDatabaseBackupBB Class. |
| 19 |
* |
| 20 |
* @class WPDatabaseBackupBB |
| 21 |
*/ |
| 22 |
class WPDatabaseBackupBB { |
| 23 |
|
| 24 |
// Function to upload files to Backblaze B2 |
| 25 |
public static function upload_backup_to_backblaze($file_path, $file_name) { |
| 26 |
$s3_token = get_transient('b2_authorization_token'); |
| 27 |
$api_url = get_transient('b2_api_url'); |
| 28 |
$bucket_id = get_option('wpdb_dest_bb_s3_bucket') ? get_option('wpdb_dest_bb_s3_bucket') : ''; |
| 29 |
if (!$s3_token) { |
| 30 |
$key_id = get_option('wpdb_dest_bb_s3_bucket_key') ? get_option('wpdb_dest_bb_s3_bucket_key') : ''; |
| 31 |
$app_key = get_option('wpdb_dest_bb_s3_bucket_secret') ? get_option('wpdb_dest_bb_s3_bucket_secret') : ''; |
| 32 |
|
| 33 |
$b2_authorize_url = "https://api.backblazeb2.com/b2api/v2/b2_authorize_account"; |
| 34 |
$credentials = base64_encode($key_id . ":" . $app_key); |
| 35 |
|
| 36 |
// Authorize account |
| 37 |
$response = wp_remote_get($b2_authorize_url, array( |
| 38 |
'headers' => array( |
| 39 |
'Authorization' => 'Basic ' . $credentials |
| 40 |
), |
| 41 |
'timeout' => 60 // Extend timeout |
| 42 |
)); |
| 43 |
|
| 44 |
if (is_wp_error($response)) { |
| 45 |
return array('success' => false, 'message' => esc_html__('Authorization request failed: ', 'wpdbbkp'). $response->get_error_message()); |
| 46 |
} |
| 47 |
|
| 48 |
$body = wp_remote_retrieve_body($response); |
| 49 |
$data = json_decode($body); |
| 50 |
|
| 51 |
if (empty($data->authorizationToken)) { |
| 52 |
return array('success' => false, 'message' => esc_html__('Failed to authorize with Backblaze.', 'wpdbbkp')); |
| 53 |
} |
| 54 |
|
| 55 |
$auth_token = $data->authorizationToken; |
| 56 |
$expiration = 1 * HOUR_IN_SECONDS; // 24 hours |
| 57 |
$upload_url = $data->apiUrl . '/b2api/v2/b2_get_upload_url'; |
| 58 |
|
| 59 |
set_transient('b2_authorization_token', $auth_token, $expiration); |
| 60 |
set_transient('b2_api_url', $data->apiUrl, $expiration); |
| 61 |
} else { |
| 62 |
$auth_token = $s3_token; |
| 63 |
$upload_url = $api_url . '/b2api/v2/b2_get_upload_url'; |
| 64 |
} |
| 65 |
|
| 66 |
// Get upload URL |
| 67 |
$response = wp_remote_post($upload_url, array( |
| 68 |
'body' => wp_json_encode(array('bucketId' => $bucket_id)), |
| 69 |
'headers' => array( |
| 70 |
'Authorization' => $auth_token, |
| 71 |
'Content-Type' => 'application/json' |
| 72 |
), |
| 73 |
'timeout' => 60 // Extend timeout |
| 74 |
)); |
| 75 |
|
| 76 |
if (is_wp_error($response)) { |
| 77 |
return array('success' => false, 'message' => esc_html__('Failed to get upload URL: ', 'wpdbbkp'). $response->get_error_message()); |
| 78 |
} |
| 79 |
|
| 80 |
$body = wp_remote_retrieve_body($response); |
| 81 |
$data = json_decode($body); |
| 82 |
|
| 83 |
if (isset($data->status) && $data->status != 200) return array('success' => false, 'message' => esc_html__('Failed to get upload URL: ' , 'wpdbbkp'). $data->message); |
| 84 |
|
| 85 |
if (empty($data->uploadUrl)) { |
| 86 |
return array('success' => false, 'message' => esc_html__('Failed to get upload URL from Backblaze.', 'wpdbbkp')); |
| 87 |
} |
| 88 |
|
| 89 |
$upload_url = $data->uploadUrl; |
| 90 |
$upload_auth_token = $data->authorizationToken; |
| 91 |
|
| 92 |
if (!file_exists($file_path)) { |
| 93 |
return array('success' => false, 'message' => esc_html__('File does not exist: ' , 'wpdbbkp'). $file_path); |
| 94 |
} |
| 95 |
|
| 96 |
$file_size = filesize($file_path); |
| 97 |
$file = fopen($file_path, 'r'); |
| 98 |
if ($file === false) { |
| 99 |
return array('success' => false, 'message' => esc_html__('Failed to open file: ' , 'wpdbbkp') . $file_path); |
| 100 |
} |
| 101 |
|
| 102 |
$file_contents = file_get_contents($file_path); |
| 103 |
if ($file_contents === false) { |
| 104 |
return array('success' => false, 'message' => esc_html__('Failed to read file: ', 'wpdbbkp') . $file_path); |
| 105 |
} |
| 106 |
|
| 107 |
$sha1_of_file_data = sha1($file_contents); |
| 108 |
$root_path = str_replace('\\', '/', ABSPATH); // Normalize to forward slashes for consistency |
| 109 |
$file_path = str_replace($root_path, '', $file_path); |
| 110 |
$file_path = ltrim($file_path, '/'); // Ensure there is no leading slash |
| 111 |
|
| 112 |
$response = wp_remote_post($upload_url, array( |
| 113 |
'body' => $file_contents, |
| 114 |
'headers' => array( |
| 115 |
'Authorization' => $upload_auth_token, |
| 116 |
'X-Bz-File-Name' => basename($file_path), |
| 117 |
'Content-Type' => 'b2/x-auto', |
| 118 |
'X-Bz-Content-Sha1' => $sha1_of_file_data |
| 119 |
), |
| 120 |
'timeout' => 900, |
| 121 |
)); |
| 122 |
|
| 123 |
if (is_wp_error($response)) { |
| 124 |
return array('success' => false, 'message' => esc_html__('Upload request failed: ', 'wpdbbkp') . $response->get_error_message()); |
| 125 |
} |
| 126 |
|
| 127 |
$response_code = wp_remote_retrieve_response_code($response); |
| 128 |
if ($response_code != 200) { |
| 129 |
$response_body = wp_remote_retrieve_body($response); |
| 130 |
return array('success' => false, 'message' => esc_html__('Failed to upload ' , 'wpdbbkp'). $file_name . ' to Backblaze. Response: ' . $response_body); |
| 131 |
} |
| 132 |
|
| 133 |
return array('success' => true, 'message' => 'File ' . $file_name . esc_html__(' uploaded successfully to Backblaze.', 'wpdbbkp')); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
/** |
| 138 |
* Run after complete backup. |
| 139 |
* |
| 140 |
* @param array $args - backup details. |
| 141 |
*/ |
| 142 |
|
| 143 |
public static function wp_db_backup_completed( &$args ) { |
| 144 |
$destination_s3 = get_option( 'wp_db_backup_destination_bb' ); |
| 145 |
if ( isset( $destination_s3 ) && 1 == $destination_s3 && get_option( 'wpdb_dest_bb_s3_bucket_host' ) && get_option( 'wpdb_dest_bb_s3_bucket' ) && get_option( 'wpdb_dest_bb_s3_bucket_key' ) && get_option( 'wpdb_dest_bb_s3_bucket_secret' ) ) { |
| 146 |
|
| 147 |
try { |
| 148 |
|
| 149 |
$ret = WPDatabaseBackupBB::upload_backup_to_backblaze($args[1], $args[1]); |
| 150 |
$args[2] = $args[2] .$ret['message']; |
| 151 |
if ($ret['success']) { |
| 152 |
$args[4] = $args[4] .= 'Backblaze, '; |
| 153 |
} |
| 154 |
} catch ( Exception $e ) { |
| 155 |
$args[2] = $args[2] . "<br>".esc_html__("Failed to upload Database Backup on s3 bucket", 'wpdbbkp'); |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
} |
| 161 |
|