I want backup my local database and import that database from live website. I'm tried something. please see below..
My Backup code
<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = 'password';
$dbname = "database";
$backupfile ='database.sql';
$backupdir = dirname(__FILE__);
$source = $backupdir.'/'.$backupfile;
system("mysqldump -h $dbhost -u $dbuser --password='$dbpass' $dbname > $backupfile");
?>
<form action="http://www.example.com/restore_database.php" method="post">
<input type="text" name="backup_file" value="<?php echo $source; ?>"/>
<input type="submit" />
</form>
my restore_database.php (example.com/restore_database.php)
<?php
$dbhost = "localhost";
$dbuser = "username";
$dbpass = 'password';
$dbname = "database";
$filename = $_POST['backup_file'];
mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to MySQL server: ' . mysql_error());
mysql_select_db($dbname) or die('Error selecting MySQL database: ' . mysql_error());
$templine = '';
$lines = file($filename);
foreach ($lines as $line) {
if (substr($line, 0, 2) == '--' || $line == '')
continue;
$templine .= $line;
if (substr(trim($line), -1, 1) == ';') {
mysql_query($templine) or print('Error performing query \'<strong>' . $templine . '\': ' . mysql_error() . '<br /><br />');
$templine = '';
}
}
?>
In my localhost , I'm successfully tested this. for my live site I think file path not detect correctly. I have no idea about this. please help me. Thanks.
assuming that the live site lay on the ip 134.122.12.109
and you are using UNIX
on your local host create these files and ~/backupdir directory
backup.sh
#!/bin/bash
mysqldump -uusername -pPassword database > ~/backupdir/backupFile.sql
restore.sh
#!/bin.bash
mysql -h 134.122.12.109 -uliveSiteUsername -pLiveSitePassword database < ~/backupdir/backupFile.sql
now you can backup your localhost by running ./backup.sh
and you can restore the live site by using ./restore.sh
hope that demonstrated the idea.
responding to your comment
let the restore_database.php be
<?php
$dbhost = "localhost";
$dbuser = "username";
$dbpass = 'password';
$dbname = "database";
$filename = $_POST['backup_file'];
system("mysql -h $dbhost -u $dbuser --password='$dbpass' $dbname < $filename");
?>
Related
I trying to backup my database using mysqldump. The file is being generated but the file is empty. How can I fix this?
PHP Code:
$dbuser = 'demo';
$dbpass = 'demo';
$host = 'localhost';
$dbname = 'demo';
$filename = __DIR__ . '/backup/'. $dbname . '_'.$date.'.sql';
exec("mysqldump --user={$dbuser} --password={$dbpass} --host={$host} $dbname > {$filename}");
I have been seriously searching for an SQL code to backup my database. I use xampp as my local server and also phpmyadmin. I simply want to do something like:
<?php
$conn = mysqli_connect("localhost", "root", " ", "products");
if(!$conn){
die("Unable to connect ".mysql_error());
}else{
$backup = "BACKUP DATABASE products";
$backup_query = mysqli_query($conn, $backup)
}
?>
How do I backup my database and output it in .sql format on the local computer and possibly upload to recover a damaged database?
Thank you so much!
You can use the shell statement "mysqldump" and call it with php like:
<?php
$dbhost = 'localhost';
$dbuser = 'username';
$dbpassword = 'password';
$dbname = 'datenbankname';
$dumpfile = 'backups/' . $dbname . '_' . date("Y-m-d_H-i-s") . '.sql.gz';
passthru("mysqldump --user=$dbuser --password=$dbpassword --host=$dbhost $dbname | gzip -c > $dumpfile");
echo "-- Dump finished -- ";
echo $dumpfile;
I need one script that when I open the script.php page for example, that export me one database from MySQL to an SQL document, and I don't know how to do it in PHP, can some one help me with that?
Edited Code:
<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpwd = "";
$dbname = "activmanagement";
$dumpfile = $dbname . "_" . date("Y-m-d_H-i-s") . ".sql";
passthru("D:/wamp/bin/mysql/mysql5.6.17/bin/mysqldump --opt --host=$dbhost --user=$dbuser --password=$dbpwd $dbname > $dumpfile");
// report - disable with // if not needed
// must look like "-- Dump completed on ..."
echo "$dumpfile "; passthru("tail -1 $dumpfile");
?>
This line:
passthru("D:/wamp/bin/mysql/mysql5.6.17/bin/mysqldump --opt --host=$dbhost --user=$dbuser --password=$dbpwd $dbname > $dumpfile");
Refer PHP - exec() vs system() vs passthru(). You just need something like this
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = 'activmanagement';
$sql_backup_file = $dbname . date("Y-m-d-H-i-s") . '.gz';
$dump = "mysqldump --opt -h $dbhost -u $dbuser -p $dbpass $dbname | gzip > $sql_backup_file";
system($dump);
?>
I am trying to use PHP to backup and restore a MySQL database:
Backup:
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'dbpass';
$dbname = 'test';
$output = "D:/backup/test.sql";
exec("D:/xampp/mysql/bin/mysqldump --opt -h $dbhost -u $dbuser -p $dbpass $dbname > $output");
echo "Backup complete!";
Restore:
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'dbpass';
$dbname = 'test';
$output = "D:/restore/test.sql";
exec("D:/xampp/mysql/bin/mysql --opt -h $dbhost -u $dbuser -p $dbpass $dbname < $output");
echo "Restore complete!";
But both are not working. When Backup is complete then I check test.sql file that is blank. When the restore is complete, the database is still blank.
How can I fix this?
Script to backup using Php
<?php
define("BACKUP_PATH", "/home/abdul/");
$server_name = "localhost";
$username = "root";
$password = "root";
$database_name = "world_copy";
$date_string = date("Ymd");
$cmd = "mysqldump --routines -h {$server_name} -u {$username} -p{$password} {$database_name} > " . BACKUP_PATH . "{$date_string}_{$database_name}.sql";
exec($cmd);
?>
Script to restore
<?php
$restore_file = "/home/abdul/20140306_world_copy.sql";
$server_name = "localhost";
$username = "root";
$password = "root";
$database_name = "test_world_copy";
$cmd = "mysql -h {$server_name} -u {$username} -p{$password} {$database_name} < $restore_file";
exec($cmd);
?>
I am trying to backup a MySQL database called Reserveboxto a .gzip file. I got this script from a tutorial, and I changed the values according to my values. The problem is when I click submit nothing happens. I do not know where did I go.
<form id="form1" name="form1" method="post" action="backup.php">
<input type="submit" name="Backup" id="Backup" value="Backup" />
</form>
<p> </p>
<?php
include ("functions_cp/f_connection.php");
Sqlconnection();
$dbname = "Reservebox";
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "123";
function backup() {
$backupFile = $dbname . date("Y-m-d-H-i-s") . '.gz';
$command = "mysqldump --opt -h $dbhost -u $dbuser -p $dbpass $dbname | gzip > $backupFile";
system($command);
}
if(isset($_POST['backup'])) {
backup();
}
if(isset($_POST['backup'])) {
I think the backup on this line needs a capital B!
If that doesn't sort it, try adding error_reporting(E_ALL); on a new line after <?php.
Try removing the space between the -p and $dbpass.
Also, remove --opt. That is not strictly necessary.