Project

General

Profile

1
<?php
2

    
3
// $Id: backup-sql.php 310 2006-02-19 05:31:10Z ryan $
4

    
5
/*
6

    
7
 Website Baker Project <http://www.websitebaker.org/>
8
 Copyright (C) 2004-2006, Ryan Djurovich
9

    
10
 Website Baker is free software; you can redistribute it and/or modify
11
 it under the terms of the GNU General Public License as published by
12
 the Free Software Foundation; either version 2 of the License, or
13
 (at your option) any later version.
14

    
15
 Website Baker is distributed in the hope that it will be useful,
16
 but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 GNU General Public License for more details.
19

    
20
 You should have received a copy of the GNU General Public License
21
 along with Website Baker; if not, write to the Free Software
22
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23

    
24
*/
25

    
26
// Filename to use
27
$filename = $_SERVER['HTTP_HOST'].'-backup-'.gmdate('Y-m-d', mktime()+TIMEZONE).'.sql';
28

    
29
// Check if user clicked on the backup button
30
if(!isset($_POST['backup'])){ 
31
	header('Location: ../');
32
	exit(0);
33
}
34

    
35
// Include config
36
require_once('../../config.php');
37

    
38
// Create new admin object
39
require(WB_PATH.'/framework/class.admin.php');
40
$admin = new admin('Settings', 'settings_advanced', false);
41

    
42
// Begin output var
43
$output = "".
44
"#\n".
45
"# Website Baker ".WB_VERSION." Database Backup\n".
46
"# ".WB_URL."\n".
47
"# ".gmdate(DATE_FORMAT, mktime()+TIMEZONE).", ".gmdate(TIME_FORMAT, mktime()+TIMEZONE)."\n".
48
"#".
49
"\n";
50

    
51
// Get table names
52
$result = $database->query("SHOW TABLE STATUS");
53

    
54
// Loop through tables
55
while($row = $result->fetchRow()) { 
56
	//show sql query to rebuild the query
57
	$sql = 'SHOW CREATE TABLE '.$row['Name'].''; 
58
	$query2 = $database->query($sql); 
59
	// Start creating sql-backup
60
	$sql_backup ="\r\n# Create table ".$row['Name']."\r\n\r\n";
61
	$out = $query2->fetchRow();
62
	$sql_backup.=$out['Create Table'].";\r\n\r\n"; 
63
	$sql_backup.="# Dump data for ".$row['Name']."\r\n\r\n";
64
	// Select everything
65
	$out = $database->query('SELECT * FROM '.$row['Name']); 
66
	$sql_code = '';
67
	// Loop through all collumns
68
	while($code = $out->fetchRow()) { 
69
		$sql_code .= "INSERT INTO ".$row['Name']." SET "; 
70
		$numeral = 0;
71
		foreach($code as $insert => $value) {
72
			// Loosing the numerals in array -> mysql_fetch_array($result, MYSQL_ASSOC) WB hasn't? 
73
			if($numeral==1) {
74
				$sql_code.=$insert ."='".addslashes($value)."',";
75
			}
76
			$numeral = 1 - $numeral;
77
		}
78
		$sql_code = substr($sql_code, 0, -1);
79
		$sql_code.= ";\r\n";
80
	}
81
	$output .= $sql_backup.$sql_code; 
82
}
83

    
84
// Output file
85
header('Content-Type: text/html');
86
header('Content-Disposition: attachment; filename='.$filename);
87
echo $output;
88

    
89
?>
(1-1/4)