首页 > 开发 > PHP > 正文

一段php代码:备份、恢复sql数据库(支持sql文本,zip。。。)

2024-05-04 22:57:48
字体:
来源:转载
供稿:网友
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($table_list)) {
if (!isset($rows[$i])) {
$schema_insert .= 'null, ';
} elseif ($rows[$i] != '') {
$row = addslashes($rows[$i]);
$row = ereg_replace("/n#", "/n".'#', $row);
$schema_insert .= ''' . $row . '', ';
} else {
$schema_insert .= ''', ';
}
}
$schema_insert = ereg_replace(', $', '', $schema_insert) . ');' . "/n";
$schema .= $schema_insert;
}
$schema .= "/n";
}

if ($http_post_vars['download'] == 'yes') {
$backup_file = 'db_' . db_database . '-' . date('ymdhis') . '.sql';
switch ($http_post_vars['compress']) {
case 'no':
header('content-type: application/x-octet-stream');
header('content-disposition: attachment; filename=' . $backup_file);
echo $schema;
exit;
break;
case 'gzip':
if ($fp = fopen(dir_fs_backup . $backup_file, 'w')) {
fputs($fp, $schema);
fclose($fp);
exec(local_exe_gzip . ' ' . dir_fs_backup . $backup_file);
$backup_file .= '.gz';
}
if ($fp = fopen(dir_fs_backup . $backup_file, 'rb')) {
$buffer = fread($fp, filesize(dir_fs_backup . $backup_file));
fclose($fp);
unlink(dir_fs_backup . $backup_file);
header('content-type: application/x-octet-stream');
header('content-disposition: attachment; filename=' . $backup_file);
echo $buffer;
exit;
}
break;
case 'zip':
if ($fp = fopen(dir_fs_backup . $backup_file, 'w')) {
fputs($fp, $schema);
fclose($fp);
exec(local_exe_zip . ' -j ' . dir_fs_backup . $backup_file . '.zip ' . dir_fs_backup . $backup_file);
unlink(dir_fs_backup . $backup_file);
$backup_file .= '.zip';
}
if ($fp = fopen(dir_fs_backup . $backup_file, 'rb')) {
$buffer = fread($fp, filesize(dir_fs_backup . $backup_file));
fclose($fp);
unlink(dir_fs_backup . $backup_file);
header('content-type: application/x-octet-stream');
header('content-disposition: attachment; filename=' . $backup_file);
echo $buffer;
exit;
}
}
} else {
$backup_file = dir_fs_backup . 'db_' . db_database . '-' . date('ymdhis') . '.sql';
if ($fp = fopen($backup_file, 'w')) {
fputs($fp, $schema);
fclose($fp);
switch ($http_post_vars['compress']) {
case 'gzip':
exec(local_exe_gzip . ' ' . $backup_file);
break;
case 'zip':
exec(local_exe_zip . ' -j ' . $backup_file . '.zip ' . $backup_file);
unlink($backup_file);
}
}
$messagestack->add_session(success_database_saved, 'success');
tep_redirect(tep_href_link(filename_backup));
}
break;
case 'restorenow':
case 'restorelocalnow':
tep_set_time_limit(0);

if ($http_get_vars['action'] == 'restorenow') {
$read_from = $http_get_vars['file'];
if (file_exists(dir_fs_backup . $http_get_vars['file'])) {
$restore_file = dir_fs_backup . $http_get_vars['file'];
$extension = substr($http_get_vars['file'], -3);
if ( ($extension == 'sql') || ($extension == '.gz') || ($extension == 'zip') ) {
switch ($extension) {
case 'sql':
$restore_from = $restore_file;
$remove_raw = false;
break;
case '.gz':
$restore_from = substr($restore_file, 0, -3);
exec(local_exe_gunzip . ' ' . $restore_file . ' -c > ' . $restore_from);
$remove_raw = true;
break;
case 'zip':
$restore_from = substr($restore_file, 0, -4);
exec(local_exe_unzip . ' ' . $restore_file . ' -d ' . dir_fs_backup);
$remove_raw = true;
}

if ( ($restore_from) && (file_exists($restore_from)) && (filesize($restore_from) > 15000) ) {
$fd = fopen($restore_from, 'rb');
$restore_query = fread($fd, filesize($restore_from));
fclose($fd);
}
}
}
} elseif ($http_get_vars['action'] == 'restorelocalnow') {
if ($http_post_files['sql_file']) {
$uploaded_file = $http_post_files['sql_file']['tmp_name'];
$read_from = basename($http_post_files['sql_file']['name']);
} elseif ($http_post_vars['sql_file']) {
$uploaded_file = $http_post_vars['sql_file'];
$read_from = basename($http_post_vars['sql_file_name']);
} else {
$uploaded_file = $sql_file;
$read_from = basename($sql_file_name);
}
if ($uploaded_file != 'none') {
if (tep_is_uploaded_file($uploaded_file)) {
$restore_query = fread(fopen($uploaded_file, 'r'), filesize($uploaded_file));
}
}
}

if ($restore_query) {
$sql_array = array();
$sql_length = strlen($restore_query);
$pos = strpos($restore_query, ';');
for ($i=$pos; $i<$sql_length; $i++) {
if ($restore_query[0] == '#') {
$restore_query = ltrim(substr($restore_query, strpos($restore_query, "/n")));
$sql_length = strlen($restore_query);
$i = strpos($restore_query, ';')-1;
continue;
}
if ($restore_query[($i+1)] == "/n") {
for ($j=($i+2); $j<$sql_length; $j++) {
if (trim($restore_query[$j]) != '') {
$next = substr($restore_query, $j, 6);
if ($next[0] == '#') {
// find out where the break position is so we can remove this line (#comment line)
for ($k=$j; $k<$sql_length; $k++) {
if ($restore_query[$k] == "/n") break;
}
$query = substr($restore_query, 0, $i+1);
$restore_query = substr($restore_query, $k);
// join the query before the comment appeared, with the rest of the dump
$restore_query = $query . $restore_query;
$sql_length = strlen($restore_query);
$i = strpos($restore_query, ';')-1;
continue 2;
}
break;
}
}
if ($next == '') { // get the last insert query
$next = 'insert';
}
if ( (eregi('create', $next)) || (eregi('insert', $next)) || (eregi('drop t', $next)) ) {
$next = '';
$sql_array[] = substr($restore_query, 0, $i);
$restore_query = ltrim(substr($restore_query, $i+1));
$sql_length = strlen($restore_query);
$i = strpos($restore_query, ';')-1;
}
}
}

$tables_query = tep_db_query('show tables');
while ($tables = tep_db_fetch_array($tables_query)) {
list(,$table) = each($tables);
tep_db_query("drop table " . $table);
}

for ($i=0; $i<sizeof($sql_array); $i++) {
tep_db_query($sql_array[$i]);
}

tep_db_query("delete from " . table_configuration . " where configuration_key = 'db_last_restore'");
tep_db_query("insert into " . table_configuration . " values ('', 'last database restore', 'db_last_restore', '" . $read_from . "', 'last database restore file', '6', '', '', now(), '', '')");

if ($remove_raw) {
unlink($restore_from);
}
}

$messagestack->add_session(success_database_restored, 'success');
tep_redirect(tep_href_link(filename_backup));
break;
case 'download':
$extension = substr($http_get_vars['file'], -3);
if ( ($extension == 'zip') || ($extension == '.gz') || ($extension == 'sql') ) {
if ($fp = fopen(dir_fs_backup . $http_get_vars['file'], 'rb')) {
$buffer = fread($fp, filesize(dir_fs_backup . $http_get_vars['file']));
fclose($fp);
header('content-type: application/x-octet-stream');
header('content-disposition: attachment; filename=' . $http_get_vars['file']);
echo $buffer;
exit;
}
} else {
$messagestack->add(error_download_link_not_acceptable, 'error');
}
break;
case 'deleteconfirm':
if (strstr($http_get_vars['file'], '..')) tep_redirect(tep_href_link(filename_backup));

tep_remove(dir_fs_backup . '/' . $http_get_vars['file']);
if (!$tep_remove_error) {
$messagestack->add_session(success_backup_deleted, 'success');
tep_redirect(tep_href_link(filename_backup));
}
break;
}
}

// check if the backup directory exists
$dir_ok = false;
if (is_dir(dir_fs_backup)) {
$dir_ok = true;
if (!is_writeable(dir_fs_backup)) $messagestack->add(error_backup_directory_not_writeable, 'error');
} else {
$messagestack->add(error_backup_directory_does_not_exist, 'error');
}
?>

<html <?php echo html_params; ?>>
<head>
<meta http-equiv="content-type" content="text/html; charset=<?php echo charset; ?>">
<title><?php echo title; ?></title>
<link rel="stylesheet" type="text/css" href="includes/stylesheet.css">
</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="#ffffff">
<!-- header //-->
<?php require(dir_ws_includes . 'header.php'); ?>
<!-- header_eof //-->

<!-- body //-->
<table border="0" width="100%" cellspacing="2" cellpadding="2">
<tr>
<td width="<?php echo box_width; ?>" valign="top"><table border="0" width="<?php echo box_width; ?>" cellspacing="1" cellpadding="1" class="columnleft">
<!-- left_navigation //-->
<?php require(dir_ws_includes . 'column_left.php'); ?>
<!-- left_navigation_eof //-->
</table></td>
<!-- body_text //-->
<td width="100%" valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="2">
<tr>
<td><table border="0" width="100%" cellspacing="0" cellpadding="0">
<tr>
<td class="pageheading"><?php echo heading_title; ?></td>
<td class="pageheading" align="right"><?php echo tep_draw_separator('pixel_trans.gif', heading_image_width, heading_image_height); ?></td>
</tr>
</table></td>
</tr>
<tr>
<td><table border="0" width="100%" cellspacing="0" cellpadding="0">
<tr>
<td valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="2">
<tr class="datatableheadingrow">
<td class="datatableheadingcontent"><?php echo table_heading_title; ?></td>
<td class="datatableheadingcontent" align="center"><?php echo table_heading_file_date; ?></td>
<td class="datatableheadingcontent" align="right"><?php echo table_heading_file_size; ?></td>
<td class="datatableheadingcontent" align="right"><?php echo table_heading_action; ?>&nbsp;</td>
</tr>
<?php
if ($dir_ok) {
$dir = dir(dir_fs_backup);
$contents = array();
while ($file = $dir->read()) {
if (!is_dir(dir_fs_backup . $file)) {
$contents[] = $file;
}
}
sort($contents);

for ($files=0; $files<sizeof($contents); $files++) {
$entry = $contents[$files];

$check = 0;

if (((!$http_get_vars['file']) || ($http_get_vars['file'] == $entry)) && (!$buinfo) && ($http_get_vars['action'] != 'backup') && ($http_get_vars['action'] != 'restorelocal')) {
$file_array['file'] = $entry;
$file_array['date'] = date(php_date_time_format, filemtime(dir_fs_backup . $entry));
$file_array['size'] = number_format(filesize(dir_fs_backup . $entry)) . ' bytes';
switch (substr($entry, -3)) {
case 'zip': $file_array['compression'] = 'zip'; break;
case '.gz': $file_array['compression'] = 'gzip'; break;
default: $file_array['compression'] = text_no_extension; break;
}

$buinfo = new objectinfo($file_array);
}

if (is_object($buinfo) && ($entry == $buinfo->file)) {
echo ' <tr class="datatablerowselected" onmouseover="this.style.cursor='hand'">' . "/n";
$onclick_link = 'file=' . $buinfo->file . '&action=restore';
} else {
echo ' <tr class="datatablerow" onmouseover="this.classname='datatablerowover';this.style.cursor='hand'" onmouseout="this.classname='datatablerow'">' . "/n";
$onclick_link = 'file=' . $entry;
}
?>
<td class="datatablecontent" onclick="document.location.href='<?php echo tep_href_link(filename_backup, $onclick_link); ?>'"><?php echo '<a href="' . tep_href_link(filename_backup, 'action=download&file=' . $entry) . '">' . tep_image(dir_ws_icons . 'file_download.gif', icon_file_download) . '</a>&nbsp;' . $entry; ?></td>
<td class="datatablecontent" align="center" onclick="document.location.href='<?php echo tep_href_link(filename_backup, $onclick_link); ?>'"><?php echo date(php_date_time_format, filemtime(dir_fs_backup . $entry)); ?></td>
<td class="datatablecontent" align="right" onclick="document.location.href='<?php echo tep_href_link(filename_backup, $onclick_link); ?>'"><?php echo number_format(filesize(dir_fs_backup . $entry)); ?> bytes</td>
<td class="datatablecontent" align="right"><?php if ( (is_object($buinfo)) && ($entry == $buinfo->file) ) { echo tep_image(dir_ws_images . 'icon_arrow_right.gif', ''); } else { echo '<a href="' . tep_href_link(filename_backup, 'file=' . $entry) . '">' . tep_image(dir_ws_images . 'icon_info.gif', image_icon_info) . '</a>'; } ?>&nbsp;</td>
</tr>
<?php
}
$dir->close();
}
?>
<tr>
<td class="smalltext" colspan="3"><?php echo text_backup_directory . ' ' . dir_fs_backup; ?></td>
<td align="right" class="smalltext"><?php if ( ($http_get_vars['action'] != 'backup') && ($dir) ) echo '<a href="' . tep_href_link(filename_backup, 'action=backup') . '">' . tep_image_button('button_backup.gif', image_backup) . '</a>'; if ( ($http_get_vars['action'] != 'restorelocal') && ($dir) ) echo '&nbsp;&nbsp;<a href="' . tep_href_link(filename_backup, 'action=restorelocal') . '">' . tep_image_button('button_restore.gif', image_restore) . '</a>'; ?></td>
</tr>
<?php
if (defined('db_last_restore')) {
?>
<tr>
<td class="smalltext" colspan="4"><?php echo text_last_restoration . ' ' . db_last_restore . ' <a href="' . tep_href_link(filename_backup, 'action=forget') . '">' . text_forget . '</a>'; ?></td>
</tr>
<?php
}
?>
</table></td>
<?php
$heading = array();
$contents = array();
switch ($http_get_vars['action']) {
case 'backup':
$heading[] = array('text' => '<b>' . text_info_heading_new_backup . '</b>');

$contents = array('form' => tep_draw_form('backup', filename_backup, 'action=backupnow'));
$contents[] = array('text' => text_info_new_backup);

if ($messagestack->size > 0) {
$contents[] = array('text' => '<br>' . tep_draw_radio_field('compress', 'no', true) . ' ' . text_info_use_no_compression);
$contents[] = array('text' => '<br>' . tep_draw_radio_field('download', 'yes', true) . ' ' . text_info_download_only . '*<br><br>*' . text_info_best_through_https);
} else {
$contents[] = array('text' => '<br>' . tep_draw_radio_field('compress', 'gzip', true) . ' ' . text_info_use_gzip);
$contents[] = array('text' => tep_draw_radio_field('compress', 'zip') . ' ' . text_info_use_zip);
$contents[] = array('text' => tep_draw_radio_field('compress', 'no') . ' ' . text_info_use_no_compression);
$contents[] = array('text' => '<br>' . tep_draw_checkbox_field('download', 'yes') . ' ' . text_info_download_only . '*<br><br>*' . text_info_best_through_https);
}

$contents[] = array('align' => 'center', 'text' => '<br>' . tep_image_submit('button_backup.gif', image_backup) . '&nbsp;<a href="' . tep_href_link(filename_backup) . '">' . tep_image_button('button_cancel.gif', image_cancel) . '</a>');
break;
case 'restore':
$heading[] = array('text' => '<b>' . $buinfo->date . '</b>');

$contents[] = array('text' => tep_break_string(sprintf(text_info_restore, dir_fs_backup . (($buinfo->compression != text_no_extension) ? substr($buinfo->file, 0, strrpos($buinfo->file, '.')) : $buinfo->file), ($buinfo->compression != text_no_extension) ? text_info_unpack : ''), 35, ' '));
$contents[] = array('align' => 'center', 'text' => '<br><a href="' . tep_href_link(filename_backup, 'file=' . $buinfo->file . '&action=restorenow') . '">' . tep_image_button('button_restore.gif', image_restore) . '</a>&nbsp;<a href="' . tep_href_link(filename_backup, 'file=' . $buinfo->file) . '">' . tep_image_button('button_cancel.gif', image_cancel) . '</a>');
break;
case 'restorelocal':
$heading[] = array('text' => '<b>' . text_info_heading_restore_local . '</b>');

$contents = array('form' => tep_draw_form('restore', filename_backup, 'action=restorelocalnow', 'post', 'enctype="multipart/form-data"'));
$contents[] = array('text' => text_info_restore_local . '<br><br>' . text_info_best_through_https);
$contents[] = array('text' => '<br>' . tep_draw_file_field('sql_file'));
$contents[] = array('text' => text_info_restore_local_raw_file);
$contents[] = array('align' => 'center', 'text' => '<br>' . tep_image_submit('button_restore.gif', image_restore) . '&nbsp;<a href="' . tep_href_link(filename_backup) . '">' . tep_image_button('button_cancel.gif', image_cancel) . '</a>');
break;
case 'delete':
$heading[] = array('text' => '<b>' . $buinfo->date . '</b>');

$contents = array('form' => tep_draw_form('delete', filename_backup, 'file=' . $buinfo->file . '&action=deleteconfirm'));
$contents[] = array('text' => text_delete_intro);
$contents[] = array('text' => '<br><b>' . $buinfo->file . '</b>');
$contents[] = array('align' => 'center', 'text' => '<br>' . tep_image_submit('button_delete.gif', image_delete) . ' <a href="' . tep_href_link(filename_backup, 'file=' . $buinfo->file) . '">' . tep_image_button('button_cancel.gif', image_cancel) . '</a>');
break;
default:
if (is_object($buinfo)) {
$heading[] = array('text' => '<b>' . $buinfo->date . '</b>');

$contents[] = array('align' => 'center', 'text' => '<a href="' . tep_href_link(filename_backup, 'file=' . $buinfo->file . '&action=restore') . '">' . tep_image_button('button_restore.gif', image_restore) . '</a> <a href="' . tep_href_link(filename_backup, 'file=' . $buinfo->file . '&action=delete') . '">' . tep_image_button('button_delete.gif', image_delete) . '</a>');
$contents[] = array('text' => '<br>' . text_info_date . ' ' . $buinfo->date);
$contents[] = array('text' => text_info_size . ' ' . $buinfo->size);
$contents[] = array('text' => '<br>' . text_info_compression . ' ' . $buinfo->compression);
}
break;
}

if ( (tep_not_null($heading)) && (tep_not_null($contents)) ) {
echo ' <td width="25%" valign="top">' . "/n";

$box = new box;
echo $box->infobox($heading, $contents);

echo ' </td>' . "/n";
}
?>
</tr>
</table></td>
</tr>
</table></td>
<!-- body_text_eof //-->
</tr>
</table>
<!-- body_eof //-->

<!-- footer //-->
<?php require(dir_ws_includes . 'footer.php'); ?>
<!-- footer_eof //-->
<br>
</body>
</html>
<?php require(dir_ws_includes . 'application_bottom.php'); ?>

商业源码热门下载www.html.org.cn

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表