|
| 1 | +<?php defined("DBSYNC_EXEC") or die("dbsync not loaded correctly"); |
| 2 | +/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 3 | + Backup mySql tables |
| 4 | + ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 5 | + Copyright 2006 by Richard Williamson (aka OldGuy). |
| 6 | + Website: http://www.scripts.oldguy.us - [email protected] |
| 7 | + Support: http://www.scripts.oldguy.us/forums/ |
| 8 | + Licensed under the terms of the GNU General Public License, June 1991. |
| 9 | + ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 10 | +*/ |
| 11 | + |
| 12 | +include 'functions.php'; |
| 13 | + |
| 14 | +// ---- end of configuration settings array ----- |
| 15 | +// Don't change anything after this line unless you know what you are doing |
| 16 | + |
| 17 | +/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 18 | + Dump a database to a file and send email containing download link |
| 19 | + ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 20 | + mysqldump.php, version 1.3, Feb 2009 |
| 21 | + By Richard Williamson (aka OldGuy). |
| 22 | + Website: http://www.scripts.oldguy.us/mybackup |
| 23 | + |
| 24 | +
|
| 25 | + Licensed under the terms of the GNU General Public License, June 1991. |
| 26 | +
|
| 27 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 28 | + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 29 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 30 | + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 31 | + WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 32 | + CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 33 | + ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 34 | +*/ |
| 35 | + |
| 36 | +// are we downloading a file? |
| 37 | +/*if ($filename) { |
| 38 | + // secret code is not required, the file name is dynamically generated and thus relatively secure |
| 39 | + downloadFile($filename); |
| 40 | + exit(); |
| 41 | +}*/ |
| 42 | + |
| 43 | +if ($code != $secret) { |
| 44 | + $msg = "Invalid code ".$secret." in the query string: ?{$_SERVER['QUERY_STRING']}"; |
| 45 | + if ($debug) print "$msg<br />"; |
| 46 | + sendLog(1, $msg . EOL); |
| 47 | + exit(); |
| 48 | +} |
| 49 | + |
| 50 | +// are we downloading a bundle? |
| 51 | +if ($bundle) { |
| 52 | + sendBundle(); |
| 53 | + exit(); |
| 54 | +} |
| 55 | + |
| 56 | +if (!is_numeric($dbid) || $dbid > count($cfg) || $dbid < 0) { |
| 57 | + $msg = "Invalid or missing dbid argument in the query string: ?{$_SERVER['QUERY_STRING']}"; |
| 58 | + if ($debug) print "$msg<br />"; |
| 59 | + sendLog(1, $msg . EOL); |
| 60 | +} |
| 61 | + |
| 62 | +// Mmake sure we can write the dump file |
| 63 | +$time = date('Ymd_His'); |
| 64 | + |
| 65 | +$dumpfile = $dir . $filename /* $time . */; |
| 66 | +if ($handle = @fopen($dumpfile, 'w')) { |
| 67 | + fclose($handle); |
| 68 | +} else { |
| 69 | + $msg = "Unable to open '$dumpfile' for writing.". EOL; |
| 70 | + if ($debug) print "$msg<bmy_hw2_20111111_034837_old.sql.gzr />"; |
| 71 | + sendLog(1, $msg . EOL); |
| 72 | +} |
| 73 | + |
| 74 | + |
| 75 | +// delete old backup files for the database |
| 76 | +deleteFiles(); |
| 77 | + |
| 78 | +// Open the database |
| 79 | +$dblink = @mysql_connect($dbhost, $dbuser, $dbpass); |
| 80 | +if (!$dblink) { |
| 81 | + $msg = "Unable to connect to mysql server using the host, user and password in \$cfg[$dbid]". EOL; |
| 82 | + if ($debug) print "$msg<br />"; |
| 83 | + sendLog(1, $msg . EOL); |
| 84 | +} |
| 85 | +$result = @mysql_select_db($dbname); |
| 86 | +if (!$result) { |
| 87 | + $msg = "The database does not exist or the \$cfg[$dbid] user does not have permission to access it". EOL; |
| 88 | + if ($debug) print "$msg<br />"; |
| 89 | + sendLog(1, $msg . EOL); |
| 90 | +} |
| 91 | + |
| 92 | +// Get table names |
| 93 | +$rows = 0; |
| 94 | +$tables = array(); |
| 95 | +$result = mysql_query("SHOW TABLES FROM $dbname ", $dblink); |
| 96 | +if (!$result) { |
| 97 | + $msg = "Error doing SHOW TABLES " . mysql_error() . EOL; |
| 98 | + if ($debug) print "$msg<br />"; |
| 99 | + sendLog(1, $msg . EOL); |
| 100 | +} |
| 101 | +while (list($table_name) = mysql_fetch_row($result)) { |
| 102 | + if ($dbprefix) { |
| 103 | + if (preg_match("/^$dbprefix/", $table_name)) { |
| 104 | + $tables[$rows] = $table_name; |
| 105 | + $rows++; |
| 106 | + } |
| 107 | + } else { |
| 108 | + $tables[$rows] = $table_name; |
| 109 | + $rows++; |
| 110 | + } |
| 111 | + |
| 112 | +} |
| 113 | + |
| 114 | +// Optimize tables, build dump command tables variable |
| 115 | +$tables_list = ''; |
| 116 | +$msg = ''; |
| 117 | +for($i = 0; $i < $rows; $i++) { |
| 118 | + if ($dbprefix) $tables_list .= " {$tables[$i]}"; |
| 119 | + if ($optimize) { |
| 120 | + $result = mysql_query("OPTIMIZE TABLE {$tables[$i]}", $dblink); |
| 121 | + if (!$result) $msg .= "Optimize query failed. mysql error = " . mysql_error() . EOL . EOL; |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +if ($msg) sendLog(0, $msg . EOL); |
| 126 | +if ($debug) print "$i tables were optimized<br />"; |
| 127 | + |
| 128 | +// dump the database |
| 129 | +//$options .= ($inserts) ? '' : ' --skip-extended-insert '; |
| 130 | +//$cmd = "$command $options --user=$dbuser --password=$dbpass $dbname $tables_list | gzip > $dumpfile"; |
| 131 | +//$last_line = system($cmd, $result); |
| 132 | + |
| 133 | +mysql_close($dblink); |
| 134 | + |
| 135 | +backup_tables($dumpfile,$dbhost, $dbuser, $dbpass, $dbname); |
| 136 | + |
| 137 | +if ($debug) print "<p>completed</p>"; |
| 138 | + |
| 139 | +if ($email) sendLog(1, "Download the dump file: http://{$_SERVER['HTTP_HOST']}{$_SERVER['PHP_SELF']}?filename=$dumpfile" . EOL); |
| 140 | + |
| 141 | +?> |
0 commit comments