how to backup and restore mysql database (localhost) to Dropbox in PHP I try this save to my HDD Drive but how to save to online drive and retrieve from online drive and also show all saved datas using PHP
Asked
Active
Viewed 950 times
-2
-
There are several topics on dumping a MySQL database to text files using php. There also are several topics how to copy files to dropbox using php. Combine the 2. The question as it stands at the moment is very broad. – Shadow Jul 18 '16 at 09:06
1 Answers
0
First save the backup on server using the following code:
<?php
try {
backup_tables('localhost','root','','database');
}
catch(Exception $e) {
echo 'Error: ' .$e->getMessage();
}
/* backup the db OR just a table */
function backup_tables($host,$user,$pass,$name,$tables = '*')
{
$link = new mysqli($host, $user, $pass, $name);
// Check connection
if ($link->connect_error) {
throw new Exception("Connection failed: " . $link->connect_error);
}
$return='';
//get all of the tables
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);
}
//cycle through
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] = str_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";
}
//save file
$time=time();
$file_name='db-backup-'.$time.'-'.(md5(implode(',',$tables))).'.sql';
$sql="INSERT INTO `backups` (`file_name`,`time`) VALUES ('$file_name','$time')";
if(mysqli_query($link,$sql)){
$handle = fopen($file_name,'w+');
fwrite($handle,$return);
fclose($handle);
}
else
{
throw new Exception("Unable to insert into backups database!");
}
}
?>
And now use Dropbox API to save it on your dropbox. For more info visit: Dropbox API
Finally, delete the file on server using unlink()
method in php.
-
excelent answer thanks my friend. but error come like this what can we do ( ! ) Deprecated: Function ereg_replace() is deprecated in C:\wamp\www\my Pawning Project\backup_restore\backup.php on line 44 Call Stack # Time Memory Function Location 1 0.0035 263824 {main}( ) ..\backup.php:0 2 0.0036 264704 backup_tables( ) ..\backup.php:2 – Derick Chooty Jul 18 '16 at 12:00
-
line number 44 is. $row[$j] = ereg_replace("\n","\\n",$row[$j]); – Derick Chooty Jul 18 '16 at 12:05
-
-
Warning: preg_replace(): Empty regular expression in C:\wamp\www\my Pawning Project\backup_restore\backup.php on line 44 – Derick Chooty Jul 18 '16 at 16:32
-
-
every think is correct but have an error in line 30 Notice: Undefined variable: return in C:\wamp\www\my Pawning Project\backup_restore\backup.php on line 30 $return .= 'DROP TABLE '.$table.';'; – Derick Chooty Jul 18 '16 at 17:01
-
$return .= this is an assignment operation but we have to initiate the variable before assign – Derick Chooty Jul 18 '16 at 17:06
-
Oops, yes my bad. Add `$return='';` after `mysql_select_db($name,$link);` – Manikiran Jul 18 '16 at 17:35
-
-
sorry no errors but it only backup my structure without data ....... before add $return=''; shows error anyway the backup is success. after add $return=''; the backup is failed but no errors – Derick Chooty Jul 18 '16 at 17:41
-
I have updated with the final working code. Please try and let me know :) – Manikiran Jul 18 '16 at 18:00
-
Thanks boss working well. and how to show all available backups and restore date – Derick Chooty Jul 19 '16 at 01:54
-
and another thing how to show "successfully backup" message if any "error show error on backup" – Derick Chooty Jul 19 '16 at 01:56
-
-
First get the file content using `$sql = file_get_contents($file_path);` and then execute it using `mysqli_query($link,$sql)`. If you want to check if successfully executed use `if(mysqli_query($link,$sql)){/*Success*/}else{/*Fail*/}` – Manikiran Jul 19 '16 at 08:25
-
Warning: file_get_contents(db-backup-1468929842-a9c7765f16edf9e2e2eb8a865a0f9.sql): failed to open stream: No such file or directory in C:\wamp\www\my Pawning Project\backup_restore\restore.php on line 9 – Derick Chooty Jul 19 '16 at 12:05
-
-
$query = "select * from backups where time='$restore_data'"; $result = mysql_query($query); while ($row = mysql_fetch_array($result)) { $file_path =$row['file_name'].".sql"; } – Derick Chooty Jul 19 '16 at 12:05
-
I guess you are creating backup in one directory and restoring in another directory. If so, please make sure you store full path of sql file in database. As in instead of saving `db-backup-xxx-xxx.sql` save it as `http://localhost/my Pawing Project/backup/db-backup-xxx-xxx.sql` – Manikiran Jul 19 '16 at 12:12
-
no bro need to do online but now iam trying this local host but not working – Derick Chooty Jul 19 '16 at 16:32
-
Online or local testing, you need have proper idea (and security) of saving backups and loading them. So have a variable like `$base="http://example.com/backups/";` and now load normally like `$sql=file_get_contents($base.$row["file_name"]);` – Manikiran Jul 19 '16 at 16:47
-
-
i have ask new question so please help with full code [new question Link](http://stackoverflow.com/questions/38484666/how-to-restore-backup-mysql-file-in-php) – Derick Chooty Jul 20 '16 at 15:28
-
hi i tried too many restore codes not working pls help to restore backup data – Derick Chooty Jul 30 '16 at 04:54
-
Did you try http://stackoverflow.com/a/19752106/3699054. If you are still unable to solve it, then let me try solving it using teamviewer. – Manikiran Jul 30 '16 at 06:10
-
code is working but error like this message "Error performing query 'DROP TABLE advance; ': Cannot delete or update a parent row: a foreign key constraint fails" when drop advance table i choose all tables link with foreign key – Derick Chooty Aug 02 '16 at 00:35
-
"mysql_query("SET FOREIGN_KEY_CHECKS=0");" i used this before perform the query section and turn on tables imported successfully message.... now its working – Derick Chooty Aug 02 '16 at 00:48