-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexport.php
executable file
·141 lines (117 loc) · 4.5 KB
/
export.php
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php defined("DBSYNC_EXEC") or die("dbsync not loaded correctly");
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Backup mySql tables
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Copyright 2006 by Richard Williamson (aka OldGuy).
Website: http://www.scripts.oldguy.us - [email protected]
Support: http://www.scripts.oldguy.us/forums/
Licensed under the terms of the GNU General Public License, June 1991.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*/
include 'functions.php';
// ---- end of configuration settings array -----
// Don't change anything after this line unless you know what you are doing
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Dump a database to a file and send email containing download link
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
mysqldump.php, version 1.3, Feb 2009
By Richard Williamson (aka OldGuy).
Website: http://www.scripts.oldguy.us/mybackup
Email: [email protected]
Licensed under the terms of the GNU General Public License, June 1991.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*/
// are we downloading a file?
/*if ($filename) {
// secret code is not required, the file name is dynamically generated and thus relatively secure
downloadFile($filename);
exit();
}*/
if ($code != $secret) {
$msg = "Invalid code ".$secret." in the query string: ?{$_SERVER['QUERY_STRING']}";
if ($debug) print "$msg<br />";
sendLog(1, $msg . EOL);
exit();
}
// are we downloading a bundle?
if ($bundle) {
sendBundle();
exit();
}
if (!is_numeric($dbid) || $dbid > count($cfg) || $dbid < 0) {
$msg = "Invalid or missing dbid argument in the query string: ?{$_SERVER['QUERY_STRING']}";
if ($debug) print "$msg<br />";
sendLog(1, $msg . EOL);
}
// Mmake sure we can write the dump file
$time = date('Ymd_His');
$dumpfile = $dir . $filename /* $time . */;
if ($handle = @fopen($dumpfile, 'w')) {
fclose($handle);
} else {
$msg = "Unable to open '$dumpfile' for writing.". EOL;
if ($debug) print "$msg<bmy_hw2_20111111_034837_old.sql.gzr />";
sendLog(1, $msg . EOL);
}
// delete old backup files for the database
deleteFiles();
// Open the database
$dblink = @mysql_connect($dbhost, $dbuser, $dbpass);
if (!$dblink) {
$msg = "Unable to connect to mysql server using the host, user and password in \$cfg[$dbid]". EOL;
if ($debug) print "$msg<br />";
sendLog(1, $msg . EOL);
}
$result = @mysql_select_db($dbname);
if (!$result) {
$msg = "The database does not exist or the \$cfg[$dbid] user does not have permission to access it". EOL;
if ($debug) print "$msg<br />";
sendLog(1, $msg . EOL);
}
// Get table names
$rows = 0;
$tables = array();
$result = mysql_query("SHOW TABLES FROM $dbname ", $dblink);
if (!$result) {
$msg = "Error doing SHOW TABLES " . mysql_error() . EOL;
if ($debug) print "$msg<br />";
sendLog(1, $msg . EOL);
}
while (list($table_name) = mysql_fetch_row($result)) {
if ($dbprefix) {
if (preg_match("/^$dbprefix/", $table_name)) {
$tables[$rows] = $table_name;
$rows++;
}
} else {
$tables[$rows] = $table_name;
$rows++;
}
}
// Optimize tables, build dump command tables variable
$tables_list = '';
$msg = '';
for($i = 0; $i < $rows; $i++) {
if ($dbprefix) $tables_list .= " {$tables[$i]}";
if ($optimize) {
$result = mysql_query("OPTIMIZE TABLE {$tables[$i]}", $dblink);
if (!$result) $msg .= "Optimize query failed. mysql error = " . mysql_error() . EOL . EOL;
}
}
if ($msg) sendLog(0, $msg . EOL);
if ($debug) print "$i tables were optimized<br />";
// dump the database
//$options .= ($inserts) ? '' : ' --skip-extended-insert ';
//$cmd = "$command $options --user=$dbuser --password=$dbpass $dbname $tables_list | gzip > $dumpfile";
//$last_line = system($cmd, $result);
mysql_close($dblink);
backup_tables($dumpfile,$dbhost, $dbuser, $dbpass, $dbname);
if ($debug) print "<p>completed</p>";
if ($email) sendLog(1, "Download the dump file: http://{$_SERVER['HTTP_HOST']}{$_SERVER['PHP_SELF']}?filename=$dumpfile" . EOL);
?>