function import_database($input_filename) { global $tabs; $input_filename = clean_input($input_filename); $root = addslashes(getcwd()); $dir = DIR_BACKUPS_PREFIX . $input_filename; if (file_exists($dir)) { if ($_POST['conf'] !== 'yes') { // Print confirmation form lcm_page_start(_T('title_archives'), '', '', 'archives_import'); show_tabs_links($tabs, 1, true); echo "<fieldset class='info_box'>\n"; show_page_subtitle(_T('generic_subtitle_warning'), 'archives_import'); echo "<p class='normal_text'><img src='images/jimmac/icon_warning.gif' alt='' " . "align='right' height='48' width='48' />" . _T('archives_info_restore_will_delete') . "</p>\n"; echo "<form action='import_db.php' method='post'>\n"; echo '<input type="hidden" name="action" value="import" />' . "\n"; echo "<button type='submit' class='simple_form_btn' name='conf' value='yes'>" . _T('info_yes') . "</button>\n"; echo "<button type='submit' class='simple_form_btn' name='conf' value='no'>" . _T('info_no') . "</button>\n"; echo "<input type='hidden' name='file' value='{$input_filename}' />\n"; echo "<input type='hidden' name='restore_type' value='" . $_POST['restore_type'] . "' />\n"; echo "</form>"; echo "</fieldset\n>"; lcm_page_end(); return; } } // Get saved database version if (!($fh = fopen("{$dir}/db-version", 'r'))) { lcm_panic("System error: Could not open file '{$dir}/db-version"); } $backup_db_version = intval(fread($fh, 10)); fclose($fh); // For debugging - use another database //lcm_query("use lcm_new"); // Recreate tables if ($_POST['restore_type'] == 'clean' || $backup_db_version < read_meta('lcm_db_version')) { // Open backup dir if (!($dh = opendir("{$dir}/"))) { lcm_panic("System error: Could not open directory '{$dir}'"); } while ($file = readdir($dh)) { // Get table name $table = substr($file, 0, -10); // Add path to filename $file = "{$dir}/{$file}"; if (strlen($file) > 10) { if (is_file($file) && substr($file, -10) === ".structure" && is_file("{$dir}/{$table}" . DATA_EXT_NAME)) { // Clear the table $q = "DROP TABLE IF EXISTS {$table}"; $result = lcm_query($q); // Create table $fh = fopen($file, 'r'); $q = fread($fh, filesize($file)); fclose($fh); $result = lcm_query_restore_table($q); } } } closedir($dh); // Update lcm_db_version // [ML] This is rather useless because they will be overwritten when the // values are loaded (LOAD FILE), but I leave it just in case there are // obscur bugs (altough this will most likely generate strange bugs..) write_meta('lcm_db_version', $backup_db_version); if (!preg_match('/^MySQL (4\\.0|3\\.)/', lcm_sql_server_info())) { write_meta('db_utf8', 'yes'); } write_metas(); } else { if ($backup_db_version > read_meta('lcm_db_version')) { // Backup version newer than installed db version lcm_page_start(_T('title_archives'), '', '', 'archives_import'); // Show tabs show_tabs_links($tabs, 1, true); // Show tab header echo "Version mismatch!\n"; // TRAD echo "<fieldset class='info_box'>\n"; echo "Backup database version is newer than the installed database."; // TRAD echo "</fieldset\n>"; lcm_page_end(); return; } else { // Backup and current db versions are equal } } // // Import data into database tables\ // // Change backup dir permissions, so MySQL could read from it. chmod($dir, 0755); // Open backup dir if (!($dh = opendir("{$dir}/"))) { lcm_panic("System error: Could not open directory '{$dir}'"); } while ($file = readdir($dh)) { // Get table name $table = substr($file, 0, -DATA_EXT_LEN); // Add path to filename $file = "{$dir}/{$file}"; if (strlen($file) > 5) { // [ML] why? if (is_file($file) && substr($file, -DATA_EXT_LEN) === DATA_EXT_NAME) { // If restore_type='clean', clear the table if ($_POST['restore_type'] == 'clean') { lcm_query("TRUNCATE TABLE {$table}"); } $q = "LOAD DATA INFILE '{$file}' "; $q .= $_POST['restore_type'] == 'replace' ? 'REPLACE' : 'IGNORE'; $q .= "\tINTO TABLE {$table}\n\t\t\t\t\tFIELDS TERMINATED BY ','\n\t\t\t\t\t\tOPTIONALLY ENCLOSED BY '\"'\n\t\t\t\t\t\tESCAPED BY '\\\\'\n\t\t\t\t\tLINES TERMINATED BY '\r\n'"; $result = lcm_query($q); } } } closedir($dh); // Change backup dir permissions back chmod($dir, 0700); // Update lcm_db_version since we have overwritten lcm_meta write_meta('lcm_db_version', $backup_db_version); if ($_REQUEST['restore_type'] == 'clean') { if (!preg_match('/^MySQL (4\\.0|3\\.)/', lcm_sql_server_info())) { write_meta('db_utf8', 'yes'); } } write_metas(); lcm_page_start(_T('title_archives'), '', '', 'archives_import'); // FIXME? show_tabs_links($tabs, 1, true); echo '<div class="sys_msg_box">' . "\n"; show_page_subtitle("Import finished", 'archives_import'); // FIXME TRAD? echo "Backup '{$input_filename}' was successfully imported into database."; // TRAD echo "</div\n>"; lcm_page_end(); }
function export_database($output_filename = '', $ignore_old = false) { global $tabs; $output_filename = clean_input($output_filename); if (!$output_filename) { $output_filename = "lcm-" . date('Ymd'); } // // Check if file exists. If exists, add a revision number to name (ex: foo-2) // $cpt = 0; while (file_exists(DIR_BACKUPS_PREFIX . $output_filename . ($cpt ? "-" . $cpt : ''))) { $cpt++; } if ($cpt) { $output_filename .= "-" . $cpt; } // // Export database // if (!mkdir(DIR_BACKUPS_PREFIX . $output_filename, 0777)) { lcm_panic("Could not create " . DIR_BACKUPS_PREFIX . $output_filename); } // Record database version $file = fopen(DIR_BACKUPS_PREFIX . $output_filename . '/db-version', 'w'); fwrite($file, read_meta('lcm_db_version')); fclose($file); // Get the list of tables in the database $q = "SHOW TABLES"; $result = lcm_query($q); while ($row = lcm_fetch_array($result)) { // Backup table structure $q = "SHOW CREATE TABLE " . $row[0]; $res = lcm_query($q); $sql = lcm_fetch_row($res); $file = fopen(DIR_BACKUPS_PREFIX . $output_filename . '/' . $row[0] . ".structure", 'w'); fwrite($file, $sql[1]); fclose($file); // Backup data $q = "SELECT * FROM " . $row[0] . "\n\t\t\t\tINTO OUTFILE '" . DIR_BACKUPS_PREFIX . $output_filename . '/' . $row[0] . DATA_EXT_NAME . "'\n\t\t\t\tFIELDS TERMINATED BY ','\n\t\t\t\t\tOPTIONALLY ENCLOSED BY '\"'\n\t\t\t\t\tESCAPED BY '\\\\'\n\t\t\t\tLINES TERMINATED BY '\r\n'"; $res = lcm_query($q, true); if (!$res) { die("<p>Configuration error: please make sure that your MySQL user\n\t\t\thas 'File_priv' = 'Y'. For example, in phpmyadmin or using the\n\t\t\tcommand line mysql tool, go to the mysql.user table, and update\n\t\t\tthe File_priv of your LCM database account. Do not forget to\n\t\t\texecute 'flush privileges' afterwards. For more information,\n\t\t\tplease refer to: <a href='http://www.lcm.ngo-bg.org/article147.html'>http://www.lcm.ngo-bg.org/article147.html</a></p>"); // TRAD } } // By default, in most installations, directory will have 0777 mode // and will be owned by the Apache process' user. chmod(DIR_BACKUPS_PREFIX . $output_filename, 0700); @(include "Archive/Tar.php"); $tar_worked = false; if (class_exists("Archive_Tar")) { $tar_worked = true; $old_cwd = getcwd(); chdir(DIR_BACKUPS); $tar_object = new Archive_Tar(FILE_PREFIX . $output_filename . '.tar'); $files = array(); $file_dir = opendir(FILE_PREFIX . $output_filename); if (!$file_dir) { lcm_panic("Could not open dir: {$file_dir}"); } while ($file = readdir($file_dir)) { if (is_file(FILE_PREFIX . $output_filename . '/' . $file)) { $files[] = FILE_PREFIX . $output_filename . '/' . $file; } } if (count($files)) { $tar_object->setErrorHandling(PEAR_ERROR_PRINT); $tar_object->create($files) or lcm_panic("Could not add files " . get_var_dump($files)); } chdir($old_cwd); } // // Finished // lcm_page_start(_T('title_archives'), '', '', 'archives_export'); show_tabs_links($tabs, 0); echo '<div class="sys_msg_box">' . "\n"; if ($tar_worked) { $name = '<a class="content_link" href="export_db.php?action=download&file=' . $output_filename . '.tar">' . $output_filename . '.tar' . '</a> (' . filesize_in_bytes(DIR_BACKUPS_PREFIX . $output_filename . '.tar') . ')'; echo _T('archives_info_new_success', array('name' => $name)); } else { echo _T('archives_info_new_success', array('name' => $output_filename)); } echo "</div>\n"; show_export_form_partial(); lcm_page_end(); }