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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
|
<?php // Dustin Bolton 2014. class backupbuddy_deploy { public $_state = array(); // Holds current state data. Retrieve with getState() and pass onto next run in the constructor. private $_errors = array(); // Hold error strings to retrieve with getErrors(). /* __construct() * * ROLLBACK, RESTORE * * @param string $type Restore type: rollback (roll back from inside WordPress), restore (importbuddy) * @param array $existinData State data from a previous instantiation. Previously returned from getState(). * */ public function __construct( $api_key, $existingState = '' ) { pb_backupbuddy::status( 'details', 'Constructing deploy class.' ); register_shutdown_function( array( &$this, 'shutdown_function' ) ); require_once( pb_backupbuddy::plugin_path() . '/classes/remote_api.php' ); if ( false === ( $decoded_key = backupbuddy_remote_api::key_to_array( $api_key ) ) ) { die( 'Error #848349478943747. Unable to interpret API key. Corrupted?' ); } if ( is_array( $existingState ) ) { // User passed along an existing state to resume. $this->_state = $existingState; } else { // Create new blank process & state. $this->_state = array( 'apiKey' => $api_key, 'destination' => $decoded_key, 'startTime' => time(), 'backupProfile' => '', 'sendTheme' => false, // Send active theme to sync 'sendPlugins' => false, // Send plugins of differing versions to sync 'sendMedia' => false, 'pushThemeFiles' => array(), 'pushPluginFiles' => array(), 'pushMediaFiles' => array(), 'pullThemeFiles' => array(), 'pullPluginFiles' => array(), 'pullMediaFiles' => array(), ); } pb_backupbuddy::status( 'details', 'Deploy class constructed.' ); } // End __construct(). /* start() * * @return bool true on success, else false. */ public function start( $sourceInfo ) { $this->_before( __FUNCTION__ ); $pingTimePre = microtime(true); if ( false === ( $this->_state['remoteInfo'] = backupbuddy_remote_api::remoteCall( $this->_state['destination'], 'getPreDeployInfo', array(), $timeout = 10 ) ) ) { pb_backupbuddy::alert( implode( ', ', backupbuddy_remote_api::getErrors() ) ); return false; } $this->_state['remoteInfo'] = $this->_state['remoteInfo']['data']; $pingTimePost = microtime(true); $this->_state['remoteInfo']['pingTime'] = $pingTimePost - $pingTimePre; // Calculate plugins that do not match. $this->_state['pushPluginFiles'] = $this->calculatePluginDiff( $sourceInfo['activePlugins'], $this->_state['remoteInfo']['activePlugins'] ); $this->_state['pullPluginFiles'] = $this->calculatePluginDiff( $this->_state['remoteInfo']['activePlugins'], $sourceInfo['activePlugins'] ); if ( $sourceInfo['activeTheme'] == $this->_state['remoteInfo']['activeTheme'] ) { // Calculate themes that do not match. $this->_state['pushThemeFiles'] = $this->calculateFileDiff( $sourceInfo['themeSignatures'], $this->_state['remoteInfo']['themeSignatures'] ); $this->_state['pullThemeFiles'] = $this->calculateFileDiff( $this->_state['remoteInfo']['themeSignatures'], $sourceInfo['themeSignatures'] ); } else { $this->_state['sendTheme'] = false; pb_backupbuddy::status( 'details', 'Different themes. No theme data will be sent.' ); } // Calculate media files that do not match. $this->_state['pushMediaFiles'] = $this->calculateMediaDiff( $sourceInfo['mediaSignatures'], $this->_state['remoteInfo']['mediaSignatures'] ); $this->_state['pullMediaFiles'] = $this->calculateMediaDiff( $this->_state['remoteInfo']['mediaSignatures'], $sourceInfo['mediaSignatures'] ); unset( $this->_state['remoteInfo']['mediaSignatures'] ); unset( $this->_state['remoteInfo']['themeSignatures'] ); return true; } // End start(). public function calculateFileDiff( $sourceFileSignatures, $destinationFileSignatures ) { $updateFiles = array(); // Files to send. // Loop through local files to see if they differ from anything on remote. foreach( $sourceFileSignatures as $file => $signature ) { if ( ! isset( $destinationFileSignatures[ $file ] ) ) { // File does not exist on destination. $updateFiles[] = $file; } else { // File exists on remote. See if content is the same. if ( $signature['sha1'] != $destinationFileSignatures[ $file ]['sha1'] ) { // Hash mismatch. Needs updating. $updateFiles[] = $file; } elseif ( '' == $signature['sha1'] ) { // sha1 not calculated. size may be too large. compare size to see if changed. if ( $signature['size'] != $destinationFileSignatures[ $file ]['size'] ) { // size mismatch $updateFiles[] = $file; } } } } return $updateFiles; } public function calculateMediaDiff( $sourceFileSignatures, $destinationFileSignatures ) { $updateFiles = array(); // Files to send. // Loop through local files to see if they differ from anything on remote. foreach( (array)$sourceFileSignatures as $file => $signature ) { if ( ! isset( $destinationFileSignatures[ $file ] ) ) { // File does not exist on destination. $updateFiles[] = $file; } else { // File exists on remote. See if content is the same. if ( $signature['modified'] != $destinationFileSignatures[ $file ]['modified'] ) { // mismatch of modified time stored in database. Needs updating. $updateFiles[] = $file; } } } return $updateFiles; } /* public function calculateThemeDiff( $sourceThemeSignatures, $destinationThemeSignatures ) { $updateThemeFiles = array(); // Theme files to send. // Loop through local theme files to see if they differ from anything on remote. foreach( $sourceThemeSignatures as $file => $signature ) { if ( ! isset( $destinationThemeSignatures[ $file ] ) ) { // File does not exist on destination. $updateThemeFiles[] = $file; } else { // File exists on remote. See if content is the same. if ( $signature['sha1'] != $destinationThemeSignatures[ $file ]['sha1'] ) { // Hash mismatch. Needs updating. $updateThemeFiles[] = $file; } } } return $updateThemeFiles; } */ public function calculatePluginDiff( $sourcePlugins, $destinationPlugins ) { $updatePlugins = array(); $pluginPath = wp_normalize_path( WP_PLUGIN_DIR ) . '/'; foreach( $sourcePlugins as $sourceSlug => $sourcePlugin ) { $update = false; if ( ! isset( $destinationPlugins[ $sourceSlug ] ) ) { // Plugin does not exist on destination. $update = true; } else { // File exists on remote. See if content is the same. if ( $sourcePlugins[ $sourceSlug ]['version'] != $destinationPlugins[ $sourceSlug ]['version'] ) { // Version mismatch. Needs updating. $update = true; } } if ( true === $update ) { $pluginFiles = pb_backupbuddy::$filesystem->deepglob( $pluginPath . dirname( $sourceSlug ) ); foreach( $pluginFiles as &$pluginFile ) { // Strip out leading path. if ( ! is_dir( $pluginFile ) ) { // Don't send just directory. Only files within. Note: This will not send a blank directory but that should not be an issue. $pluginFile = str_replace( $pluginPath, '', $pluginFile ); } else { unset( $pluginFile ); } } $updatePlugins = array_merge( $updatePlugins, $pluginFiles ); } } return $updatePlugins; } public function hashFileMap( $root ) { $generate_sha1 = true; echo 'mem:' . memory_get_usage(true) . '<br>'; $files = (array) pb_backupbuddy::$filesystem->deepglob( $root ); echo 'mem:' . memory_get_usage(true) . '<br>'; $root_len = strlen( $root ); $new_files = array(); foreach( $files as $file_id => &$file ) { $stat = stat( $file ); if ( FALSE === $stat ) { pb_backupbuddy::status( 'error', 'Unable to read file `' . $file . '` stat.' ); } $new_file = substr( $file, $root_len ); $sha1 = ''; if ( ( true === $generate_sha1 ) && ( $stat['size'] < 1073741824 ) ) { // < 100mb $sha1 = sha1_file( $file ); } $new_files[$new_file] = array( 'scanned' => time(), 'size' => $stat['size'], 'modified' => $stat['mtime'], 'sha1' => $sha1, // TODO: don't render sha1 here? do it in a subsequent step(s) with cron to allow for more time? update fileoptions file every x number of tiles and a count attempts without proceeding to assume failure? max_overall attempts? ); unset( $files[$file_id] ); // Better to free memory or leave out for performance? } unset( $files ); echo 'mem:' . memory_get_usage(true) . '<br>'; echo 'filecount: ' . count( $new_files ) . '<br>'; print_r( $new_files ); } // end crcMap(). /* extractDatabase() * * ROLLBACK, RESTORE * Extracts database file(s) into temp dir. * * @param bool true on success, else false. */ public function extractDatabase() { $this->_before( __FUNCTION__ ); $this->_priorRollbackCleanup(); pb_backupbuddy::status( 'details', 'Loading zipbuddy.' ); require_once( pb_backupbuddy::plugin_path() . '/lib/zipbuddy/zipbuddy.php' ); $zipbuddy = new pluginbuddy_zipbuddy( backupbuddy_core::getBackupDirectory() ); pb_backupbuddy::status( 'details', 'Zipbuddy loaded.' ); // Find SQL file location in archive. pb_backupbuddy::status( 'details', 'Calculating possible SQL file locations.' ); $possibleSQLLocations = array(); $possibleSQLLocations[] = trim( rtrim( str_replace( 'backupbuddy_dat.php', '', $this->_state['datLocation'] ), '\\/' ) . '/db_1.sql', '\\/' ); // SQL file most likely is in the same spot the dat file was. $possibleSQLLocations[] = 'db_1.sql'; // DB backup. $possibleSQLLocations[] = 'wp-content/uploads/backupbuddy_temp/' . $this->_state['serial'] . '/db_1.sql'; // Full backup. pb_backupbuddy::status( 'details', 'Possible SQL file locations: `' . implode( ';', $possibleSQLLocations ) . '`.' ); $possibleSQLLocations = array_unique( $possibleSQLLocations ); foreach( $possibleSQLLocations as $possibleSQLLocation ) { if ( true === $zipbuddy->file_exists( $this->_state['archive'], $possibleSQLLocation, $leave_open = true ) ) { $detectedSQLLocation = $possibleSQLLocation; break; } } // end foreach. pb_backupbuddy::status( 'details', 'Confirmed SQL file location: `' . $detectedSQLLocation . '`.' ); // Get SQL file. $files = array( $detectedSQLLocation => 'db_1.sql' ); pb_backupbuddy::$filesystem->unlink_recursive( $this->_state['tempPath'] ); // Remove if already exists. mkdir( $this->_state['tempPath'] ); // Make empty directory. require( pb_backupbuddy::plugin_path() . '/classes/_restoreFiles.php' ); // Extract SQL file. pb_backupbuddy::status( 'details', 'Extracting SQL file(s).' ); if ( false === backupbuddy_restore_files::restore( $this->_state['archive'], $files, $this->_state['tempPath'], $zipbuddy ) ) { return $this->_error( 'Error #85384: Unable to restore one or more database files.' ); } pb_backupbuddy::status( 'details', 'Finished database extraction function.' ); return true; } // End extractDatabase(). /* _error() * * Logs error messages for retrieval with getErrors(). * * @param string $message Error message to log. * @return null */ private function _error( $message ) { $this->_errors[] = $message; pb_backupbuddy::status( 'error', $message ); return false; } /* getErrors() * * Get any errors which may have occurred. * * @return array Returns an array of string error messages. */ public function getErrors() { return $this->_errors; } // End getErrors(); /* getState() * * Get state array data for passing to the constructor for subsequent calls. * * @return array Returns an array of state data. */ public function getState() { pb_backupbuddy::status( 'details', 'Getting deploy state.' ); return $this->_state; } // End getState(). /* setState() * * Replace current state array with provided one. * */ public function setState( $stateData ) { $this->_state = $stateData; } // End setState(). /* _before() * * Runs before every function to keep track of ran functions in the state data for debugging. * * @return null */ private function _before( $functionName ) { $this->_state['stepHistory'][] = array( 'function' => $functionName, 'start' => time() ); pb_backupbuddy::status( 'details', 'Starting function `' . $functionName . '`.' ); return; } // End _before(). /* shutdown_function() * * Used for catching fatal PHP errors during backup to write to log for debugging. * * @return null */ public function shutdown_function() { // Get error message. // Error types: http://php.net/manual/en/errorfunc.constants.php $e = error_get_last(); if ( $e === NULL ) { // No error of any kind. return; } else { // Some type of error. if ( !is_array( $e ) || ( $e['type'] != E_ERROR ) && ( $e['type'] != E_USER_ERROR ) ) { // Return if not a fatal error. return; } } $e_string = ''; foreach( (array)$e as $e_line_title => $e_line ) { $e_string .= $e_line_title . ' => ' . $e_line . "\n"; } pb_backupbuddy::status( 'error', 'FATAL PHP ERROR: ' . $e_string ); } // End shutdown_function. } // end class.
|