1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
<?php // @author Dustin Bolton 2013. // Incoming variables: $destination
?>
<script type="text/javascript"> jQuery(document).ready(function() { jQuery( '.pb_backupbuddy_hoveraction_copy' ).click( function() { var backup_file = jQuery(this).attr( 'rel' ); var backup_url = '<?php echo pb_backupbuddy::page_url(); ?>&custom=remoteclient&destination_id=<?php echo pb_backupbuddy::_GET( 'destination_id' ); ?>&remote_path=<?php echo htmlentities( pb_backupbuddy::_GET( 'remote_path' ) ); ?>&cpy_file=' + backup_file; window.location.href = backup_url; return false; } ); jQuery( '.pb_backupbuddy_hoveraction_download_link' ).click( function() { var backup_file = jQuery(this).attr( 'rel' ); var backup_url = '<?php echo pb_backupbuddy::page_url(); ?>&custom=remoteclient&destination_id=<?php echo pb_backupbuddy::_GET( 'destination_id' ); ?>&remote_path=<?php echo htmlentities( pb_backupbuddy::_GET( 'remote_path' ) ); ?>&downloadlink_file=' + backup_file; window.location.href = backup_url; return false; } ); }); </script>
<?php
// Load required files. require_once( pb_backupbuddy::plugin_path() . '/destinations/s3/init.php' ); require_once( dirname( dirname( __FILE__ ) ) . '/_s3lib/aws-sdk/sdk.class.php' );
// Settings. if ( isset( pb_backupbuddy::$options['remote_destinations'][pb_backupbuddy::_GET('destination_id')] ) ) { $settings = &pb_backupbuddy::$options['remote_destinations'][pb_backupbuddy::_GET('destination_id')]; } $settings = array_merge( pb_backupbuddy_destination_s3::$default_settings, $settings ); $settings['bucket'] = strtolower( $settings['bucket'] ); // Buckets must be lowercase.
$remote_path = pb_backupbuddy_destination_s3::get_remote_path( $settings['directory'] );
// Welcome text. pb_backupbuddy::$ui->title( __( 'S3 Destination', 'it-l10n-backupbuddy' ) . ' "' . $destination['title'] . '"' ); $manage_data = pb_backupbuddy_destination_s3::get_credentials( $settings );
// Connect to S3. $s3 = new AmazonS3( $manage_data ); // the key, secret, token if ( $settings['ssl'] == '0' ) { @$s3->disable_ssl(true); }
// The bucket must be in existence and we must get it's region to be able to proceed $region = ''; pb_backupbuddy::status( 'details', 'Getting region for bucket: `' . $settings['bucket'] . "`." ); $response = pb_backupbuddy_destination_s3::get_bucket_region( $s3, $settings['bucket'] ); if( !$response->isOK() ) {
$this_error = 'Bucket region could not be determined for management operation. Message details: `' . (string)$response->body->Message . '`.'; pb_backupbuddy::status( 'error' , $this_error ); } else {
pb_backupbuddy::status( 'details', 'Bucket exists in region: ' . (($response->body ==="") ? 'us-east-1' : $response->body ) ); $region = $response->body; // Must leave as is for actual operational usage }
// Set region context for later operations - will be s3.amazonaws.com or s3-<region>.amazonaws.com $s3->set_region( 's3' . ( ($region == "" ) ? "" : '-' . $region ) . '.amazonaws.com' );
// Handle deletion. if ( pb_backupbuddy::_POST( 'bulk_action' ) == 'delete_backup' ) { pb_backupbuddy::verify_nonce(); $deleted_files = array(); foreach( (array)pb_backupbuddy::_POST( 'items' ) as $item ) { $response = $s3->delete_object( $manage_data['bucket'], $remote_path . $item ); if ( $response->isOK() ) { $deleted_files[] = $item; } else { pb_backupbuddy::alert( 'Error: Unable to delete `' . $item . '`. Verify permissions.' ); } } if ( count( $deleted_files ) > 0 ) { pb_backupbuddy::alert( 'Deleted ' . implode( ', ', $deleted_files ) . '.' ); } echo '<br>'; }
// Handle copying files to local if ( pb_backupbuddy::_GET( 'cpy_file' ) != '' ) { pb_backupbuddy::alert( 'The remote file is now being copied to your local backups. If the backup gets marked as bad during copying, please wait a bit then click the `Refresh` icon to rescan after the transfer is complete.' ); echo '<br>'; pb_backupbuddy::status( 'details', 'Scheduling Cron for creating S3 copy.' ); backupbuddy_core::schedule_single_event( time(), pb_backupbuddy::cron_tag( 'process_remote_copy' ), array( 's3', pb_backupbuddy::_GET( 'cpy_file' ), $settings ) ); spawn_cron( time() + 150 ); // Adds > 60 seconds to get around once per minute cron running limit. update_option( '_transient_doing_cron', 0 ); // Prevent cron-blocking for next item. }
// Handle download link if ( pb_backupbuddy::_GET( 'downloadlink_file' ) != '' ) { $link = $s3->get_object( $manage_data['bucket'], $remote_path . pb_backupbuddy::_GET( 'downloadlink_file' ), array('preauth'=>time()+3600)); pb_backupbuddy::alert( 'You may download this backup (' . pb_backupbuddy::_GET( 'downloadlink_file' ) . ') with <a href="' . $link . '">this link</a>. The link is valid for one hour.' ); echo '<br>'; }
$prefix = backupbuddy_core::backup_prefix();
// Get file listing. $response = $s3->list_objects( $manage_data['bucket'], array( 'prefix' => $remote_path ) ); // list all the files in the subscriber account
// Get list of files. $backup_list_temp = array(); foreach( $response->body->Contents as $object ) { $file = str_ireplace( $remote_path, '', $object->Key ); if ( FALSE !== stristr( $file, '/' ) ) { // Do NOT display any files within a deeper subdirectory. continue; } if ( ( ! preg_match( pb_backupbuddy_destination_s3::BACKUP_FILENAME_PATTERN, $file ) ) && ( 'importbuddy.php' !== $file ) ) { // Do not display any files that do not appear to be a BackupBuddy backup file (except importbuddy.php). continue; } /* Unsure whether to include this here or not? if ( FALSE === ( strpos( $file, 'backup-' . $prefix . '-' ) ) ) { // Not a backup for THIS site. Skip. continue; } */ $last_modified = strtotime( $object->LastModified ); $size = (double) $object->Size; if ( stristr( $file, '-db-' ) !== FALSE ) { $backup_type = 'Database'; } elseif ( stristr( $file, '-full-' ) !== FALSE ) { $backup_type = 'Full'; } elseif( $file == 'importbuddy.php' ) { $backup_type = 'ImportBuddy Tool'; } else { $backup_type = 'Unknown'; } // Generate array of table rows. while( isset( $backup_list_temp[$last_modified] ) ) { // Avoid collisions. $last_modified += 0.1; } $backup_list_temp[$last_modified] = array( $file, pb_backupbuddy::$format->date( pb_backupbuddy::$format->localize_time( $last_modified ) ) . '<br /><span class="description">(' . pb_backupbuddy::$format->time_ago( $last_modified ) . ' ago)</span>', pb_backupbuddy::$format->file_size( $size ), $backup_type );
}
krsort( $backup_list_temp ); $backup_list = array(); foreach( $backup_list_temp as $backup_item ) { $backup_list[ $backup_item[0] ] = $backup_item; } unset( $backup_list_temp );
$urlPrefix = pb_backupbuddy::ajax_url( 'remoteClient' ) . '&destination_id=' . htmlentities( pb_backupbuddy::_GET( 'destination_id' ) );
// Render table listing files. if ( count( $backup_list ) == 0 ) { echo '<b>'; _e( 'You have not completed sending any backups to this S3 destination for this site yet.', 'it-l10n-backupbuddy' ); echo '</b>'; } else { pb_backupbuddy::$ui->list_table( $backup_list, array( 'action' => pb_backupbuddy::ajax_url( 'remoteClient' ) . '&function=remoteClient&destination_id=' . htmlentities( pb_backupbuddy::_GET( 'destination_id' ) ) . '&remote_path=' . htmlentities( pb_backupbuddy::_GET( 'remote_path' ) ), 'columns' => array( 'Backup File', 'Uploaded <img src="' . pb_backupbuddy::plugin_url() . '/images/sort_down.png" style="vertical-align: 0px;" title="Sorted most recent first">', 'File Size', 'Type' ), 'hover_actions' => array( $urlPrefix . '&cpy_file=' => 'Copy to Local', $urlPrefix . '&downloadlink_file=' => 'Get download link' ), 'hover_action_column_key' => '0', 'bulk_actions' => array( 'delete_backup' => 'Delete' ), 'css' => 'width: 100%;', ) ); }
// Display troubleshooting subscriber key. echo '<br style="clear: both;">';
return;
|