PageRenderTime 55ms CodeModel.GetById 5ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-content/plugins/backupbuddy/classes/live.php

https://bitbucket.org/summitds/bloomsburgpa.org
PHP | 185 lines | 78 code | 41 blank | 66 comment | 12 complexity | 321f6c2eb8bbbc85910ca85ba758f087 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, BSD-3-Clause, GPL-3.0, LGPL-2.1
  1. <?php
  2. /* Class pb_backupbuddy_live
  3. *
  4. * Live backup of files to Stash servers.
  5. *
  6. * @author Dustin Bolton < http://dustinbolton.com >
  7. * @date Nov 26, 2012
  8. *
  9. * Usage:
  10. * Generate DB dump before generating queue to easily backup db.
  11. * Call generate_queue() periodicially (ie 2x daily?)
  12. * Hook into media library to auto-add uploaded files into queue.
  13. */
  14. class pb_backupbuddy_live {
  15. /* generate_queue()
  16. *
  17. * Determine what files have changed since this function was last run.
  18. * Generate a list of said files and append to any existing queue list file.
  19. * process_queue() will be scheduled to run shortly after function completes.
  20. *
  21. * @return null
  22. */
  23. public function generate_queue() {
  24. $root = WP_CONTENT_DIR . '/uploads/';
  25. echo 'mem:' . memory_get_usage(true) . '<br>';
  26. $files = (array) pb_backupbuddy::$filesystem->deepglob( $root );
  27. echo 'mem:' . memory_get_usage(true) . '<br>';
  28. $root_len = strlen( $root );
  29. $new_files = array();
  30. foreach( $files as $file_id => &$file ) {
  31. $stat = stat( $file );
  32. if ( FALSE === $stat ) {
  33. pb_backupbuddy::status( 'error', 'Unable to read file `' . $file . '` stat.' );
  34. }
  35. $new_file = substr( $file, $root_len );
  36. $new_files[$new_file] = array(
  37. //'file' => $new_file,
  38. 'size' => $stat['size'],
  39. 'modified' => $stat['mtime'],
  40. );
  41. unset( $files[$file_id] ); // Better to free memory or leave out for performance?
  42. }
  43. unset( $files );
  44. echo 'mem:' . memory_get_usage(true) . '<br>';
  45. function pb_queuearray_size_compare($a, $b) {
  46. return ($a['size'] > $b['size']);
  47. }
  48. uasort( $new_files, 'pb_queuearray_size_compare' );
  49. print_r( $new_files );
  50. // get file listing of site: glob and store in an array
  51. // open previously generated master list (master file listing since last queue generation).
  52. // loop through and compare file specs to specs in master list. ( anything changed AND not yet in queue AND not maxed out send attempts ) gets added into $queue_files[];
  53. // add master file to end of list so it will be backed up as soon files are finished sending. to keep it up to date.
  54. // sort list smallest to largest
  55. // store in $queue_files[] in format:
  56. /*
  57. array(
  58. 'size' => 434344,
  59. 'attempts' => 0,
  60. );
  61. */
  62. // open current queue file (if exists)
  63. // combine new files into queue
  64. // serialize $queue_files
  65. // base64 encode
  66. // write to queue file
  67. pb_backupbuddy::status( 'details', '12 new or modified files added into Stash queue.' );
  68. // Schedule process_queue() to run in 30 seconds from now _IF_ not already scheduled to run.
  69. } // End generate_queue().
  70. /* enqueue_file()
  71. *
  72. * Manually add a file into the transfer queue to be transferred soon(ish).
  73. *
  74. * @param string $file Full path to the file to transfer.
  75. * @param boolean True if enqueued, else false (file does not exist).
  76. */
  77. public function enqueue_file( $file ) {
  78. if ( file_exists( $file ) ) {
  79. // open current queue file (if exists)
  80. // combine new file into queue
  81. // serialize
  82. // base64
  83. // write
  84. } else {
  85. return false;
  86. }
  87. } // End enqueue_file().
  88. /* process_queue()
  89. *
  90. * description
  91. *
  92. */
  93. public function process_queue() {
  94. // open queue file.
  95. $max_session_size = '50'; // Size (MB) that is the max size sum of all files sent per instance. TODO: On timeout failure detect and scale back some to help with timeouts.
  96. $max_session_time = '30'; // Currently only used to determine if we should auto-reduce the max session size if we are getting close to going over our time limit (help automatically avoid timeouts).
  97. $send_now_files = array(); // Files that will be queued up to be sent this PHP instance.
  98. $send_now_size = 0; // Running sum of the size of all files queued up to be send this PHP instance.
  99. $need_save = false; // Whether or not we have updated something in the queue that needs saving.
  100. $unsent_files = false;
  101. foreach( $files as &$file ) { // Loop through files in queue that need sent to Live.
  102. if ( ( $send_now_size + $file['size'] ) <= $max_session_size ) { // There is room to add this file.
  103. pb_backupbuddy::status( 'details', 'Added file `file.png` into queue.', 'live' );
  104. if ( $file['attempts'] >= 3 ) {
  105. // send error email notifying that its not going to make it. give suggestions. chunking?
  106. pb_backupbuddy::status( 'error', 'Large 94 MB file `file.png` has timed out X times and has is on hold pending user intervention.', 'live' );
  107. } else {
  108. $send_now_files .= $file;
  109. $file['attempts']++;
  110. $need_save = true;
  111. }
  112. } else { // There is not room for this file.
  113. if ( ( count( $send_now_files ) == 0 ) && ( $file['size'] > $max_session_size ) ) { // If no files are queued in this send now list yet then we will try to send just this one big file on its own.
  114. pb_backupbuddy::status( 'details', 'Large 94 MB file `file.png` exceeds max session size so it will be sent by itself to improve transfer success.', 'live' );
  115. $send_now_files .= $file;
  116. $file['attempts']++;
  117. $need_save = true;
  118. $unsent_files = true;
  119. break; // We have maxed out the size with a single file so no need to keep going.
  120. }
  121. $unsent_files = true;
  122. break; // No more room for any other files if we made it here so stop looping.
  123. }
  124. } // end foreach.
  125. if ( $need_save === true ) {
  126. pb_backupbuddy::status( 'details', 'Saving queue file.', 'live' );
  127. // Code to save the updated data structure to file.
  128. // After saving add this file itself to the send queue so it (the queue file) gets backed up soon?
  129. }
  130. // Call Stash to send these files.
  131. require_once( pb_backupbuddy::plugin_path() . '/destinations/bootstrap.php' );
  132. $send_result = pb_backupbuddy_destinations::send( $destination_settings, $send_now_files );
  133. pb_backupbuddy::status( 'message', '4 MB file `file.png` Stashed in 12 seconds.', 'live' );
  134. pb_backupbuddy::status( 'message', '4 MB file `file.png` did not complete after 60 seconds. Stashing it will be re-attempted in 30 seconds.', 'live' );
  135. // remove all succesful transfers from the queue file and re-save it. be quick as we may be running out of time.
  136. //
  137. $this->kick_db(); // Kick the database to make sure it didn't go away, preventing options saving.
  138. if ( $unsent_files === true ) {
  139. // schedule next queue_process() call.
  140. }
  141. // make note in data structure the last time the queue was processed & status (sent X mb in Y seconds. all files succeeded[4/5 files succeded])
  142. } // End process_queue().
  143. } // End class.