Escolar Documentos
Profissional Documentos
Cultura Documentos
php
/*
* MySQL Database Backup / Restore Tool
*
* Copyright (C) 2003 Mark Wraith. All rights reserved
*
* Graphics and layout derived from those made by Matt Mecham
*
*/
// If you intend to keep the script
// on your server set this password
$password = '0';
$data = base64_encode(serialize($options));
$header = <<<DATA
-- SQL Dump
-- Backup script written by Mark Wraith
DATA;
if (!$fp = fopen($filename, 'wb'))
{
return $this->error('Unable to write to backup file. Please CHMo
d the current directory so it is writable');
}
fwrite($fp,$header);
fclose($fp);
$url = 'mysql_tool.php?act=do_backup&file='.urlencode($filename).'&data=
'.$data;
$this->meta = '<meta http-equiv="refresh" content="1; url='.$url.'">';
$this->output = <<<HTML
<table align="center" class="tablewrap" cellpadding="0" cellspacing="3" wi
dth="350">
<tr>
<td align="center" class="title">Backup in progress...</td>
</tr>
<tr>
<td>
<table class="table1" align="center" width="100%">
<tr>
<td class="tdrow2" colspan="2">
<div align="center">The backup process has now started<br />
<br /><a href="$url">Click here if you are not redirected</a></div>
</td>
</tr>
</table>
</td>
</tr>
</table>
HTML;
}
function do_backup() {
global $HTTP_GET_VARS;
$this->read_db_details();
$this->connect();
$data = unserialize(base64_decode($HTTP_GET_VARS['data']));
$filename = $HTTP_GET_VARS['file'];
$timedout = 0;
$dump = '';
foreach ($data as $table => $line)
{
if (!$this->timeout())
{
$returned = $this->backup_table($table, $line);
if (is_array($returned))
{
$timedout = 1;
$dump .= $returned[0];
$data[ $table ] = $returned[1];
}
else
{
$dump .= $returned;
unset($data[ $table ]);
}
}
else
{
$timedout = 1;
}
}
if (!$fp = fopen($filename, 'ab'))
{
return $this->error('Unable to write to backup file. Please CHMo
d the current directory so it is writable');
}
fwrite($fp,$dump);
fclose($fp);
if ($timedout)
{
$data = base64_encode(serialize($data));
$url = 'mysql_tool.php?act=do_backup&file='.urlencode($filename).'&d
ata='.$data;
$this->meta = '<meta http-equiv="refresh" content="1; url='.$url.'">
';
$this->output = <<<HTML
<table align="center" class="tablewrap" cellpadding="0" cellspacing="3" wi
dth="350">
<tr>
<td align="center" class="title">Backup in progress...</td>
</tr>
<tr>
<td>
<table class="table1" align="center" width="100%">
<tr>
<td class="tdrow2">
<div align="center">The backup process is in progress<br /><
br /><a href="$url">Click here if you are not redirected</a></div>
</td>
</tr>
</table>
</td>
</tr>
</table>
HTML;
}
else
{
$this->output = <<<HTML
<table align="center" class="tablewrap" cellpadding="0" cellspacing="3" wi
dth="350">
<tr>
<td align="center" class="title">Backup Completed</td>
</tr>
<tr>
<td>
<table class="table1" align="center" width="100%">
<tr>
<td class="tdrow2">
The backup progress has finished and the file has been writt
en to "$filename".<br /><br />
<a href="$filename">Click here to download the file</a>
</td>
</tr>
</table>
</td>
</tr>
</table>
HTML;
}
}
function backup_table($table,$start) {
$dump = '';
if (!$start)
{
$result = mysql_query('SHOW FIELDS FROM '.$table);
while ($field = mysql_fetch_assoc($result))
{
if (!$field['Null'])
{
$null = ' NOT NULL default "'.$field['Default'].'"';
}
else
{
$null = '';
}
if ($field['Extra'])
{
$field['Extra'] = ' '.$field['Extra'];
}
$field_row[] = ' ' . $field['Field'] . ' ' . $field['Type'] . $
null . $field['Extra'];
}
$result = mysql_query('SHOW KEYS FROM '.$table);
while ($key = mysql_fetch_assoc($result))
{
if ($key['Key_name'] == 'PRIMARY')
{
$primary_key = $key['Column_name'];
}
else
{
$unique[ $key['Key_name'] ][] = $key['Column_name'];
}
}
if (isset($primary_key))
{
$field_row[] = ' PRIMARY KEY (' . $primary_key . ')';
}
if (isset($unique))
{
foreach ($unique as $name => $keys)
{
$field_row[] = ' UNIQUE ' . $name . ' (' . implode(',',$key
s) . ')';
}
}
$dump .= "\n\n--\n";
$dump .= "-- Table structure for table '$table'\n";
$dump .= "--\n\n";
$dump .= "CREATE TABLE $table (\n";
$dump .= implode(",\n",$field_row);
$dump .= "\n);\n\n";
$dump .= "\n\n--\n";
$dump .= "-- Dumping data for table '$table'\n";
$dump .= "--\n\n";
}
//
// Records
//
$done = 0;
$result = mysql_query('SELECT * FROM '.$table.' LIMIT '.$start.',-1');
while ($row = mysql_fetch_row($result))
{
if ($this->timeout())
{
return array($dump,$done);
}
$done++;
foreach ($row as $id => $value)
{
$value = str_replace('"','\\"',$value);
$row[$id] = '"'.$value.'"';
}
$dump .= 'INSERT INTO ' . $table . ' VALUES (' . implode(',',$ro
w) . ");\n";
}
return $dump;
}
function main() {
if (!$this->link)
{
if (!$this->read_db_details())
{
return $this->set_database();
}
if ($error_text = $this->connect(1))
{
return $this->set_database($error_text);
}
}
$tables_to_backup = '';
if ($this->db['prefix'])
{
$tables_to_backup .= '<input type="radio" name="tables" value="p
refix" checked="checked" />IPB Tables Only <br />';
$tables_to_backup .= '<input type="radio" name="tables" value="a
ll" />All<br />';
}
else
{
$tables_to_backup .= '<input type="radio" name="tables" value="a
ll" checked="checked" />All<br />';
}
$tables = mysql_list_tables($this->db['name']);
$options = '';
while (list($table_name) = mysql_fetch_array($tables))
{
$options .= '<option value="'.$table_name.'">'.$table_name.'</option>
';
}
$tables_to_backup .= <<<HTML
<input type="radio" name="tables" value="selected" />Selected tables:<br />
<div style="margin-left: 40px">
<select name="table_select[]" class="textbox" size="5" style="width: 250px"
multiple="multiple">
$options
</select>
</div>
HTML;
$options = '';
if ($dir = @opendir('./'))
{
while ($file = readdir($dir))
{
$temp = strtolower($file);
if ($file != '.' && $file != '..' && strpos($temp, '.sql'))
{
$options .= '<option value="'.$file.'">'.$file.'</option>';
}
}
closedir($dir);
}
$restore_files = '<select name="filename" class="textbox">'.$options.'</
select>';
$restore_files .= '<br /><br /><u>or</u> path:<br /><br /><input type="t
ext" name="relfilename" class="textbox" />';
$this->output = <<<HTML
<form method="post" action="mysql_tool.php?act=login">
<table align="center" class="tablewrap" cellpadding="0" cellspacing="3" wi
dth="450">
<tr>
<td align="center" class="title">Selected Database Details</td>
</tr>
<tr>
<td>
<table class="table1" align="center" width="100%">
<tr>
<td width="150" class="tdrow1"><strong>Host / Port</strong></td>
<td width="300" class="tdrow2">{$this->db['host']}:{$this->db['p
ort']}</td>
</tr>
<tr>
<td class="tdrow1"><strong>Database Name</strong></td>
<td class="tdrow2">{$this->db['name']}</td>
</tr>
<tr>
<td class="tdrow1"><strong>Username</strong></td>
<td class="tdrow2">{$this->db['user']}</td>
</tr>
<tr>
<td class="tdrow2" colspan="2"><div align="center">[ <a href="my
sql_tool.php?act=change_db">Change Database</a> ]</div></td>
</tr>
</table>
</td>
</tr>
</table>
</form>
<br /><br />
<form method="post" action="mysql_tool.php?act=backup">
<table align="center" class="tablewrap" cellpadding="0" cellspacing="3" wi
dth="450">
<tr>
<td align="center" class="title">Backup Options</td>
</tr>
<tr>
<td>
<table class="table1" align="center" width="100%">
<tr>
<td width="150" class="tdrow1" valign="top"><strong>Tables to ba
ckup:</strong></td>
<td width="300" class="tdrow2">$tables_to_backup</td>
</tr>
<tr>
<td class="tdrow1"><strong>Filename</strong></td>
<td class="tdrow2"><input type="text" name="filename" class="tex
tbox" value="sql_backup.sql"></td>
</tr>
<tr>
<td class="tdrow2" colspan="2"><div align="center"><input type="
submit" value="Backup"></div></td>
</tr>
</table>
</td>
</tr>
</table>
</form>
<br /><br />
<form method="post" action="mysql_tool.php?act=restore">
<table align="center" class="tablewrap" cellpadding="0" cellspacing="3" wi
dth="450">
<tr>
<td align="center" class="title">Restore Options</td>
</tr>
<tr>
<td>
<table class="table1" align="center" width="100%">
<tr>
<td width="150" class="tdrow1" valign="top"><strong>SQL File to
restore:</strong></td>
<td width="300" class="tdrow2">$restore_files</td>
</tr>
<tr>
<td class="tdrow2" colspan="2"><div align="center"><input type="
submit" value="Restore"></div></td>
</tr>
</table>
</td>
</tr>
</table>
</form>
HTML;
}
function restore() {
global $HTTP_POST_VARS;
$this->read_db_details();
$filename = $HTTP_POST_VARS['filename'];
$relfilename = $HTTP_POST_VARS['relfilename'];
if ($relfilename)
{
$filename = $relfilename;
}
$url = 'mysql_tool.php?act=do_restore&filename='.urlencode($filename);
$this->output = <<<HTML
<table align="center" class="tablewrap" cellpadding="0" cellspacing="3" wi
dth="350">
<tr>
<td align="center" class="title">Confirm Restoration</td>
</tr>
<tr>
<td>
<table class="table1" align="center" width="100%">
<tr>
<td class="tdrow2">
<strong>Are you sure you want to restore the SQL file?</stro
ng><br /><br />
<a href="$url">Click here to restore "$filename" to "{$this-
>db['name']}"</a>
</td>
</tr>
</table>
</td>
</tr>
</table>
HTML;
}
function do_restore() {
global $HTTP_GET_VARS;
$filename = $HTTP_GET_VARS['filename'];
$this->read_db_details();
$this->connect();
$filesize = filesize($filename);
$file_position = isset($HTTP_GET_VARS['pos']) ? $HTTP_GET_VARS['pos'] :
0;
$errors = isset($HTTP_GET_VARS['ignore_errors']) ? 0 : 1;
if (!$fp = fopen($filename,'rb'))
{
return $this->error('Unable to open file "'.$filename.'"');
}
$buffer = '';
$inside_quote = 0;
$quote_inside = '';
$started_query = 0;
$data_buffer = '';
$last_char = "\n";
// Sets file position indicator
fseek($fp,$file_position);
while ((!feof($fp) || strlen($buffer)) && !$this->timeout())
{
do
{
// Deals with the length of the buffer
if (!strlen($buffer))
{
$buffer .= fread ($fp,1024);
}
// Fiddle around with the buffers
$current_char = $buffer[0];
$buffer = substr($buffer, 1);
if ($started_query)
{
$data_buffer .= $current_char;
}
elseif (preg_match("/[A-Za-z]/i",$current_char) && $last_char ==
"\n")
{
$started_query = 1;
$data_buffer = $current_char;
}
else
{
$last_char = $current_char;
}
} while (!$started_query && (!feof($fp) || strlen($buffer)));
$data_buffer = '';
$last_char = "\n";
$started_query = 0;
}
$last_char = $current_char;
}
?>