日期:2011-03-17  浏览次数:20524 次

PHP代码:--------------------------------------------------------------------------------

<?php


require('includes/application_top.php');

if ($HTTP_GET_VARS['action']) {
switch ($HTTP_GET_VARS['action']) {
case 'forget':
tep_db_query("delete from " . TABLE_CONFIGURATION . " where configuration_key = 'DB_LAST_RESTORE'");
$messageStack->add_session(SUCCESS_LAST_RESTORE_CLEARED, 'success');
tep_redirect(tep_href_link(FILENAME_BACKUP));
break;
case 'backupnow':
tep_set_time_limit(0);
$schema = '# citespa, Open Source E-Commerce Solutions' . "\n" .
'# <a href="http://www.XXXXXX.com" target="_blank">http://www.XXXXXX.com</a>' . "\n" .
'#' . "\n" .
'# Database Backup For ' . STORE_NAME . "\n" .
'# Copyright (c) ' . date('Y') . ' ' . STORE_OWNER . "\n" .
'#' . "\n" .
'# Database: ' . DB_DATABASE . "\n" .
'# Database Server: ' . DB_SERVER . "\n" .
'#' . "\n" .
'# Backup Date: ' . date(PHP_DATE_TIME_FORMAT) . "\n\n";
$tables_query = tep_db_query('show tables');
while ($tables = tep_db_fetch_array($tables_query)) {
list(,$table) = each($tables);
$schema .= 'drop table if exists ' . $table . ';' . "\n" .
'create table ' . $table . ' (' . "\n";
$table_list = array();
$fields_query = tep_db_query("show fields from " . $table);
while ($fields = tep_db_fetch_array($fields_query)) {
$table_list[] = $fields['Field'];
$schema .= ' ' . $fields['Field'] . ' ' . $fields['Type'];
if (strlen($fields['Default']) > 0) $schema .= ' default '' . $fields['Default'] . ''';
if ($fields['Null'] != 'YES') $schema .= ' not null';
if (isset($fields['Extra'])) $schema .= ' ' . $fields['Extra'];
$schema .= ',' . "\n";
}
$schema = ereg_replace(",\n$", '', $schema);

// Add the keys
$index = array();
$keys_query = tep_db_query("show keys from " . $table);
while ($keys = tep_db_fetch_array($keys_query)) {
$kname = $keys['Key_name'];
if (!isset($index[$kname])) {
$index[$kname] = array('unique' => !$keys['Non_unique'],
'columns' => array());
}
$index[$kname]['columns'][] = $keys['Column_name'];
}
while (list($kname, $info) = each($index)) {
$schema .= ',' . "\n";
$columns = implode($info['columns'], ', ');
if ($kname == 'PRIMARY') {
$schema .= ' PRIMARY KEY (' . $columns . ')';
} elseif ($info['unique']) {
$schema .= ' UNIQUE ' . $kname . ' (' . $columns . ')';
} else {
$schema .= ' KEY ' . $kname . ' (' . $columns . ')';
}
}
$schema .= "\n" . ');' . "\n\n";

// Dump the data
$rows_query = tep_db_query("select " . implode(',', $table_list) . " from " . $table);
while ($rows = tep_db_fetch_array($rows_query)) {
$schema_insert = 'insert into ' . $table . ' (' . implode(', ', $table_list) . ') values (';
reset($table_list);
while (list(,$i) = each($ta