public static function restore_from_file($backup_dir, $backup_file) { if (mkdir($backup_dir) === false) { throw new Sns_Exception_Unavailable_Operation('Cannot create the directory ' . $backup_dir); } self::restore_item($backup_file, $backup_dir); $options = Sns_Option::get_locations(); foreach ($options as $option => $to) { $item = $option == Sns_Option::DB ? $backup_dir . SNS_DS . 'wp_dump.sql' : $backup_dir . SNS_DS . $option . '.zip'; if (file_exists($item)) { if ($option == Sns_Option::DB) { Sns_Log::log_action('Restoring database'); try { Sns_Backup::import_db($item, true); } catch (Exception $e) { Sns_Log::log_exception_obj($e); } Sns_Log::log_action('Restoring database', SNS_LOG_END); } else { Sns_Log::log_action('Restoring item ' . $item); try { self::restore_item($item, $to); } catch (Exception $e) { Sns_Log::log_exception_obj($e); } Sns_Log::log_action('Restoring item ' . $item, SNS_LOG_END); } } } self::delete_dir($backup_dir); }
public static function export_db($sql_file) { global $wpdb; $export_str = "-- Wordpress database dump --\n"; Sns_Log::log_action('Fetching tables list'); $query = "SHOW TABLES"; $tables = $wpdb->get_results($query, ARRAY_A); if ($tables === false) { throw new Sns_Exception_DB_Error($query); } Sns_Log::log_action('Fetching tables list', SNS_LOG_END); foreach ($tables as $db_item) { $table = current($db_item); Sns_Log::log_action('Fetching table - ' . $table); $query = "SHOW CREATE TABLE " . $table; $create = $wpdb->get_var($query, 1); if ($create === false) { throw new Sns_Exception_DB_Error($query); } $myisam = strpos($create, 'MyISAM'); if ($table == SNS_DB_PREFIX . 'backups' || $table == SNS_DB_PREFIX . 'settings' || $table == SNS_DB_PREFIX . 'options' || $table == SNS_DB_PREFIX . 'settings_destinations' || $table == SNS_DB_PREFIX . 'settings_ftp' || $table == SNS_DB_PREFIX . 'state') { continue; } $export_str .= "-- ----------------------------------------------------------\n"; $export_str .= "-- Dump of table `" . $table . "` --\n"; $export_str .= "-- ----------------------------------------------------------\n\n"; $export_str .= "DROP TABLE IF EXISTS `" . $table . "`;\n\n" . $create . ";\n\n"; $query = "SELECT * FROM `" . $table . "` LIMIT 1000"; $data = $wpdb->get_results($query, ARRAY_A); if ($query === false) { throw new Sns_Exception_DB_Error($query); } if (!empty($data)) { if (false !== $myisam) { $export_str .= "-- !40000 ALTER TABLE `" . $table . "` DISABLE KEYS ;\n\n"; } $offset = 0; while (!empty($data)) { foreach ($data as $entry) { $cols = ''; foreach ($entry as $key => $value) { if (NULL === $value) { $entry[$key] = "NULL"; } elseif ("" === $value || false === $value) { $entry[$key] = "''"; } elseif (!is_numeric($value)) { $entry[$key] = "'" . esc_sql($value) . "'"; } $cols .= ", `" . $key . "` "; } $cols = substr($cols, 1); $export_str .= "INSERT INTO `" . $table . "` ( " . $cols . " ) VALUES ( " . implode(", ", $entry) . " );\n"; if (file_put_contents($sql_file, $export_str, FILE_APPEND) === false) { throw new Sns_Exception_Unavailable_Operation('Cannot write in ' . $sql_file); } $export_str = ''; } $offset += 1000; $query = "SELECT * FROM `" . $table . "` LIMIT " . $offset . ",1000"; //force to free memory // $data = null; // if( gc_enabled() ){ // gc_collect_cycles(); // }else{ // gc_enable(); // gc_collect_cycles(); // gc_disable(); // } $data = $wpdb->get_results($query, ARRAY_A); if ($data === false) { throw new Sns_Exception_DB_Error($query); } } if (false !== $myisam) { $export_str .= "\n --!40000 ALTER TABLE `" . $table . "` ENABLE KEYS ;"; } } if (file_put_contents($sql_file, $export_str, FILE_APPEND) === false) { throw new Sns_Exception_Unavailable_Operation('Cannot write in ' . $sql_file); } $export_str = ''; Sns_Log::log_action('Fetching table - ' . $table, SNS_LOG_END); } }
function sns_backup_external_restore($uname) { try { Sns_Log::log_msg('########PROCESS STARTED########' . PHP_EOL); $state = Sns_State::get_status(); if ($state['status'] == Sns_State::STATUS_ACTIVE) { throw new Sns_Exception_Unavailable_Operation('There is an existing active process.Please wait.'); } $stateData = array('status' => Sns_State::STATUS_ACTIVE, 'type' => Sns_State::TYPE_RESTORE, 'start_date' => date('Y-m-d H:i:s')); Sns_State::update($stateData); try { $file_dir = dirname(__FILE__) . SNS_DS . 'sns_backup-external-' . $uname . '.zip'; if (!file_exists($file_dir)) { throw new Sns_Exception_Not_Found('File not found'); } $backup_dir = substr($file_dir, 0, strlen($file_dir) - 4); Sns_Log::log_action('Restoring from external file'); Sns_History::restore_from_file($backup_dir, $file_dir); Sns_Log::log_action('Restoring from external file', SNS_LOG_END); @unlink($file_dir); Sns_Log::log_msg('[SUCCEED Restore]' . PHP_EOL); } catch (Exception $e) { Sns_Log::log_msg('[FAILED Restore]' . PHP_EOL); throw $e; } $stateData = array('status' => Sns_State::STATUS_FINISHED, 'type' => Sns_State::TYPE_RESTORE); Sns_State::update($stateData); Sns_Log::log_msg('########PROCESS ENDED########' . PHP_EOL); } catch (Exception $e) { Sns_Log::log_exception_obj($e); $ex_data = Sns_Exception_Handler::get_exception_data($e); $stateData = array('status' => Sns_State::STATUS_FAILED, 'type' => Sns_State::TYPE_RESTORE, 'msg' => $ex_data['status'] . ' : ' . $ex_data['msg']); Sns_State::update($stateData); Sns_Log::log_msg('########PROCESS ENDED########' . PHP_EOL); Sns_Log::report('restore'); wp_redirect(admin_url("admin.php?page=" . $_GET['page'])); } }