Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /***************************************************************************
- * copyright: (C) 2008 Sawyerr Ken
- * developer site: http://www.sawyerrken.net/
- * description: non-incremental mysql database backup script
- * written: Feb 2008
- ***************************************************************************/
- /***************************************************************************
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version. Although none of the code may be
- * sold. If you have been sold this script, get a refund.
- ***************************************************************************/
- //change the variables here to fit your connection settings
- $host = "";
- $username = "";
- $password = "";
- $database = "";
- //do not change anything from here
- function backup_tables($host,$user,$pass,$name,$tables = '*')
- {
- $link = mysqli_connect($host,$user,$pass);
- mysqli_select_db($link,$name);
- if($tables == '*')
- {
- $tables = array();
- $result = mysqli_query($link,'SHOW TABLES');
- while($row = mysqli_fetch_row($result))
- {
- $tables[] = $row[0];
- }
- }
- else
- {
- $tables = is_array($tables) ? $tables : explode(',',$tables);
- }
- foreach($tables as $table)
- {
- $result = mysqli_query($link,'SELECT * FROM '.$table);
- $num_fields = @mysqli_num_fields($result);
- $return.= 'DROP TABLE '.$table.';';
- $row2 = @mysqli_fetch_row(mysqli_query($link,'SHOW CREATE TABLE '.$table));
- $return.= "\n\n".$row2[1].";\n\n";
- for ($i = 0; $i < $num_fields; $i++)
- {
- while($row = mysqli_fetch_row($result))
- {
- $return.= 'INSERT INTO '.$table.' VALUES(';
- for($j=0; $j<$num_fields; $j++)
- {
- $row[$j] = addslashes($row[$j]);
- $row[$j] = ereg_replace("\n","\\n",$row[$j]);
- if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
- if ($j<($num_fields-1)) { $return.= ','; }
- }
- $return.= ");\n";
- }
- }
- $return.="\n\n\n";
- }
- $time=date("Y-m-d:H:i:s");
- $handle = fopen('../../hourly_backups/db-backup-'.$time.'-'.$name.'.sql','w+');
- fwrite($handle,$return);
- fclose($handle);
- }
- backup_tables($host,$username,$password,$database);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement