MySQL backup doesn't work - php

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.

Related

how to properly execute mysqldump through php

After a database is successfully inserted with data, I want now to backup the database. Since this is a script done in codeigniter, I created this function below.
This is bugging me for a while. I want to execute a php script that contains command line.
Here is my code:
public function backupDb()
{
log_message('debug', 'Preparing to Back-up Database...');
$dbuser = "root";
$dbpass = "root#111";
$dbname = "listingdb";
$dumpfile = $dbname . "_" . date("Ymd") . ".sql";
$command = "mysqldump -u $dbuser -p$dbpass $dbname > $dumpfile";
exec($command);
}
I tried manually doing this code;
mysqldump -u root -proot#111 listingdb > listingdb_20171013.sql
and it Successfully creates a .sql file
But my function seems to not work. How can I do it right?

How can I export MySQL database with a PHP script?

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);
?>

PHP exec() only work on command line only

I am using PHP to backup MySQL database. But my code only works on command line (command: php index.php). If I execute it on browser, the dump file not generate. here is my code:
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$dbname = 'otoworks';
$backup_file = $dbname . date("Y-m-d-H-i-s") . '.gz';
$command = "/usr/bin/mysqldump --opt -h $dbhost -u $dbuser -p$dbpass $dbname | gzip > $backup_file";
exec($command);
?>
My php run on apache 2.2.3, centos 6. php.ini (safe_mode = Off).
Thanks in advance

PHP mysqldump file import

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");
?>

i want to export a single table from the database witout using phpmyadmin

i have only ftp credentials in which i am using this script
$r="mysqldump $dbuser $dbpass $dbname wp_posts > table1.sql";
system($r);
but unfortunatly m getting a blank result in table1.sql
You need to add the option switches to your command, see the mysqldump manual, like this
$r="mysqldump -u $dbuser -p $dbpass $dbname wp_posts > table1.sql";
finally i got the solution, here is my code:
<?php
$username = "abc";
$password = "xyz";
$hostname = "domain.com";
$database = "test_db";
$username =escapeshellcmd($username);
$password =escapeshellcmd($password);
$hostname =escapeshellcmd($hostname);
$database =escapeshellcmd($database);
$backupFile='url'.date("Y-m-d-H-i-s").$database.'.sql.tgz';
$command = "mysqldump -u$username -p$password -h$hostname $database wp_posts > $backupFile";
system($command, $result);
echo $result;
?>
thnx for your support, really appreciate it.
if the user has the permissions try this:
mysqldump -u$dbuser -p$dbpass $dbname wp_posts > table1.sql
to specify the parameters (which is the user and which is the password)
I think it's
mysqldump -u $dbuser -p $dbpass $dbname wp_posts
Apache user needs to have the permission to write table1.sql and also execute mysqldump command. If on shared hosting, this is doubtful.

Categories