Revision 1224
Added by Luisehahne almost 16 years ago
| backup-sql.php | ||
|---|---|---|
| 1 | 
    <?php  | 
|
| 2 | 
     | 
|
| 3 | 
    // $Id$  | 
|
| 4 | 
     | 
|
| 5 | 
    /*  | 
|
| 6 | 
     | 
|
| 7 | 
    Website Baker Project <http://www.websitebaker.org/>  | 
|
| 8 | 
    Copyright (C) 2004-2009, 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', time()+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 | 
    // Begin output var  | 
|
| 39 | 
    $output = "".  | 
|
| 40 | 
    "#\n".  | 
|
| 41 | 
    "# Website Baker ".WB_VERSION." Database Backup\n".  | 
|
| 42 | 
    "# ".WB_URL."\n".  | 
|
| 43 | 
    "# ".gmdate(DATE_FORMAT, time()+TIMEZONE).", ".gmdate(TIME_FORMAT, time()+TIMEZONE)."\n".  | 
|
| 44 | 
    "#".  | 
|
| 45 | 
    "\n";  | 
|
| 46 | 
     | 
|
| 47 | 
    // Get table names  | 
|
| 48 | 
    // Use this one for ALL tables in DB  | 
|
| 49 | 
    $query = "SHOW TABLES";  | 
|
| 50 | 
     | 
|
| 51 | 
    if ($_POST['tables']=='WB') {
   | 
|
| 52 | 
    // Or use this to get ONLY wb tables  | 
|
| 53 | 
    	$prefix=str_replace('_','\_',TABLE_PREFIX);
   | 
|
| 54 | 
    $query = "SHOW TABLES LIKE '".$prefix."%'";  | 
|
| 55 | 
    }  | 
|
| 56 | 
     | 
|
| 57 | 
    $result = $database->query($query);  | 
|
| 58 | 
     | 
|
| 59 | 
    // Loop through tables  | 
|
| 60 | 
    while($row = $result->fetchRow()) { 
   | 
|
| 61 | 
    //show sql query to rebuild the query  | 
|
| 62 | 
    $sql = 'SHOW CREATE TABLE '.$row[0].'';  | 
|
| 63 | 
    $query2 = $database->query($sql);  | 
|
| 64 | 
    // Start creating sql-backup  | 
|
| 65 | 
    $sql_backup ="\r\n# Create table ".$row[0]."\r\n\r\n";  | 
|
| 66 | 
    $out = $query2->fetchRow();  | 
|
| 67 | 
    $sql_backup.=$out['Create Table'].";\r\n\r\n";  | 
|
| 68 | 
    $sql_backup.="# Dump data for ".$row[0]."\r\n\r\n";  | 
|
| 69 | 
    // Select everything  | 
|
| 70 | 
    	$out = $database->query('SELECT * FROM '.$row[0]); 
   | 
|
| 71 | 
    $sql_code = '';  | 
|
| 72 | 
    // Loop through all collumns  | 
|
| 73 | 
    	while($code = $out->fetchRow()) { 
   | 
|
| 74 | 
    $sql_code .= "INSERT INTO ".$row[0]." SET ";  | 
|
| 75 | 
    $numeral = 0;  | 
|
| 76 | 
    		foreach($code as $insert => $value) {
   | 
|
| 77 | 
    // Loosing the numerals in array -> mysql_fetch_array($result, MYSQL_ASSOC) WB hasn't?  | 
|
| 78 | 
    			if($numeral==1) {
   | 
|
| 79 | 
    $sql_code.=$insert ."='".addslashes($value)."',";  | 
|
| 80 | 
    }  | 
|
| 81 | 
    $numeral = 1 - $numeral;  | 
|
| 82 | 
    }  | 
|
| 83 | 
    $sql_code = substr($sql_code, 0, -1);  | 
|
| 84 | 
    $sql_code.= ";\r\n";  | 
|
| 85 | 
    }  | 
|
| 86 | 
    $output .= $sql_backup.$sql_code;  | 
|
| 87 | 
    }  | 
|
| 88 | 
     | 
|
| 89 | 
    // Output file  | 
|
| 90 | 
    header('Content-Type: text/html');
   | 
|
| 91 | 
    header('Content-Disposition: attachment; filename='.$filename);
   | 
|
| 92 | 
    echo $output;  | 
|
| 93 | 
     | 
|
| 1 | 
    <?php  | 
|
| 2 | 
    /****************************************************************************  | 
|
| 3 | 
    * SVN Version information:  | 
|
| 4 | 
    *  | 
|
| 5 | 
    * $Id$  | 
|
| 6 | 
    *  | 
|
| 7 | 
    *****************************************************************************  | 
|
| 8 | 
    *  | 
|
| 9 | 
    *****************************************************************************  | 
|
| 10 | 
    * WebsiteBaker  | 
|
| 11 | 
    *  | 
|
| 12 | 
    * WebsiteBaker Project <http://www.websitebaker2.org/>  | 
|
| 13 | 
    * Copyright (C) 2009, Website Baker Org. e.V.  | 
|
| 14 | 
    * http://start.websitebaker2.org/impressum-datenschutz.php  | 
|
| 15 | 
    * Copyright (C) 2004-2009, Ryan Djurovich  | 
|
| 16 | 
    *  | 
|
| 17 | 
    * About WebsiteBaker  | 
|
| 18 | 
    *  | 
|
| 19 | 
    * Website Baker is a PHP-based Content Management System (CMS)  | 
|
| 20 | 
    * designed with one goal in mind: to enable its users to produce websites  | 
|
| 21 | 
    * with ease.  | 
|
| 22 | 
    *  | 
|
| 23 | 
    *****************************************************************************  | 
|
| 24 | 
    *  | 
|
| 25 | 
    *****************************************************************************  | 
|
| 26 | 
    * WebsiteBaker Extra Information (where needed)  | 
|
| 27 | 
    *  | 
|
| 28 | 
    *  | 
|
| 29 | 
    *  | 
|
| 30 | 
    *****************************************************************************  | 
|
| 31 | 
    *  | 
|
| 32 | 
    *****************************************************************************  | 
|
| 33 | 
    * LICENSE INFORMATION  | 
|
| 34 | 
    *  | 
|
| 35 | 
    * WebsiteBaker is free software; you can redistribute it and/or  | 
|
| 36 | 
    * modify it under the terms of the GNU General Public License  | 
|
| 37 | 
    * as published by the Free Software Foundation; either version 2  | 
|
| 38 | 
    * of the License, or (at your option) any later version.  | 
|
| 39 | 
    *  | 
|
| 40 | 
    * WebsiteBaker is distributed in the hope that it will be useful,  | 
|
| 41 | 
    * but WITHOUT ANY WARRANTY; without even the implied warranty of  | 
|
| 42 | 
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  | 
|
| 43 | 
    * See the GNU General Public License for more details.  | 
|
| 44 | 
    *  | 
|
| 45 | 
    * You should have received a copy of the GNU General Public License  | 
|
| 46 | 
    * along with this program; if not, write to the Free Software  | 
|
| 47 | 
    * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  | 
|
| 48 | 
    *****************************************************************************/  | 
|
| 49 | 
    /**  | 
|
| 50 | 
    *  | 
|
| 51 | 
    * @category modules  | 
|
| 52 | 
    * @package backup  | 
|
| 53 | 
    * @author Ryan Djurovich  | 
|
| 54 | 
    * @copyright 2004-2009, Ryan Djurovich  | 
|
| 55 | 
    * @copyright 2009, Website Baker Org. e.V.  | 
|
| 56 | 
    * @version $Id$  | 
|
| 57 | 
    * @platform WebsiteBaker 2.8.x  | 
|
| 58 | 
    * @requirements >= PHP 4.3.4  | 
|
| 59 | 
    * @license http://www.gnu.org/licenses/gpl.html  | 
|
| 60 | 
    *  | 
|
| 61 | 
    *  | 
|
| 62 | 
    */  | 
|
| 63 | 
     | 
|
| 64 | 
    // Filename to use  | 
|
| 65 | 
    $filename = $_SERVER['HTTP_HOST'].'-backup-'.gmdate('Y-m-d', time()+TIMEZONE).'.sql';
   | 
|
| 66 | 
     | 
|
| 67 | 
    // Check if user clicked on the backup button  | 
|
| 68 | 
    if(!isset($_POST['backup'])){
   | 
|
| 69 | 
    	header('Location: ../');
   | 
|
| 70 | 
    exit(0);  | 
|
| 71 | 
    }  | 
|
| 72 | 
     | 
|
| 73 | 
    // Include config  | 
|
| 74 | 
    require_once('../../config.php');
   | 
|
| 75 | 
     | 
|
| 76 | 
    // Begin output var  | 
|
| 77 | 
    $output = "".  | 
|
| 78 | 
    "#\n".  | 
|
| 79 | 
    "# Website Baker ".WB_VERSION." Database Backup\n".  | 
|
| 80 | 
    "# ".WB_URL."\n".  | 
|
| 81 | 
    "# ".gmdate(DATE_FORMAT, time()+TIMEZONE).", ".gmdate(TIME_FORMAT, time()+TIMEZONE)."\n".  | 
|
| 82 | 
    "#".  | 
|
| 83 | 
    "\n";  | 
|
| 84 | 
     | 
|
| 85 | 
    // Get table names  | 
|
| 86 | 
    // Use this one for ALL tables in DB  | 
|
| 87 | 
    $query = "SHOW TABLES";  | 
|
| 88 | 
     | 
|
| 89 | 
    if ($_POST['tables']=='WB') {
   | 
|
| 90 | 
    // Or use this to get ONLY wb tables  | 
|
| 91 | 
    	$prefix=str_replace('_','\_',TABLE_PREFIX);
   | 
|
| 92 | 
    $query = "SHOW TABLES LIKE '".$prefix."%'";  | 
|
| 93 | 
    }  | 
|
| 94 | 
     | 
|
| 95 | 
    $result = $database->query($query);  | 
|
| 96 | 
     | 
|
| 97 | 
    // Loop through tables  | 
|
| 98 | 
    while($row = $result->fetchRow()) {
   | 
|
| 99 | 
    //show sql query to rebuild the query  | 
|
| 100 | 
    $sql = 'SHOW CREATE TABLE '.$row[0].'';  | 
|
| 101 | 
    $query2 = $database->query($sql);  | 
|
| 102 | 
    // Start creating sql-backup  | 
|
| 103 | 
    $sql_backup ="\r\n# Create table ".$row[0]."\r\n\r\n";  | 
|
| 104 | 
    $out = $query2->fetchRow();  | 
|
| 105 | 
    $sql_backup.=$out['Create Table'].";\r\n\r\n";  | 
|
| 106 | 
    $sql_backup.="# Dump data for ".$row[0]."\r\n\r\n";  | 
|
| 107 | 
    // Select everything  | 
|
| 108 | 
    	$out = $database->query('SELECT * FROM '.$row[0]); 
   | 
|
| 109 | 
    $sql_code = '';  | 
|
| 110 | 
    // Loop through all collumns  | 
|
| 111 | 
    	while($code = $out->fetchRow()) { 
   | 
|
| 112 | 
    $sql_code .= "INSERT INTO ".$row[0]." SET ";  | 
|
| 113 | 
    $numeral = 0;  | 
|
| 114 | 
    		foreach($code as $insert => $value) {
   | 
|
| 115 | 
    // Loosing the numerals in array -> mysql_fetch_array($result, MYSQL_ASSOC) WB hasn't?  | 
|
| 116 | 
    			if($numeral==1) {
   | 
|
| 117 | 
    $sql_code.=$insert ."='".addslashes($value)."',";  | 
|
| 118 | 
    }  | 
|
| 119 | 
    $numeral = 1 - $numeral;  | 
|
| 120 | 
    }  | 
|
| 121 | 
    $sql_code = substr($sql_code, 0, -1);  | 
|
| 122 | 
    $sql_code.= ";\r\n";  | 
|
| 123 | 
    }  | 
|
| 124 | 
    $output .= $sql_backup.$sql_code;  | 
|
| 125 | 
    }  | 
|
| 126 | 
     | 
|
| 127 | 
    // Output file  | 
|
| 128 | 
    header('Content-Type: text/html');
   | 
|
| 129 | 
    header('Content-Disposition: attachment; filename='.$filename);
   | 
|
| 130 | 
    echo $output;  | 
|
| 131 | 
     | 
|
| 94 | 132 | 
    ?>  | 
Also available in: Unified diff
update header