PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/betaimages/chakalos
PHP | 151 lines | 53 code | 33 blank | 65 comment | 11 complexity | 3733095b6f8ae8c686631f5fd9f57f33 MD5 | raw file
Possible License(s): BSD-3-Clause
  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. // get file listing of site: glob and store in an array
  25. // open previously generated master list (master file listing since last queue generation).
  26. // 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[];
  27. // 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.
  28. // sort list smallest to largest
  29. // store in $queue_files[] in format:
  30. /*
  31. array(
  32. 'size' => 434344,
  33. 'attempts' => 0,
  34. );
  35. */
  36. // open current queue file (if exists)
  37. // combine new files into queue
  38. // serialize $queue_files
  39. // base64 encode
  40. // write to queue file
  41. pb_backupbuddy::status( 'details', '12 new or modified files added into Stash queue.' );
  42. // Schedule process_queue() to run in 30 seconds from now _IF_ not already scheduled to run.
  43. } // End generate_queue().
  44. /* enqueue_file()
  45. *
  46. * Manually add a file into the transfer queue to be transferred soon(ish).
  47. *
  48. * @param string $file Full path to the file to transfer.
  49. * @param boolean True if enqueued, else false (file does not exist).
  50. */
  51. public function enqueue_file( $file ) {
  52. if ( file_exists( $file ) ) {
  53. // open current queue file (if exists)
  54. // combine new file into queue
  55. // serialize
  56. // base64
  57. // write
  58. } else {
  59. return false;
  60. }
  61. } // End enqueue_file().
  62. /* process_queue()
  63. *
  64. * description
  65. *
  66. */
  67. public function process_queue() {
  68. // open queue file.
  69. $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.
  70. $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).
  71. $send_now_files = array(); // Files that will be queued up to be sent this PHP instance.
  72. $send_now_size = 0; // Running sum of the size of all files queued up to be send this PHP instance.
  73. $need_save = false; // Whether or not we have updated something in the queue that needs saving.
  74. $unsent_files = false;
  75. foreach( $files as &$file ) { // Loop through files in queue that need sent to Live.
  76. if ( ( $send_now_size + $file['size'] ) <= $max_session_size ) { // There is room to add this file.
  77. pb_backupbuddy::status( 'details', 'Added file `file.png` into queue.', 'live' );
  78. if ( $file['attempts'] >= 3 ) {
  79. // send error email notifying that its not going to make it. give suggestions. chunking?
  80. pb_backupbuddy::status( 'error', 'Large 94 MB file `file.png` has timed out X times and has is on hold pending user intervention.', 'live' );
  81. } else {
  82. $send_now_files .= $file;
  83. $file['attempts']++;
  84. $need_save = true;
  85. }
  86. } else { // There is not room for this file.
  87. 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.
  88. 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' );
  89. $send_now_files .= $file;
  90. $file['attempts']++;
  91. $need_save = true;
  92. $unsent_files = true;
  93. break; // We have maxed out the size with a single file so no need to keep going.
  94. }
  95. $unsent_files = true;
  96. break; // No more room for any other files if we made it here so stop looping.
  97. }
  98. } // end foreach.
  99. if ( $need_save === true ) {
  100. pb_backupbuddy::status( 'details', 'Saving queue file.', 'live' );
  101. // Code to save the updated data structure to file.
  102. // After saving add this file itself to the send queue so it (the queue file) gets backed up soon?
  103. }
  104. // Call Stash to send these files.
  105. require_once( pb_backupbuddy::plugin_path() . '/destinations/bootstrap.php' );
  106. $send_result = pb_backupbuddy_destinations::send( $destination_settings, $send_now_files );
  107. pb_backupbuddy::status( 'message', '4 MB file `file.png` Stashed in 12 seconds.', 'live' );
  108. 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' );
  109. // remove all succesful transfers from the queue file and re-save it. be quick as we may be running out of time.
  110. //
  111. $this->kick_db(); // Kick the database to make sure it didn't go away, preventing options saving.
  112. if ( $unsent_files === true ) {
  113. // schedule next queue_process() call.
  114. }
  115. // 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])
  116. } // End process_queue().
  117. } // End class.