annotate forum/Sources/DumpDatabase.php @ 85:6d7b61434be7 website

Add a copy of this here, just in case!
author Chris Cannam
date Mon, 20 Jan 2014 11:02:36 +0000
parents e3e11437ecea
children
rev   line source
Chris@76 1 <?php
Chris@76 2
Chris@76 3 /**
Chris@76 4 * Simple Machines Forum (SMF)
Chris@76 5 *
Chris@76 6 * @package SMF
Chris@76 7 * @author Simple Machines http://www.simplemachines.org
Chris@76 8 * @copyright 2011 Simple Machines
Chris@76 9 * @license http://www.simplemachines.org/about/smf/license.php BSD
Chris@76 10 *
Chris@76 11 * @version 2.0
Chris@76 12 */
Chris@76 13
Chris@76 14 if (!defined('SMF'))
Chris@76 15 die('Hacking attempt...');
Chris@76 16
Chris@76 17 /*
Chris@76 18 This file has a single job - database backup.
Chris@76 19
Chris@76 20 void DumpDatabase2()
Chris@76 21 - writes all of the database to standard output.
Chris@76 22 - uses gzip compression if compress is set in the URL/post data.
Chris@76 23 - may possibly time out in some cases.
Chris@76 24 - the data dumped depends on whether "struct" and "data" are passed.
Chris@76 25 - requires an administrator and the session hash by post.
Chris@76 26 - is called from ManageMaintenance.php.
Chris@76 27
Chris@76 28 */
Chris@76 29
Chris@76 30 // Dumps the database to a file.
Chris@76 31 function DumpDatabase2()
Chris@76 32 {
Chris@76 33 global $db_name, $scripturl, $context, $modSettings, $crlf, $smcFunc, $db_prefix;
Chris@76 34
Chris@76 35 // Administrators only!
Chris@76 36 if (!allowedTo('admin_forum'))
Chris@76 37 fatal_lang_error('no_dump_database', 'critical');
Chris@76 38
Chris@76 39 // You can't dump nothing!
Chris@76 40 if (!isset($_REQUEST['struct']) && !isset($_REQUEST['data']))
Chris@76 41 $_REQUEST['data'] = true;
Chris@76 42
Chris@76 43 checkSession('post');
Chris@76 44
Chris@76 45 // We will need this, badly!
Chris@76 46 db_extend();
Chris@76 47
Chris@76 48 // Attempt to stop from dying...
Chris@76 49 @set_time_limit(600);
Chris@76 50 if (@ini_get('memory_limit') < 256)
Chris@76 51 @ini_set('memory_limit', '256M');
Chris@76 52
Chris@76 53 // Start saving the output... (don't do it otherwise for memory reasons.)
Chris@76 54 if (isset($_REQUEST['compress']) && function_exists('gzencode'))
Chris@76 55 {
Chris@76 56 // Make sure we're gzipping output, but then say we're not in the header ^_^.
Chris@76 57 if (empty($modSettings['enableCompressedOutput']))
Chris@76 58 @ob_start('ob_gzhandler');
Chris@76 59 // Try to clean any data already outputted.
Chris@76 60 elseif (ob_get_length() != 0)
Chris@76 61 {
Chris@76 62 ob_end_clean();
Chris@76 63 @ob_start('ob_gzhandler');
Chris@76 64 }
Chris@76 65
Chris@76 66 // Send faked headers so it will just save the compressed output as a gzip.
Chris@76 67 header('Content-Type: application/x-gzip');
Chris@76 68 header('Accept-Ranges: bytes');
Chris@76 69 header('Content-Encoding: none');
Chris@76 70
Chris@76 71 // Gecko browsers... don't like this. (Mozilla, Firefox, etc.)
Chris@76 72 if (!$context['browser']['is_gecko'])
Chris@76 73 header('Content-Transfer-Encoding: binary');
Chris@76 74
Chris@76 75 // The file extension will include .gz...
Chris@76 76 $extension = '.sql.gz';
Chris@76 77 }
Chris@76 78 else
Chris@76 79 {
Chris@76 80 // Get rid of the gzipping alreading being done.
Chris@76 81 if (!empty($modSettings['enableCompressedOutput']))
Chris@76 82 @ob_end_clean();
Chris@76 83 // If we can, clean anything already sent from the output buffer...
Chris@76 84 elseif (function_exists('ob_clean') && ob_get_length() != 0)
Chris@76 85 ob_clean();
Chris@76 86
Chris@76 87 // Tell the client to save this file, even though it's text.
Chris@76 88 header('Content-Type: ' . ($context['browser']['is_ie'] || $context['browser']['is_opera'] ? 'application/octetstream' : 'application/octet-stream'));
Chris@76 89 header('Content-Encoding: none');
Chris@76 90
Chris@76 91 // This time the extension should just be .sql.
Chris@76 92 $extension = '.sql';
Chris@76 93 }
Chris@76 94
Chris@76 95 // This should turn off the session URL parser.
Chris@76 96 $scripturl = '';
Chris@76 97
Chris@76 98 // If this database is flat file and has a handler function pass it to that.
Chris@76 99 if (!empty($smcFunc['db_get_backup']))
Chris@76 100 {
Chris@76 101 $smcFunc['db_get_backup']();
Chris@76 102 exit;
Chris@76 103 }
Chris@76 104
Chris@76 105 // Send the proper headers to let them download this file.
Chris@76 106 header('Content-Disposition: filename="' . $db_name . '-' . (empty($_REQUEST['struct']) ? 'data' : (empty($_REQUEST['data']) ? 'structure' : 'complete')) . '_' . strftime('%Y-%m-%d') . $extension . '"');
Chris@76 107 header('Cache-Control: private');
Chris@76 108 header('Connection: close');
Chris@76 109
Chris@76 110 // This makes things simpler when using it so very very often.
Chris@76 111 $crlf = "\r\n";
Chris@76 112
Chris@76 113 // SQL Dump Header.
Chris@76 114 echo
Chris@76 115 '-- ==========================================================', $crlf,
Chris@76 116 '--', $crlf,
Chris@76 117 '-- Database dump of tables in `', $db_name, '`', $crlf,
Chris@76 118 '-- ', timeformat(time(), false), $crlf,
Chris@76 119 '--', $crlf,
Chris@76 120 '-- ==========================================================', $crlf,
Chris@76 121 $crlf;
Chris@76 122
Chris@76 123 // Get all tables in the database....
Chris@76 124 if (preg_match('~^`(.+?)`\.(.+?)$~', $db_prefix, $match) != 0)
Chris@76 125 {
Chris@76 126 $db = strtr($match[1], array('`' => ''));
Chris@76 127 $dbp = str_replace('_', '\_', $match[2]);
Chris@76 128 }
Chris@76 129 else
Chris@76 130 {
Chris@76 131 $db = false;
Chris@76 132 $dbp = $db_prefix;
Chris@76 133 }
Chris@76 134
Chris@76 135 // Dump each table.
Chris@76 136 $tables = $smcFunc['db_list_tables'](false, $db_prefix . '%');
Chris@76 137 foreach ($tables as $tableName)
Chris@76 138 {
Chris@76 139 if (function_exists('apache_reset_timeout'))
Chris@76 140 @apache_reset_timeout();
Chris@76 141
Chris@76 142 // Are we dumping the structures?
Chris@76 143 if (isset($_REQUEST['struct']))
Chris@76 144 {
Chris@76 145 echo
Chris@76 146 $crlf,
Chris@76 147 '--', $crlf,
Chris@76 148 '-- Table structure for table `', $tableName, '`', $crlf,
Chris@76 149 '--', $crlf,
Chris@76 150 $crlf,
Chris@76 151 $smcFunc['db_table_sql']($tableName), ';', $crlf;
Chris@76 152 }
Chris@76 153
Chris@76 154 // How about the data?
Chris@76 155 if (!isset($_REQUEST['data']) || substr($tableName, -10) == 'log_errors')
Chris@76 156 continue;
Chris@76 157
Chris@76 158 // Are there any rows in this table?
Chris@76 159 $get_rows = $smcFunc['db_insert_sql']($tableName);
Chris@76 160
Chris@76 161 // No rows to get - skip it.
Chris@76 162 if (empty($get_rows))
Chris@76 163 continue;
Chris@76 164
Chris@76 165 echo
Chris@76 166 $crlf,
Chris@76 167 '--', $crlf,
Chris@76 168 '-- Dumping data in `', $tableName, '`', $crlf,
Chris@76 169 '--', $crlf,
Chris@76 170 $crlf,
Chris@76 171 $get_rows,
Chris@76 172 '-- --------------------------------------------------------', $crlf;
Chris@76 173 }
Chris@76 174
Chris@76 175 echo
Chris@76 176 $crlf,
Chris@76 177 '-- Done', $crlf;
Chris@76 178
Chris@76 179 exit;
Chris@76 180 }
Chris@76 181
Chris@76 182 ?>