-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackup_database.php
More file actions
90 lines (82 loc) · 2.31 KB
/
backup_database.php
File metadata and controls
90 lines (82 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
/*
Backups a MySQL database via web in PHP
Originally by David Walsh https://davidwalsh.name/backup-mysql-database-php
Adapted and converted to PHP5
by Antonio Bonifati http://ninuzzo.github.io
*/
if (isset($_POST['name'])) {
/* backup the db OR just a table */
function backup_tables($host,$user,$pass,$name,$tables = '*')
{
$link = mysqli_connect($host,$user,$pass);
mysqli_select_db($link,$name);
//get all of the tables
if($tables == '*')
{
$tables = array();
$result = mysqli_query($link, 'SHOW TABLES');
while($row = mysqli_fetch_row($result))
{
$tables[] = $row[0];
}
}
else
{
$tables = is_array($tables) ? $tables : explode(',',$tables);
}
//cycle through
$return = '';
foreach($tables as $table)
{
$result = mysqli_query($link, 'SELECT * FROM '.$table);
$num_fields = mysqli_field_count($link);
$return.= 'DROP TABLE '.$table.';';
$row2 = mysqli_fetch_row(mysqli_query($link, 'SHOW CREATE TABLE '.$table));
$return.= "\n\n".$row2[1].";\n\n";
for ($i = 0; $i < $num_fields; $i++)
{
while($row = mysqli_fetch_row($result))
{
$return.= 'INSERT INTO '.$table.' VALUES(';
for($j=0; $j < $num_fields; $j++)
{
$row[$j] = addslashes($row[$j]);
$row[$j] = preg_replace("/\n/","\\n",$row[$j]);
if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
if ($j < ($num_fields-1)) { $return.= ','; }
}
$return.= ");\n";
}
}
$return.="\n\n\n";
}
//save file to the browser
header("Content-Type: text/plain");
header("Content-Disposition: attachment; filename=\"$name.sqldump.txt\"");
header("Content-Length: " . strlen($return));
header("Content-Transfer-Encoding: binary");
echo $return;
}
backup_tables($_POST['host'], $_POST['user'], $_POST['pass'], $_POST['name']);
} else {
?><!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Backup database</title>
</head>
<body>
<h1>Backup database</h1>
<form method="post">
<label>host: <input type="text" name="host" value="localhost"></label><br>
<label>user: <input type="text" name="user"</label><br>
<label>pass: <input type="password" name="pass"</label><br>
<label>db: <input type="text" name="name"></label><br>
<input type="submit" value="dump">
</form>
</body>
</html>
<?php
}
?>