PageRenderTime 67ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 1ms

/wp-content/plugins/updraftplus/updraftplus.php

https://bitbucket.org/nathancorbier/wastark.com
PHP | 2438 lines | 1898 code | 265 blank | 275 comment | 496 complexity | 472068cefb4b0eec666fc93e24832b40 MD5 | raw file
Possible License(s): BSD-3-Clause

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /*
  3. Plugin Name: UpdraftPlus - Backup/Restore
  4. Plugin URI: http://updraftplus.com
  5. Description: Backup and restore: your content and database can be automatically backed up to Amazon S3, Dropbox, Google Drive, FTP or email, on separate schedules.
  6. Author: David Anderson
  7. Version: 1.4.14
  8. Donate link: http://david.dw-perspective.org.uk/donate
  9. License: GPLv3 or later
  10. Author URI: http://wordshell.net
  11. */
  12. /*
  13. TODO
  14. //Put in old-WP-version warning, and point them to where they can get help
  15. //Add SFTP, Box.Net, SugarSync and Microsoft Skydrive support??
  16. //The restorer has a hard-coded wp-content - fix
  17. //Change DB encryption to not require whole gzip in memory (twice)
  18. //improve error reporting / pretty up return messages in admin area. One thing: have a "backup is now finished" flag. Otherwise with the resuming things get ambiguous/confusing. See http://wordpress.org/support/topic/backup-status - user was not aware that backup completely failed. Maybe a "backup status" field for each nonce that gets updated? (Even via AJAX?)
  19. //?? On 'backup now', open up a Lightbox, count down 5 seconds, then start examining the log file (if it can be found)
  20. //Should make clear in dashboard what is a non-fatal error (i.e. can be retried) - leads to unnecessary bug reports
  21. // Move the inclusion, cloud and retention data into the backup job (i.e. don't read current config, make it an attribute of each job). In fact, everything should be. So audit all code for where get_option is called inside a backup run: it shouldn't happen.
  22. // Should we resume if the only errors were upon deletion (i.e. the backup itself was fine?) Presently we do, but it displays errors for the user to confuse them. Perhaps better to make pruning a separate scheuled task??
  23. // Warn the user if their zip-file creation is slooowww...
  24. // Create a "Want Support?" button/console, that leads them through what is needed, and performs some basic tests...
  25. // Resuming partial FTP uploads
  26. // Provide backup/restoration for UpdraftPlus's settings, to allow 'bootstrap' on a fresh WP install - some kind of single-use code which a remote UpdraftPlus can use to authenticate
  27. // Multiple jobs
  28. // Change FTP to use SSL by default
  29. // Chunked downloading of material for resumption + do resume it
  30. // Disk free-space display
  31. // Multisite add-on should allow restoring of each blog individually
  32. // When looking for files to delete, is the current encryption setting used? Should not be.
  33. // Create single zip, containing even WordPress itself
  34. // When a new backup starts, AJAX-update the 'Last backup' display in the admin page.
  35. // Remove the recurrence of admin notices when settings are saved due to _wp_referer
  36. // Auto-detect what the real execution time is (max_execution_time is just one of the upper limits, there can be others, some insivible directly), and tweak our resumption time accordingly
  37. Encrypt filesystem, if memory allows (and have option for abort if not); split up into multiple zips when needed
  38. // Does not delete old custom directories upon a restore?
  39. // New sub-module to verify that the backups are there, independently of backup thread
  40. */
  41. /* Portions copyright 2010 Paul Kehrer
  42. Portions copyright 2011-13 David Anderson
  43. Other portions copyright as indicated authors in the relevant files
  44. Particular thanks to Sorin Iclanzan, author of the "Backup" plugin, from which much Google Drive code was taken under the GPLv3+
  45. This program is free software; you can redistribute it and/or modify
  46. it under the terms of the GNU General Public License as published by
  47. the Free Software Foundation; either version 3 of the License, or
  48. (at your option) any later version.
  49. This program is distributed in the hope that it will be useful,
  50. but WITHOUT ANY WARRANTY; without even the implied warranty of
  51. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  52. GNU General Public License for more details.
  53. You should have received a copy of the GNU General Public License
  54. along with this program; if not, write to the Free Software
  55. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  56. */
  57. // 15 minutes
  58. @set_time_limit(900);
  59. define('UPDRAFTPLUS_DIR', dirname(__FILE__));
  60. define('UPDRAFTPLUS_URL', plugins_url('', __FILE__));
  61. define('UPDRAFT_DEFAULT_OTHERS_EXCLUDE','upgrade,cache,updraft,index.php,backup,backups');
  62. // This is used in various places, based on our assumption of the maximum time any job should take. May need lengthening in future if we get reports which show enormous sets hitting the limit.
  63. // Also one section requires at least 1% progress each run, so on a 5-minute schedule, that equals just under 9 hours - then an extra allowance takes it just over
  64. define('UPDRAFT_TRANSTIME', 3600*9+5);
  65. // Load add-ons
  66. if (is_file(UPDRAFTPLUS_DIR.'/premium.php')) require_once(UPDRAFTPLUS_DIR.'/premium.php');
  67. if ($dir_handle = opendir(UPDRAFTPLUS_DIR.'/addons')) {
  68. while ($e = readdir($dir_handle)) {
  69. if (is_file(UPDRAFTPLUS_DIR.'/addons/'.$e) && preg_match('/\.php$/', $e)) {
  70. include_once(UPDRAFTPLUS_DIR.'/addons/'.$e);
  71. }
  72. }
  73. @closedir($dir_handle);
  74. }
  75. if (!isset($updraftplus)) $updraftplus = new UpdraftPlus();
  76. if (!$updraftplus->memory_check(192)) {
  77. # TODO: Better solution is to split the backup set into manageable chunks based on this limit
  78. @ini_set('memory_limit', '192M'); //up the memory limit for large backup files
  79. }
  80. if (!class_exists('UpdraftPlus_Options')) require_once(UPDRAFTPLUS_DIR.'/options.php');
  81. class UpdraftPlus {
  82. var $version;
  83. var $plugin_title = 'UpdraftPlus Backup/Restore';
  84. // Choices will be shown in the admin menu in the order used here
  85. var $backup_methods = array (
  86. "s3" => "Amazon S3",
  87. "dropbox" => "Dropbox",
  88. "googledrive" => "Google Drive",
  89. "ftp" => "FTP",
  90. "email" => "Email"
  91. );
  92. var $dbhandle;
  93. var $dbhandle_isgz;
  94. var $errors = array();
  95. var $nonce;
  96. var $logfile_name = "";
  97. var $logfile_handle = false;
  98. var $backup_time;
  99. var $opened_log_time;
  100. var $backup_dir;
  101. var $jobdata;
  102. // Used to schedule resumption attempts beyond the tenth, if needed
  103. var $current_resumption;
  104. var $newresumption_scheduled = false;
  105. var $zipfiles_added;
  106. var $zipfiles_existingfiles;
  107. var $zipfiles_dirbatched;
  108. var $zipfiles_batched;
  109. var $zip_preferpcl = false;
  110. function __construct() {
  111. // Initialisation actions - takes place on plugin load
  112. if ($fp = fopen( __FILE__, 'r')) {
  113. $file_data = fread( $fp, 1024 );
  114. if (preg_match("/Version: ([\d\.]+)(\r|\n)/", $file_data, $matches)) {
  115. $this->version = $matches[1];
  116. }
  117. fclose( $fp );
  118. }
  119. # Create admin page
  120. add_action('admin_init', array($this, 'admin_init'));
  121. add_action('updraft_backup', array($this,'backup_files'));
  122. add_action('updraft_backup_database', array($this,'backup_database'));
  123. # backup_all is used by the manual "Backup Now" button
  124. add_action('updraft_backup_all', array($this,'backup_all'));
  125. # this is our runs-after-backup event, whose purpose is to see if it succeeded or failed, and resume/mom-up etc.
  126. add_action('updraft_backup_resume', array($this,'backup_resume'), 10, 3);
  127. add_action('wp_ajax_updraft_download_backup', array($this, 'updraft_download_backup'));
  128. add_action('wp_ajax_updraft_ajax', array($this, 'updraft_ajax_handler'));
  129. # http://codex.wordpress.org/Plugin_API/Filter_Reference/cron_schedules
  130. add_filter('cron_schedules', array($this,'modify_cron_schedules'));
  131. add_filter('plugin_action_links', array($this, 'plugin_action_links'), 10, 2);
  132. add_action('init', array($this, 'handle_url_actions'));
  133. }
  134. // Handle actions passed on to method plugins; e.g. Google OAuth 2.0 - ?page=updraftplus&action=updraftmethod-googledrive-auth
  135. // Also handle action=downloadlog
  136. function handle_url_actions() {
  137. // First, basic security check: must be an admin page, with ability to manage options, with the right parameters
  138. if ( UpdraftPlus_Options::user_can_manage() && isset( $_GET['page'] ) && $_GET['page'] == 'updraftplus' && isset($_GET['action']) ) {
  139. if (preg_match("/^updraftmethod-([a-z]+)-([a-z]+)$/", $_GET['action'], $matches) && file_exists(UPDRAFTPLUS_DIR.'/methods/'.$matches[1].'.php')) {
  140. $method = $matches[1];
  141. require_once(UPDRAFTPLUS_DIR.'/methods/'.$method.'.php');
  142. $call_class = "UpdraftPlus_BackupModule_".$method;
  143. $call_method = "action_".$matches[2];
  144. if (method_exists($call_class, $call_method)) call_user_func(array($call_class,$call_method));
  145. } elseif ($_GET['action'] == 'downloadlog' && isset($_GET['updraftplus_backup_nonce']) && preg_match("/^[0-9a-f]{12}$/",$_GET['updraftplus_backup_nonce'])) {
  146. $updraft_dir = $this->backups_dir_location();
  147. $log_file = $updraft_dir.'/log.'.$_GET['updraftplus_backup_nonce'].'.txt';
  148. if (is_readable($log_file)) {
  149. header('Content-type: text/plain');
  150. readfile($log_file);
  151. exit;
  152. } else {
  153. add_action('admin_notices', array($this,'show_admin_warning_unreadablelog') );
  154. }
  155. }
  156. }
  157. }
  158. // Cleans up temporary files found in the updraft directory
  159. function clean_temporary_files() {
  160. $updraft_dir = $this->backups_dir_location();
  161. if ($handle = opendir($updraft_dir)) {
  162. $now_time=time();
  163. while (false !== ($entry = readdir($handle))) {
  164. if (preg_match('/\.tmp(\.gz)?$/', $entry) && is_file($updraft_dir.'/'.$entry) && $now_time-filemtime($updraft_dir.'/'.$entry)>86400) {
  165. $this->log("Deleting old temporary file: $entry");
  166. @unlink($updraft_dir.'/'.$entry);
  167. }
  168. }
  169. @closedir($handle);
  170. }
  171. }
  172. # Adds the settings link under the plugin on the plugin screen.
  173. function plugin_action_links($links, $file) {
  174. if ($file == plugin_basename(__FILE__)){
  175. $settings_link = '<a href="'.site_url().'/wp-admin/options-general.php?page=updraftplus">'.__("Settings", "UpdraftPlus").'</a>';
  176. array_unshift($links, $settings_link);
  177. $settings_link = '<a href="http://david.dw-perspective.org.uk/donate">'.__("Donate","UpdraftPlus").'</a>';
  178. array_unshift($links, $settings_link);
  179. $settings_link = '<a href="http://updraftplus.com">'.__("Add-Ons / Pro Support","UpdraftPlus").'</a>';
  180. array_unshift($links, $settings_link);
  181. }
  182. return $links;
  183. }
  184. function backup_time_nonce() {
  185. $this->backup_time = time();
  186. $nonce = substr(md5(time().rand()), 20);
  187. $this->nonce = $nonce;
  188. }
  189. function logfile_open($nonce) {
  190. //set log file name and open log file
  191. $updraft_dir = $this->backups_dir_location();
  192. $this->logfile_name = $updraft_dir. "/log.$nonce.txt";
  193. // Use append mode in case it already exists
  194. $this->logfile_handle = fopen($this->logfile_name, 'a');
  195. $this->opened_log_time = microtime(true);
  196. $this->log("Opened log file at time: ".date('r'));
  197. global $wp_version;
  198. $logline = "UpdraftPlus: ".$this->version." WordPress: ".$wp_version." PHP: ".phpversion()." (".php_uname().") PHP Max Execution Time: ".@ini_get("max_execution_time")." ZipArchive::addFile exists: ";
  199. // method_exists causes some faulty PHP installations to segfault, leading to support requests
  200. if (version_compare(phpversion(), '5.2.0', '>=') && extension_loaded('zip')) {
  201. $logline .= 'Y';
  202. } else {
  203. $logline .= (method_exists('ZipArchive', 'addFile')) ? "Y" : "N";
  204. }
  205. $this->log($logline);
  206. }
  207. # Logs the given line, adding (relative) time stamp and newline
  208. function log($line) {
  209. if ($this->logfile_handle) fwrite($this->logfile_handle, sprintf("%08.03f", round(microtime(true)-$this->opened_log_time, 3))." ".$line."\n");
  210. UpdraftPlus_Options::update_updraft_option("updraft_lastmessage", $line." (".date('M d H:i:s').")");
  211. }
  212. // This function is used by cloud methods to provide standardised logging, but more importantly to help us detect that meaningful activity took place during a resumption run, so that we can schedule further resumptions if it is worthwhile
  213. function record_uploaded_chunk($percent, $extra) {
  214. // Log it
  215. $service = $this->jobdata_get('service');
  216. $log = ucfirst($service)." chunked upload: $percent % uploaded";
  217. if ($extra) $log .= " ($extra)";
  218. $this->log($log);
  219. // If we are on an 'overtime' resumption run, and we are still meainingfully uploading, then schedule a new resumption
  220. // Our definition of meaningful is that we must maintain an overall average of at least 1% per run, after allowing 9 runs for everything else to get going
  221. // i.e. Max 109 runs = 545 minutes = 9 hrs 05
  222. // If they get 2 minutes on each run, and the file is 1Gb, then that equals 10.2Mb/120s = minimum 87Kb/s upload speed required
  223. if ($this->current_resumption >= 9 && $this->newresumption_scheduled == false && $percent > ( $this->current_resumption - 9)) {
  224. $resume_interval = $this->jobdata_get('resume_interval');
  225. if (!is_numeric($resume_interval) || $resume_interval<200) { $resume_interval = 200; }
  226. $schedule_for = time()+$resume_interval;
  227. $this->newresumption_scheduled = $schedule_for;
  228. $this->log("This is resumption ".$this->current_resumption.", but meaningful uploading is still taking place; so a new one will be scheduled");
  229. wp_schedule_single_event($schedule_for, 'updraft_backup_resume', array($this->current_resumption + 1, $this->nonce));
  230. }
  231. }
  232. function backup_resume($resumption_no, $bnonce) {
  233. @ignore_user_abort(true);
  234. // This is scheduled for 5 minutes after a backup job starts
  235. // Restore state
  236. if ($resumption_no > 0) {
  237. $this->nonce = $bnonce;
  238. $this->backup_time = $this->jobdata_get('backup_time');
  239. $this->logfile_open($bnonce);
  240. }
  241. $btime = $this->backup_time;
  242. $this->log("Backup run: resumption=$resumption_no, nonce=$bnonce, begun at=$btime");
  243. $this->current_resumption = $resumption_no;
  244. // Schedule again, to run in 5 minutes again, in case we again fail
  245. // The actual interval can be increased (for future resumptions) by other code, if it detects apparent overlapping
  246. $resume_interval = $this->jobdata_get('resume_interval');
  247. if (!is_numeric($resume_interval) || $resume_interval<200) $resume_interval = 200;
  248. // A different argument than before is needed otherwise the event is ignored
  249. $next_resumption = $resumption_no+1;
  250. if ($next_resumption < 10) {
  251. $this->log("Scheduling a resumption ($next_resumption) in case this run gets aborted");
  252. $schedule_for = time()+$resume_interval;
  253. wp_schedule_single_event($schedule_for, 'updraft_backup_resume', array($next_resumption, $bnonce));
  254. $this->newresumption_scheduled = $schedule_for;
  255. } else {
  256. $this->log("The current run is our tenth attempt - will not schedule a further attempt until we see something useful happening");
  257. }
  258. // This should be always called; if there were no files in this run, it returns us an empty array
  259. $backup_array = $this->resumable_backup_of_files($resumption_no);
  260. // This save, if there was something, is then immediately picked up again
  261. if (is_array($backup_array)) $this->save_backup_history($backup_array);
  262. // Returns an array, most recent first, of backup sets
  263. $backup_history = $this->get_backup_history();
  264. if (!isset($backup_history[$btime])) {
  265. $this->log("Could not find a record in the database of a backup with this timestamp");
  266. }
  267. $our_files=$backup_history[$btime];
  268. if (!is_array($our_files)) $our_files = array();
  269. $undone_files = array();
  270. $backup_database = $this->jobdata_get('backup_database');
  271. // The transient is read and written below (instead of using the existing variable) so that we can copy-and-paste this part as needed.
  272. if ($backup_database == "begun" || $backup_database == 'finished' || $backup_database == 'encrypted') {
  273. if ($backup_database == "begun") {
  274. if ($resumption_no > 0) {
  275. $this->log("Resuming creation of database dump");
  276. } else {
  277. $this->log("Beginning creation of database dump");
  278. }
  279. } elseif ($backup_database == 'encrypted') {
  280. $this->log("Database dump: Creation and encryption were completed already");
  281. } else {
  282. $this->log("Database dump: Creation was completed already");
  283. }
  284. $db_backup = $this->backup_db($backup_database);
  285. if(is_array($our_files) && is_string($db_backup)) $our_files['db'] = $db_backup;
  286. if ($backup_database != 'encrypted') $this->jobdata_set("backup_database", 'finished');
  287. } else {
  288. $this->log("Unrecognised data when trying to ascertain if the database was backed up ($backup_database)");
  289. }
  290. // Save this to our history so we can track backups for the retain feature
  291. $this->log("Saving backup history");
  292. // This is done before cloud despatch, because we want a record of what *should* be in the backup. Whether it actually makes it there or not is not yet known.
  293. $this->save_backup_history($our_files);
  294. // Potentially encrypt the database if it is not already
  295. if (isset($our_files['db']) && !preg_match("/\.crypt$/", $our_files['db'])) {
  296. $our_files['db'] = $this->encrypt_file($our_files['db']);
  297. $this->save_backup_history($our_files);
  298. if (preg_match("/\.crypt$/", $our_files['db'])) $this->jobdata_set("backup_database", 'encrypted');
  299. }
  300. foreach ($our_files as $key => $file) {
  301. // Only continue if the stored info was about a dump
  302. if ($key != 'plugins' && $key != 'themes' && $key != 'others' && $key != 'uploads' && $key != 'db') continue;
  303. $hash = md5($file);
  304. $fullpath = $this->backups_dir_location().'/'.$file;
  305. if ($this->jobdata_get("uploaded_$hash") === "yes") {
  306. $this->log("$file: $key: This file has already been successfully uploaded");
  307. } elseif (is_file($fullpath)) {
  308. $this->log("$file: $key: This file has not yet been successfully uploaded: will queue");
  309. $undone_files[$key] = $file;
  310. } else {
  311. $this->log("$file: Note: This file was not marked as successfully uploaded, but does not exist on the local filesystem");
  312. $this->uploaded_file($file);
  313. }
  314. }
  315. if (count($undone_files) == 0) {
  316. $this->log("There were no more files that needed uploading; backup job is complete");
  317. // No email, as the user probably already got one if something else completed the run
  318. $this->backup_finish($next_resumption, true, false, $resumption_no);
  319. return;
  320. }
  321. $this->log("Requesting backup of the files that were not successfully uploaded");
  322. $this->cloud_backup($undone_files);
  323. $this->log("Resume backup ($bnonce, $resumption_no): finish run");
  324. if (is_array($our_files)) $this->save_last_backup($our_files);
  325. $this->backup_finish($next_resumption, true, true, $resumption_no);
  326. }
  327. function backup_all() {
  328. $this->boot_backup(true,true);
  329. }
  330. function backup_files() {
  331. # Note that the "false" for database gets over-ridden automatically if they turn out to have the same schedules
  332. $this->boot_backup(true,false);
  333. }
  334. function backup_database() {
  335. # Note that nothing will happen if the file backup had the same schedule
  336. $this->boot_backup(false,true);
  337. }
  338. function jobdata_set($key, $value) {
  339. if (is_array($this->jobdata)) {
  340. $this->jobdata[$key] = $value;
  341. } else {
  342. $this->jobdata = array($key => $value);
  343. }
  344. set_transient("updraft_jobdata_".$this->nonce, $this->jobdata, 14400);
  345. }
  346. function jobdata_get($key) {
  347. if (!is_array($this->jobdata)) {
  348. $this->jobdata = get_transient("updraft_jobdata_".$this->nonce);
  349. if (!is_array($this->jobdata)) return false;
  350. }
  351. return (isset($this->jobdata[$key])) ? $this->jobdata[$key] : false;
  352. }
  353. // This uses a transient; its only purpose is to indicate *total* completion; there is no actual danger, just wasted time, in resuming when it was not needed. So the transient just helps save resources.
  354. function resumable_backup_of_files($resumption_no) {
  355. //backup directories and return a numerically indexed array of file paths to the backup files
  356. $transient_status = $this->jobdata_get('backup_files');
  357. if ($transient_status == 'finished') {
  358. $this->log("Creation of backups of directories: already finished");
  359. } elseif ($transient_status == "begun") {
  360. if ($resumption_no>0) {
  361. $this->log("Creation of backups of directories: had begun; will resume");
  362. } else {
  363. $this->log("Creation of backups of directories: beginning");
  364. }
  365. } else {
  366. # This is not necessarily a backup run which is meant to contain files at all
  367. $this->log("This backup run is not intended for files - skipping");
  368. return array();
  369. }
  370. // We want this array, even if already finished
  371. $backup_array = $this->backup_dirs($transient_status);
  372. // This can get over-written later
  373. $this->jobdata_set('backup_files', 'finished');
  374. return $backup_array;
  375. }
  376. // This procedure initiates a backup run
  377. function boot_backup($backup_files, $backup_database) {
  378. @ignore_user_abort(true);
  379. //generate backup information
  380. $this->backup_time_nonce();
  381. $this->logfile_open($this->nonce);
  382. // Some house-cleaning
  383. $this->clean_temporary_files();
  384. // Log some information that may be helpful
  385. $this->log("Tasks: Backup files: $backup_files (schedule: ".UpdraftPlus_Options::get_updraft_option('updraft_interval', 'unset').") Backup DB: $backup_database (schedule: ".UpdraftPlus_Options::get_updraft_option('updraft_interval_database', 'unset').")");
  386. # If the files and database schedules are the same, and if this the file one, then we rope in database too.
  387. # On the other hand, if the schedules were the same and this was the database run, then there is nothing to do.
  388. if (UpdraftPlus_Options::get_updraft_option('updraft_interval') == UpdraftPlus_Options::get_updraft_option('updraft_interval_database') || UpdraftPlus_Options::get_updraft_option('updraft_interval_database','xyz') == 'xyz' ) {
  389. $backup_database = ($backup_files == true) ? true : false;
  390. }
  391. $this->log("Processed schedules. Tasks now: Backup files: $backup_files Backup DB: $backup_database");
  392. # If nothing to be done, then just finish
  393. if (!$backup_files && !$backup_database) {
  394. $this->backup_finish(1, false, false, 0);
  395. return;
  396. }
  397. // Save what *should* be done, to make it resumable from this point on
  398. if ($backup_database) $this->jobdata_set("backup_database", 'begun');
  399. if ($backup_files) $this->jobdata_set('backup_files', 'begun');
  400. $this->jobdata_set('service', UpdraftPlus_Options::get_updraft_option('updraft_service'));
  401. // This can be adapted if we see a need
  402. $this->jobdata_set('resume_interval', 300);
  403. $this->jobdata_set('backup_time', $this->backup_time);
  404. // Everthing is now set up; now go
  405. $this->backup_resume(0, $this->nonce);
  406. }
  407. // Encrypts the file if the option is set; returns the basename of the file (according to whether it was encrypted or nto)
  408. function encrypt_file($file) {
  409. $encryption = UpdraftPlus_Options::get_updraft_option('updraft_encryptionphrase');
  410. if (strlen($encryption) > 0) {
  411. $this->log("$file: applying encryption");
  412. $encryption_error = 0;
  413. $microstart = microtime(true);
  414. require_once(UPDRAFTPLUS_DIR.'/includes/Rijndael.php');
  415. $rijndael = new Crypt_Rijndael();
  416. $rijndael->setKey($encryption);
  417. $updraft_dir = $this->backups_dir_location();
  418. $file_size = @filesize($updraft_dir.'/'.$file)/1024;
  419. if (false === file_put_contents($updraft_dir.'/'.$file.'.crypt' , $rijndael->encrypt(file_get_contents($updraft_dir.'/'.$file)))) {$encryption_error = 1;}
  420. if (0 == $encryption_error) {
  421. $time_taken = max(0.000001, microtime(true)-$microstart);
  422. $this->log("$file: encryption successful: ".round($file_size,1)."Kb in ".round($time_taken,1)."s (".round($file_size/$time_taken, 1)."Kb/s)");
  423. # Delete unencrypted file
  424. @unlink($updraft_dir.'/'.$file);
  425. return basename($file.'.crypt');
  426. } else {
  427. $this->log("Encryption error occurred when encrypting database. Encryption aborted.");
  428. $this->error("Encryption error occurred when encrypting database. Encryption aborted.");
  429. return basename($file);
  430. }
  431. } else {
  432. return basename($file);
  433. }
  434. }
  435. function backup_finish($cancel_event, $clear_nonce_transient, $allow_email, $resumption_no) {
  436. // In fact, leaving the hook to run (if debug is set) is harmless, as the resume job should only do tasks that were left unfinished, which at this stage is none.
  437. if (empty($this->errors)) {
  438. if ($clear_nonce_transient) {
  439. $this->log("There were no errors in the uploads, so the 'resume' event is being unscheduled");
  440. wp_clear_scheduled_hook('updraft_backup_resume', array($cancel_event, $this->nonce));
  441. // TODO: Delete the job transient (is presently useful for debugging, and only lasts 4 hours)
  442. }
  443. } else {
  444. $this->log("There were errors in the uploads, so the 'resume' event is remaining scheduled");
  445. }
  446. // Send the results email if appropriate, which means:
  447. // - The caller allowed it (which is not the case in an 'empty' run)
  448. // - And: An email address was set (which must be so in email mode)
  449. // And one of:
  450. // - Debug mode
  451. // - There were no errors (which means we completed and so this is the final run - time for the final report)
  452. // - It was the tenth resumption; everything failed
  453. $send_an_email = false;
  454. // Make sure that the final status is shown
  455. if (empty($this->errors)) {
  456. $send_an_email = true;
  457. $final_message = "The backup apparently succeeded and is now complete";
  458. } elseif ($this->newresumption_scheduled == false) {
  459. $send_an_email = true;
  460. $final_message = "The backup attempt has finished, apparently unsuccessfully";
  461. } else {
  462. // There are errors, but a resumption will be attempted
  463. $final_message = "The backup has not finished; a resumption is scheduled within 5 minutes";
  464. }
  465. // Now over-ride the decision to send an email, if needed
  466. if (UpdraftPlus_Options::get_updraft_option('updraft_debug_mode')) {
  467. $send_an_email = true;
  468. $this->log("An email has been scheduled for this job, because we are in debug mode");
  469. }
  470. // If there's no email address, or the set was empty, that is the final over-ride: don't send
  471. if (!$allow_email) {
  472. $send_an_email = false;
  473. $this->log("No email will be sent - this backup set was empty.");
  474. } elseif (UpdraftPlus_Options::get_updraft_option('updraft_email') == '') {
  475. $send_an_email = false;
  476. $this->log("No email will/can be sent - the user has not configured an email address.");
  477. }
  478. if ($send_an_email) $this->send_results_email($final_message);
  479. $this->log($final_message);
  480. @fclose($this->logfile_handle);
  481. // Don't delete the log file now; delete it upon rotation
  482. //if (!UpdraftPlus_Options::get_updraft_option('updraft_debug_mode')) @unlink($this->logfile_name);
  483. }
  484. function send_results_email($final_message) {
  485. $debug_mode = UpdraftPlus_Options::get_updraft_option('updraft_debug_mode');
  486. $sendmail_to = UpdraftPlus_Options::get_updraft_option('updraft_email');
  487. $backup_files = $this->jobdata_get('backup_files');
  488. $backup_db = $this->jobdata_get("backup_database");
  489. if ($backup_files == 'finished' && ( $backup_db == 'finished' || $backup_db == 'encrypted' ) ) {
  490. $backup_contains = "Files and database";
  491. } elseif ($backup_files == 'finished') {
  492. $backup_contains = ($backup_db == "begun") ? "Files (database backup has not completed)" : "Files only (database was not part of this particular schedule)";
  493. } elseif ($backup_db == 'finished' || $backup_db == 'encrypted') {
  494. $backup_contains = ($backup_files == "begun") ? "Database (files backup has not completed)" : "Database only (files were not part of this particular schedule)";
  495. } else {
  496. $backup_contains = "Unknown/unexpected error - please raise a support request";
  497. }
  498. $this->log("Sending email ('$backup_contains') report to: ".substr($sendmail_to, 0, 5)."...");
  499. $append_log = ($debug_mode && $this->logfile_name != "") ? "\r\nLog contents:\r\n".file_get_contents($this->logfile_name) : "" ;
  500. wp_mail($sendmail_to,'Backed up: '.get_bloginfo('name').' (UpdraftPlus '.$this->version.') '.date('Y-m-d H:i',time()),'Site: '.site_url()."\r\nUpdraftPlus WordPress backup is complete.\r\nBackup contains: ".$backup_contains."\r\nLatest status: $final_message\r\n\r\n".$this->wordshell_random_advert(0)."\r\n".$append_log);
  501. }
  502. function save_last_backup($backup_array) {
  503. $success = (empty($this->errors)) ? 1 : 0;
  504. $last_backup = array('backup_time'=>$this->backup_time, 'backup_array'=>$backup_array, 'success'=>$success, 'errors'=>$this->errors, 'backup_nonce' => $this->nonce);
  505. UpdraftPlus_Options::update_updraft_option('updraft_last_backup', $last_backup);
  506. }
  507. // This should be called whenever a file is successfully uploaded
  508. function uploaded_file($file, $id = false) {
  509. $hash = md5($file);
  510. $this->log("Recording as successfully uploaded: $file ($hash)");
  511. $this->jobdata_set("uploaded_$hash", "yes");
  512. if ($id) {
  513. $ids = UpdraftPlus_Options::get_updraft_option('updraft_file_ids', array() );
  514. $ids[$file] = $id;
  515. UpdraftPlus_Options::update_updraft_option('updraft_file_ids',$ids);
  516. $this->log("Stored file<->id correlation in database ($file <-> $id)");
  517. }
  518. // Delete local files immediately if the option is set
  519. // Where we are only backing up locally, only the "prune" function should do deleting
  520. if ($this->jobdata_get('service') != '' && $this->jobdata_get('service') != 'none') $this->delete_local($file);
  521. }
  522. // Dispatch to the relevant function
  523. function cloud_backup($backup_array) {
  524. $service = $this->jobdata_get('service');
  525. $this->log("Cloud backup selection: ".$service);
  526. @set_time_limit(900);
  527. $method_include = UPDRAFTPLUS_DIR.'/methods/'.$service.'.php';
  528. if (file_exists($method_include)) require_once($method_include);
  529. if ($service == "none") {
  530. $this->log("No remote despatch: user chose no remote backup service");
  531. } else {
  532. $this->log("Beginning dispatch of backup to remote");
  533. }
  534. $objname = "UpdraftPlus_BackupModule_${service}";
  535. if (method_exists($objname, "backup")) {
  536. // New style - external, allowing more plugability
  537. $remote_obj = new $objname;
  538. $remote_obj->backup($backup_array);
  539. } elseif ($service == "none") {
  540. $this->prune_retained_backups("none", null, null);
  541. }
  542. }
  543. function prune_file($service, $dofile, $method_object = null, $object_passback = null ) {
  544. $this->log("Delete this file: $dofile, service=$service");
  545. $fullpath = $this->backups_dir_location().'/'.$dofile;
  546. // delete it if it's locally available
  547. if (file_exists($fullpath)) {
  548. $this->log("Deleting local copy ($fullpath)");
  549. @unlink($fullpath);
  550. }
  551. // Despatch to the particular method's deletion routine
  552. if (!is_null($method_object)) $method_object->delete($dofile, $object_passback);
  553. }
  554. // Carries out retain behaviour. Pass in a valid S3 or FTP object and path if relevant.
  555. function prune_retained_backups($service, $backup_method_object = null, $backup_passback = null) {
  556. // If they turned off deletion on local backups, then there is nothing to do
  557. if (UpdraftPlus_Options::get_updraft_option('updraft_delete_local') == 0 && $service == 'none') {
  558. $this->log("Prune old backups from local store: nothing to do, since the user disabled local deletion and we are using local backups");
  559. return;
  560. }
  561. $this->log("Retain: beginning examination of existing backup sets");
  562. // Number of backups to retain - files
  563. $updraft_retain = UpdraftPlus_Options::get_updraft_option('updraft_retain', 1);
  564. $updraft_retain = (is_numeric($updraft_retain)) ? $updraft_retain : 1;
  565. $this->log("Retain files: user setting: number to retain = $updraft_retain");
  566. // Number of backups to retain - db
  567. $updraft_retain_db = UpdraftPlus_Options::get_updraft_option('updraft_retain_db', $updraft_retain);
  568. $updraft_retain_db = (is_numeric($updraft_retain_db)) ? $updraft_retain_db : 1;
  569. $this->log("Retain db: user setting: number to retain = $updraft_retain_db");
  570. // Returns an array, most recent first, of backup sets
  571. $backup_history = $this->get_backup_history();
  572. $db_backups_found = 0;
  573. $file_backups_found = 0;
  574. $this->log("Number of backup sets in history: ".count($backup_history));
  575. foreach ($backup_history as $backup_datestamp => $backup_to_examine) {
  576. // $backup_to_examine is an array of file names, keyed on db/plugins/themes/uploads
  577. // The new backup_history array is saved afterwards, so remember to unset the ones that are to be deleted
  578. $this->log("Examining backup set with datestamp: $backup_datestamp");
  579. if (isset($backup_to_examine['db'])) {
  580. $db_backups_found++;
  581. $this->log("$backup_datestamp: this set includes a database (".$backup_to_examine['db']."); db count is now $db_backups_found");
  582. if ($db_backups_found > $updraft_retain_db) {
  583. $this->log("$backup_datestamp: over retain limit ($updraft_retain_db); will delete this database");
  584. $dofile = $backup_to_examine['db'];
  585. if (!empty($dofile)) $this->prune_file($service, $dofile, $backup_method_object, $backup_passback);
  586. unset($backup_to_examine['db']);
  587. }
  588. }
  589. if (isset($backup_to_examine['plugins']) || isset($backup_to_examine['themes']) || isset($backup_to_examine['uploads']) || isset($backup_to_examine['others'])) {
  590. $file_backups_found++;
  591. $this->log("$backup_datestamp: this set includes files; fileset count is now $file_backups_found");
  592. if ($file_backups_found > $updraft_retain) {
  593. $this->log("$backup_datestamp: over retain limit ($updraft_retain); will delete this file set");
  594. $file = isset($backup_to_examine['plugins']) ? $backup_to_examine['plugins'] : "";
  595. $file2 = isset($backup_to_examine['themes']) ? $backup_to_examine['themes'] : "";
  596. $file3 = isset($backup_to_examine['uploads']) ? $backup_to_examine['uploads'] : "";
  597. $file4 = isset($backup_to_examine['others']) ? $backup_to_examine['others'] : "";
  598. foreach (array($file, $file2, $file3, $file4) as $dofile) {
  599. if (!empty($dofile)) $this->prune_file($service, $dofile, $backup_method_object, $backup_passback);
  600. }
  601. unset($backup_to_examine['plugins']);
  602. unset($backup_to_examine['themes']);
  603. unset($backup_to_examine['uploads']);
  604. unset($backup_to_examine['others']);
  605. }
  606. }
  607. // Delete backup set completely if empty, o/w just remove DB
  608. if (count($backup_to_examine) == 0 || (count($backup_to_examine) == 1 && isset($backup_to_examine['nonce']))) {
  609. $this->log("$backup_datestamp: this backup set is now empty; will remove from history");
  610. unset($backup_history[$backup_datestamp]);
  611. if (isset($backup_to_examine['nonce'])) {
  612. $fullpath = $this->backups_dir_location().'/log.'.$backup_to_examine['nonce'].'.txt';
  613. if (is_file($fullpath)) {
  614. $this->log("$backup_datestamp: deleting log file (log.".$backup_to_examine['nonce'].".txt)");
  615. @unlink($fullpath);
  616. } else {
  617. $this->log("$backup_datestamp: corresponding log file not found - must have already been deleted");
  618. }
  619. } else {
  620. $this->log("$backup_datestamp: no nonce record found in the backup set, so cannot delete any remaining log file");
  621. }
  622. } else {
  623. $this->log("$backup_datestamp: this backup set remains non-empty; will retain in history");
  624. $backup_history[$backup_datestamp] = $backup_to_examine;
  625. }
  626. }
  627. $this->log("Retain: saving new backup history (sets now: ".count($backup_history).") and finishing retain operation");
  628. UpdraftPlus_Options::update_updraft_option('updraft_backup_history',$backup_history);
  629. }
  630. function delete_local($file) {
  631. if(UpdraftPlus_Options::get_updraft_option('updraft_delete_local')) {
  632. $this->log("Deleting local file: $file");
  633. //need error checking so we don't delete what isn't successfully uploaded?
  634. $fullpath = $this->backups_dir_location().'/'.$file;
  635. return unlink($fullpath);
  636. }
  637. return true;
  638. }
  639. function reschedule($how_far_ahead) {
  640. // Reschedule - remove presently scheduled event
  641. wp_clear_scheduled_hook('updraft_backup_resume', array($this->current_resumption + 1, $this->nonce));
  642. // Add new event
  643. if ($how_far_ahead < 200) $how_far_ahead=200;
  644. $schedule_for = time() + $how_far_ahead;
  645. wp_schedule_single_event($schedule_for, 'updraft_backup_resume', array($this->current_resumption + 1, $this->nonce));
  646. $this->newresumption_scheduled = $schedule_for;
  647. }
  648. function increase_resume_and_reschedule($howmuch = 120) {
  649. $resume_interval = $this->jobdata_get('resume_interval');
  650. if (!is_numeric($resume_interval) || $resume_interval<200) { $resume_interval = 200; }
  651. if ($this->newresumption_scheduled != false) $this->reschedule($resume_interval+$howmuch);
  652. $this->jobdata_set('resume_interval', $resume_interval+$howmuch);
  653. $this->log("To decrease the likelihood of overlaps, increasing resumption interval to: ".($resume_interval+$howmuch));
  654. }
  655. function create_zip($create_from_dir, $whichone, $create_in_dir, $backup_file_basename) {
  656. // Note: $create_from_dir can be an array or a string
  657. @set_time_limit(900);
  658. if ($whichone != "others") $this->log("Beginning creation of dump of $whichone");
  659. $full_path = $create_in_dir.'/'.$backup_file_basename.'-'.$whichone.'.zip';
  660. if (file_exists($full_path)) {
  661. $this->log("$backup_file_basename-$whichone.zip: this file has already been created");
  662. return basename($full_path);
  663. }
  664. // Temporary file, to be able to detect actual completion (upon which, it is renamed)
  665. // Firstly, make sure that the temporary file is not already being written to - which can happen if a resumption takes place whilst an old run is still active
  666. $zip_name = $full_path.'.tmp';
  667. $time_now = time();
  668. $time_mod = (int)@filemtime($zip_name);
  669. if (file_exists($zip_name) && $time_mod>100 && ($time_now-$time_mod)<30) {
  670. $file_size = filesize($zip_name);
  671. $this->log("Terminate: the temporary file $zip_name already exists, and was modified within the last 30 seconds (time_mod=$time_mod, time_now=$time_now, diff=".($time_now-$time_mod).", size=$file_size). This likely means that another UpdraftPlus run is still at work; so we will exit.");
  672. $this->increase_resume_and_reschedule(120);
  673. die;
  674. } elseif (file_exists($zip_name)) {
  675. $this->log("File exists ($zip_name), but was apparently not modified within the last 30 seconds, so we assume that any previous run has now terminated (time_mod=$time_mod, time_now=$time_now, diff=".($time_now-$time_mod).")");
  676. }
  677. $microtime_start = microtime(true);
  678. # The paths in the zip should then begin with '$whichone', having removed WP_CONTENT_DIR from the front
  679. $zipcode = $this->make_zipfile($create_from_dir, $zip_name);
  680. if ($zipcode !== true) {
  681. $this->log("ERROR: Zip failure: /*Could not create*/ $whichone zip: code=$zipcode");
  682. $this->error("Could not create $whichone zip: code $zipcode. Consult the log file for more information.");
  683. return false;
  684. } else {
  685. rename($full_path.'.tmp', $full_path);
  686. $timetaken = max(microtime(true)-$microtime_start, 0.000001);
  687. $kbsize = filesize($full_path)/1024;
  688. $rate = round($kbsize/$timetaken, 1);
  689. $this->log("Created $whichone zip - file size is ".round($kbsize,1)." Kb in ".round($timetaken,1)." s ($rate Kb/s)");
  690. }
  691. return basename($full_path);
  692. }
  693. // This function is resumable
  694. function backup_dirs($transient_status) {
  695. if(!$this->backup_time) $this->backup_time_nonce();
  696. $updraft_dir = $this->backups_dir_location();
  697. if(!is_writable($updraft_dir)) {
  698. $this->log("Backup directory ($updraft_dir) is not writable, or does not exist");
  699. $this->error("Backup directory ($updraft_dir) is not writable, or does not exist.");
  700. return array();
  701. }
  702. //get the blog name and rip out all non-alphanumeric chars other than _
  703. $blog_name = str_replace(' ','_',substr(get_bloginfo(), 0, 96));
  704. $blog_name = preg_replace('/[^A-Za-z0-9_]/','', $blog_name);
  705. if(!$blog_name) $blog_name = 'non_alpha_name';
  706. $backup_file_basename = 'backup_'.date('Y-m-d-Hi', $this->backup_time).'_'.$blog_name.'_'.$this->nonce;
  707. $backup_array = array();
  708. $wp_themes_dir = WP_CONTENT_DIR.'/themes';
  709. $wp_upload_dir = wp_upload_dir();
  710. $wp_upload_dir = $wp_upload_dir['basedir'];
  711. $wp_plugins_dir = WP_PLUGIN_DIR;
  712. $possible_backups = array ('plugins' => $wp_plugins_dir, 'themes' => $wp_themes_dir, 'uploads' => $wp_upload_dir);
  713. # Plugins, themes, uploads
  714. foreach ($possible_backups as $youwhat => $whichdir) {
  715. if (UpdraftPlus_Options::get_updraft_option("updraft_include_$youwhat", true)) {
  716. if ($transient_status == 'finished') {
  717. $backup_array[$youwhat] = $backup_file_basename.'-'.$youwhat.'.zip';
  718. } else {
  719. $created = $this->create_zip($whichdir, $youwhat, $updraft_dir, $backup_file_basename);
  720. if ($created) $backup_array[$youwhat] = $created;
  721. }
  722. } else {
  723. $this->log("No backup of $youwhat: excluded by user's options");
  724. }
  725. }
  726. # Others
  727. if (UpdraftPlus_Options::get_updraft_option('updraft_include_others', true)) {
  728. if ($transient_status == 'finished') {
  729. $backup_array['others'] = $backup_file_basename.'-others.zip';
  730. } else {
  731. $this->log("Beginning backup of other directories found in the content directory");
  732. // http://www.phpconcept.net/pclzip/user-guide/53
  733. /* First parameter to create is:
  734. An array of filenames or dirnames,
  735. or
  736. A string containing the filename or a dirname,
  737. or
  738. A string containing a list of filename or dirname separated by a comma.
  739. */
  740. # Initialise
  741. $other_dirlist = array();
  742. $others_skip = preg_split("/,/",UpdraftPlus_Options::get_updraft_option('updraft_include_others_exclude', UPDRAFT_DEFAULT_OTHERS_EXCLUDE));
  743. # Make the values into the keys
  744. $others_skip = array_flip($others_skip);
  745. $this->log('Looking for candidates to back up in: '.WP_CONTENT_DIR);
  746. if ($handle = opendir(WP_CONTENT_DIR)) {
  747. while (false !== ($entry = readdir($handle))) {
  748. $candidate = WP_CONTENT_DIR.'/'.$entry;
  749. if ($entry == "." || $entry == "..") { ; }
  750. elseif ($candidate == $updraft_dir) { $this->log("others: $entry: skipping: this is the updraft directory"); }
  751. elseif ($candidate == $wp_themes_dir) { $this->log("others: $entry: skipping: this is the themes directory"); }
  752. elseif ($candidate == $wp_upload_dir) { $this->log("others: $entry: skipping: this is the uploads directory"); }
  753. elseif ($candidate == $wp_plugins_dir) { $this->log("others: $entry: skipping: this is the plugins directory"); }
  754. elseif (isset($others_skip[$entry])) { $this->log("others: $entry: skipping: excluded by options"); }
  755. else { $this->log("others: $entry: adding to list"); array_push($other_dirlist, $candidate); }
  756. }
  757. @closedir($handle);
  758. } else {
  759. $this->log('ERROR: Could not read the content directory: '.WP_CONTENT_DIR);
  760. $this->error('Could not read the content directory: '.WP_CONTENT_DIR);
  761. }
  762. if (count($other_dirlist)>0) {
  763. $created = $this->create_zip($other_dirlist, 'others', $updraft_dir, $backup_file_basename);
  764. if ($created) $backup_array['others'] = $created;
  765. } else {
  766. $this->log("No backup of other directories: there was nothing found to back up");
  767. }
  768. # If we are not already finished
  769. }
  770. } else {
  771. $this->log("No backup of other directories: excluded by user's options");
  772. }
  773. return $backup_array;
  774. }
  775. function save_backup_history($backup_array) {
  776. if(is_array($backup_array)) {
  777. $backup_history = UpdraftPlus_Options::get_updraft_option('updraft_backup_history');
  778. $backup_history = (is_array($backup_history)) ? $backup_history : array();
  779. $backup_array['nonce'] = $this->nonce;
  780. $backup_history[$this->backup_time] = $backup_array;
  781. UpdraftPlus_Options::update_updraft_option('updraft_backup_history',$backup_history);
  782. } else {
  783. $this->log('Could not save backup history because we have no backup array. Backup probably failed.');
  784. $this->error('Could not save backup history because we have no backup array. Backup probably failed.');
  785. }
  786. }
  787. function get_backup_history() {
  788. //$backup_history = UpdraftPlus_Options::get_updraft_option('updraft_backup_history');
  789. //by doing a raw DB query to get the most up-to-date data from this option we slightly narrow the window for the multiple-cron race condition
  790. global $wpdb;
  791. $backup_history = @unserialize($wpdb->get_var($wpdb->prepare("SELECT option_value from $wpdb->options WHERE option_name='updraft_backup_history'")));
  792. if(is_array($backup_history)) {
  793. krsort($backup_history); //reverse sort so earliest backup is last on the array. Then we can array_pop.
  794. } else {
  795. $backup_history = array();
  796. }
  797. return $backup_history;
  798. }
  799. // Open a file, store its filehandle
  800. function backup_db_open($file, $allow_gz = true) {
  801. if (function_exists('gzopen') && $allow_gz == true) {
  802. $this->dbhandle = @gzopen($file, 'w');
  803. $this->dbhandle_isgz = true;
  804. } else {
  805. $this->dbhandle = @fopen($file, 'w');
  806. $this->dbhandle_isgz = false;
  807. }
  808. if(!$this->dbhandle) {
  809. $this->log("ERROR: $file: Could not open the backup file for writing");
  810. $this->error("$file: Could not open the backup file for writing");
  811. }
  812. }
  813. function backup_db_header() {
  814. //Begin new backup of MySql
  815. $this->stow("# " . 'WordPress MySQL database backup' . "\n");
  816. $this->stow("#\n");
  817. $this->stow("# " . sprintf(__('Generated: %s','wp-db-backup'),date("l j. F Y H:i T")) . "\n");
  818. $this->stow("# " . sprintf(__('Hostname: %s','wp-db-backup'),DB_HOST) . "\n");
  819. $this->stow("# " . sprintf(__('Database: %s','wp-db-backup'),$this->backquote(DB_NAME)) . "\n");
  820. $this->stow("# --------------------------------------------------------\n");
  821. if (defined("DB_CHARSET")) {
  822. $this->stow("/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;\n");
  823. $this->stow("/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;\n");
  824. $this->stow("/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;\n");
  825. $this->stow("/*!40101 SET NAMES " . DB_CHARSET . " */;\n");
  826. }
  827. $this->stow("/*!40101 SET foreign_key_checks = 0 */;\n");
  828. }
  829. /* This function is resumable, using the following method:
  830. - Each table is written out to ($final_filename).table.tmp
  831. - When the writing finishes, it is renamed to ($final_filename).table
  832. - When all tables are finished, they are concatenated into the final file
  833. */
  834. function backup_db($already_done = "begun") {
  835. // Get the file prefix
  836. $updraft_dir = $this->backups_dir_location();
  837. if(!$this->backup_time) $this->backup_time_nonce();
  838. if (!$this->opened_log_time) $this->logfile_open($this->nonce);
  839. // Get the blog name and rip out all non-alphanumeric chars other than _
  840. $blog_name = preg_replace('/[^A-Za-z0-9_]/','', str_replace(' ','_', substr(get_bloginfo(), 0, 96)));
  841. if (!$blog_name) $blog_name = 'non_alpha_name';
  842. $file_base = 'backup_'.date('Y-m-d-Hi',$this->backup_time).'_'.$blog_name.'_'.$this->nonce;
  843. $backup_file_base = $updraft_dir.'/'.$file_base;
  844. if ('finished' == $already_done) return basename($backup_file_base.'-db.gz');
  845. if ('encrypted' == $already_done) return basename($backup_file_base.'-db.gz.crypt');
  846. $total_tables = 0;
  847. global $table_prefix, $wpdb;
  848. $all_tables = $wpdb->get_results("SHOW TABLES", ARRAY_N);
  849. $all_tables = array_map(create_function('$a', 'return $a[0];'), $all_tables);
  850. if (!is_writable($updraft_dir)) {
  851. $this->log("The backup directory ($updraft_dir) is not writable.");
  852. $this->error("The backup directory ($updraft_dir) is not writable.");
  853. return false;
  854. }
  855. $stitch_files = array();
  856. foreach ($all_tables as $table) {
  857. $total_tables++;
  858. // Increase script execution time-limit to 15 min for every table.
  859. if ( !@ini_get('safe_mode') || strtolower(@ini_get('safe_mode')) == "off") @set_time_limit(15*60);
  860. // The table file may already exist if we have produced it on a previous run
  861. $table_file_prefix = $file_base.'-db-table-'.$table.'.table';
  862. if (file_exists($updraft_dir.'/'.$table_file_prefix.'.gz')) {
  863. $this->log("Table $table: corresponding file already exists; moving on");
  864. } else {
  865. // Open file, store the handle
  866. $this->backup_db_open($updraft_dir.'/'.$table_file_prefix.'.tmp.gz', true);
  867. # === is needed, otherwise 'false' matches (i.e. prefix does not match)
  868. if ( strpos($table, $table_prefix) === 0 ) {
  869. // Create the SQL statements
  870. $this->stow("# --------------------------------------------------------\n");
  871. $this->stow("# " . sprintf(__('Table: %s','wp-db-backup'),$this->backquote($table)) . "\n");
  872. $this->stow("# --------------------------------------------------------\n");
  873. $this->backup_table($table);
  874. } else {
  875. $this->stow("# --------------------------------------------------------\n");
  876. $this->stow("# " . sprintf(__('Skipping non-WP table: %s','wp-db-backup'),$this->backquote($table)) . "\n");
  877. $this->stow("# --------------------------------------------------------\n");
  878. }
  879. // Close file
  880. $this->close($this->dbhandle);
  881. $this->log("Table $table: finishing file (${table_file_prefix}.gz)");
  882. rename($updraft_dir.'/'.$table_file_prefix.'.tmp.gz', $updraft_dir.'/'.$table_file_prefix.'.gz');
  883. }
  884. $stitch_files[] = $table_file_prefix;
  885. }
  886. // Race detection - with zip files now being resumable, these can more easily occur, with two running side-by-side
  887. $backup_final_file_name = $backup_file_base.'-db.gz';
  888. $time_now = time();
  889. $time_mod = (int)@filemtime($backup_final_file_name);
  890. if (file_exists($backup_final_file_name) && $time_mod>100 && ($time_now-$time_mod)<20) {
  891. $file_size = filesize($backup_final_file_name);
  892. $this->log("Terminate: the final database file ($backup_final_file_name) exists, and was modified within the last 20 seconds (time_mod=$time_mod, time_now=$time_now, diff=".($time_now-$time_mod).", size=$file_size). This likely means that another UpdraftPlus run is at work; so we will exit.");
  893. $this->increase_resume_and_reschedule(120);
  894. die;
  895. } elseif (file_exists($backup_final_file_name)) {
  896. $this->log("The final database file ($backup_final_file_name) exists, but was apparently not modified within the last 20 seconds (time_mod=$time_mod, time_now=$time_now, diff=".($time_now-$time_mod)."). Thus we assume that another UpdraftPlus terminated; thus we will continue.");
  897. }
  898. // Finally, stitch the files together
  899. $this->backup_db_open($backup_final_file_name, true);
  900. $this->backup_db_header();
  901. // We delay the unlinking because if two runs go concurrently and fail to detect each other (should not happen…

Large files files are truncated, but you can click here to view the full file