/** * Initiates the backup procedure. * * @global string $wp_version WordPress version */ public function do_backup($id = '') { global $wp_version; $used_memory = memory_get_usage(true); // Get the memory usage before we do anything. if ($id) { $this->log_file = $this->options['backup_list'][$id]['log']; } else { $id = base_convert($this->time, 10, 36); $this->log_file = $this->local_folder . '/' . $id . '.log'; file_put_contents($this->log_file, "#Fields:\tdate\ttime\ttype\tmessage\n"); $this->options['backup_list'][$id] = array('timestamp' => $this->time, 'title' => $this->options['backup_title'] . ' (' . $id . ')', 'file_path' => '', 'drive_id' => '', 'log' => $this->log_file, 'attempt' => 0); // Save info about our new backup now, so we can have access to the log file even if MySQL connection is stolen. update_option('backup_options', $this->options); // Log environment information. if (!($z = phpversion('zip'))) { $z = 'false'; } $c = ''; if (in_array('curl', $this->options['enabled_transports']) && function_exists('curl_version')) { $c = curl_version(); $c = '; CURL ' . $c['version']; } $env = "Environment: Backup " . $this->version . "; WordPress " . $wp_version . "; PHP " . phpversion() . "; SAPI " . php_sapi_name() . "; OS " . PHP_OS . "; ZIP " . $z . $c; if ((bool) ini_get('safe_mode')) { $env .= "; Safe mode ON"; } $env .= "; Time limit " . ini_get('max_execution_time') . "s" . "; Memory limit " . ini_get('memory_limit'); $this->log("NOTICE", $env); } // We can't hook to WP's shudown hook because we need to pass the $id. register_shutdown_function(array(&$this, 'handle_timeout'), $id); $file_name = sanitize_file_name($this->options['backup_list'][$id]['title']) . '.zip'; $file_path = $this->local_folder . '/' . $file_name; if (@is_file($file_path)) { $this->options['backup_list'][$id]['file_path'] = $file_path; } if (empty($this->options['backup_list'][$id]['file_path'])) { // Check if the backup folder is writable if (!@is_dir($this->local_folder) && !$this->create_dir($this->local_folder) || !@is_writable($this->local_folder)) { $this->log('ERROR', sprintf(__("The directory '%s' does not exist or is not writable.", $this->text_domain), esc_html($this->local_folder))); $this->reschedule_backup($id); } // Create database dump sql file. if (in_array('database', $this->options['source_list'])) { $this->log('NOTICE', __('Attempting to dump database.', $this->text_domain)); if (is_wp_error($dump_time = db_dump($this->dump_file))) { $this->log_wp_error($dump_time); $this->reschedule_backup($id); } $this->log('NOTICE', sprintf(__('The database dump was completed successfully in %s seconds.', $this->text_domain), number_format_i18n($dump_time, 3))); } // Merge the default exclude list with the user provided one and make them absolute paths. $exclude = array_merge($this->options['exclude_list'], $this->exclude); foreach ($exclude as $i => $path) { if (false !== strpos($path, '/') || false !== strpos($path, "\\")) { $exclude[$i] = absolute_path($path, ABSPATH); } } // Create the source list from the user selected sources. $sources = array(); foreach ($this->options['source_list'] as $source) { $sources[] = $this->sources[$source]['path']; } // Remove subdirectories from the sources. $count = count($sources); for ($i = 0; $i < $count; $i++) { for ($j = 0; $j < $count; $j++) { if ($j != $i && isset($sources[$i]) && isset($sources[$j]) && is_subdir($sources[$j], $sources[$i]) && $this->sources['database']['path'] != $sources[$j]) { unset($sources[$j]); } } } // Transform include paths to absolute paths. $include = $this->options['include_list']; foreach ($include as $i => $path) { $include[$i] = absolute_path($path, ABSPATH); } // Merge the include list and the sources $sources = array_merge($sources, $include); // Create archive from the sources list. $this->log('NOTICE', sprintf(__("Attempting to create archive '%s'.", $this->text_domain), esc_html($file_name))); if (!phpversion('zip')) { define('PCLZIP_TEMPORARY_DIR', $this->local_folder); } if (is_wp_error($zip = zip($sources, $file_path, $exclude))) { $this->log_wp_error($zip); delete_path($this->dump_file); $this->reschedule_backup($id); } delete_path($this->dump_file); $this->log('NOTICE', sprintf(__('Successfully archived %1$s files in %2$s seconds. Archive file size is %3$s.', $this->text_domain), number_format_i18n($zip['count']), number_format_i18n($zip['time'], 3), size_format(filesize($file_path), 2))); $this->options['backup_list'][$id]['file_path'] = $file_path; } if ($this->options['drive_number'] > 0 && $this->goauth->is_authorized()) { if (is_wp_error($e = $this->need_gdocs())) { $this->log_wp_error($e); $this->reschedule_backup($id); } if (empty($this->options['backup_list'][$id]['location'])) { $this->log('NOTICE', __("Attempting to upload archive to Google Drive.", $this->text_domain)); $location = $this->gdocs->prepare_upload($this->options['backup_list'][$id]['file_path'], $this->options['backup_list'][$id]['title'], $this->options['drive_folder']); } else { $this->log('NOTICE', __('Attempting to resume upload.', $this->text_domain)); $location = $this->gdocs->resume_upload($this->options['backup_list'][$id]['file_path'], $this->options['backup_list'][$id]['location']); } if (is_wp_error($location)) { $this->log_wp_error($location); $this->reschedule_backup($id); } if (is_string($location)) { $res = $location; $this->log('NOTICE', sprintf(__("Uploading file with title '%s'.", $this->text_domain), esc_html($this->options['backup_list'][$id]['title']))); $d = 0; echo '<div id="progress">'; do { $this->options['backup_list'][$id]['location'] = $res; $res = $this->gdocs->upload_chunk(); $p = $this->gdocs->get_upload_percentage(); if ($p - $d >= 1) { $b = intval($p - $d); echo '<span style="width:' . $b . '%"></span>'; $d += $b; } $this->options['backup_list'][$id]['percentage'] = $p; $this->options['backup_list'][$id]['speed'] = $this->gdocs->get_upload_speed(); } while (is_string($res)); echo '</div>'; if (is_wp_error($res)) { $this->log_wp_error($res); $this->reschedule_backup($id); } $this->log('NOTICE', sprintf(__('The file was successfully uploaded to Google Drive in %1$s seconds at an upload speed of %2$s/s.', $this->text_domain), number_format_i18n($this->gdocs->time_taken(), 3), size_format($this->gdocs->get_upload_speed()))); unset($this->options['backup_list'][$id]['location'], $this->options['backup_list'][$id]['attempt']); } elseif (true === $location) { $this->log('WARNING', sprintf(__("The file '%s' is already uploaded.", $this->text_domain), esc_html($this->options['backup_list'][$id]['file_path']))); } $this->options['backup_list'][$id]['drive_id'] = $this->gdocs->get_file_id(); unset($this->options['backup_list'][$id]['percentage'], $this->options['backup_list'][$id]['speed']); $this->update_quota(); if (empty($this->options['user_info'])) { $this->set_user_info(); } } $this->options['backup_list'][$id]['status'] = 1; $this->purge_backups(); $this->log('NOTICE', sprintf(__('Backup process completed in %1$s seconds.' . ' Initial PHP memory usage was %2$s and the backup process used another %3$s of RAM.', $this->text_domain), number_format_i18n(microtime(true) - $this->time, 3), size_format($used_memory, 2), size_format(memory_get_peak_usage(true) - $used_memory, 2))); }