John Davidson

Save the mysql database backup dump generated with ifsnop/mysqldump-php on a local folder and not on the server

0 comments
Message:


I am using for the first time ifsnop/mysqldump-php to backup a mysql database. Works fine, does the job, including blob fields (the reason I went with it).
By default it saves the dump file online as a txt\sql file in a given folder on the server.
I need to save it locally and get thesave as window where I can choose the location on my computer.


The basic script I have is:


<?php

include_once('../myfolder/mysqldump-php-master/src/Ifsnop/Mysqldump/Mysqldump.php');
$dump = new Ifsnop\Mysqldump\Mysqldump('mysql:host=myhost';dbname=myname', 'myuser', 'mypassword');
$dump->start('mybackup.sql');

?>

I tried something like the code here below, that generated a downloadable file, but I can't seem to get the dump content into the variable $content and write it into the file.


<?php

header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\""mybackup.sql"\"");
echo '$content'; exit;

?>


My Answer:
To save the MySQL database backup dump generated with ifsnop/mysqldump-php on a local folder, you can modify your script as follows:

php

include_once('../myfolder/mysqldump-php-master/src/Ifsnop/Mysqldump/Mysqldump.php');

$dump = new Ifsnop\Mysqldump\Mysqldump('mysql:host=myhost';dbname=myname', 'myuser', 'mypassword');

// Specify the local file path where you want to save the backup
$localFilePath = '/path/to/local/folder/mybackup.sql';

// Dump the database content to a variable
$content = $dump->getDump();

// Save the dump content to a local file
file_put_contents($localFilePath, $content);

echo 'Backup saved locally at: ' . $localFilePath;

?>


In this modified script, we first specify the local file path where we want to save the backup dump. Then, we use the `getDump()` method to get the dump content into the variable `$content`. Finally, we use `file_put_contents()` to write the dump content to the specified local file.

Make sure to replace `/path/to/local/folder/mybackup.sql` with the actual path where you want to save the backup dump on your local machine.

Rate this post

5 of 5 based on 1895 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us