getting 0 kb after backup the sql - php

Why when i backup my sql the size is 0 kb what should i do ? or did i miss something? . and what is the code for recovering the sql. Thank you in advance
define("BACKUP_PATH", "D:\xampp/patient.sql");
$server_name = "localhost";
$username = "root";
$password = "7831365";
$database_name = "patient";
$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);

I think you should define your BACKUP_PATH like this:
define("BACKUP_PATH", "D:\xampp\patient_");

try the following:escape characters
define("BACKUP_PATH", "D:\\xampp\\patient.sql");
check the response:
exec($cmd,$response);
print_r($response); //<-- might see the problem

Related

mysqldump in .php file

What's wrong with the exec() function
<?php
exec('mysqldump --host=localhost --user=root --password="" testing > C:/Users/deleo/Desktop/newfile.sql');
?>
when I run this code it's nothing happen but only white screen
I expected the output .sql file with my database(testing) table structure and data itself
When I did this
`
<?php
define("BACKUP_PATH", "C:/Users/deleo/Desktop/");
$server_name = "localhost";
$username = "root";
$password = "";
$database_name = "testing";
$date_string = date("Ymd");
$cmd = "mysqldump --hex-blob --routines --skip-lock-tables --log-error=mysqldump_error.log -h {$server_name} -u {$username} -p{$password} {$database_name} > " . BACKUP_PATH . "{$date_string}_{$database_name}.sql";
$arr_out = array();
unset($return);
exec($cmd, $arr_out, $return);
if($return !== 0) {
echo "mysqldump for {$server_name} : {$database_name} failed with a return code of {$return}\n\n";
echo "Error message was:\n";
$file = escapeshellarg("mysqldump_error.log");
$message = `tail -n 1 $file`;
echo "- $message\n\n";
}
?>
`
I got this error
mysqldump for localhost : tapsihannibhey failed with a return code of 1 Error message was: -

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

Restore mysql dump for specific table via PHP

I've got a mysql table dump file (which can be viewed here: http://pastebin.com/raw.php?i=GQkjrDNz) that I would like to use to create a table (named php_blog_archive as per the dump file) using the contents of that sql file.
The problem is I don't have access to phpmyadmin, so I can only execute this via php command, I've looked at a few threads like this and the code I have thus far is,
<?php
// Config
$db_user = "username";
$db_pass = "password";
exec("mysql -u $db_user -p $db_pass -h localhost databasename < restoreold.sql ");
?>
But it doesn't work, it simply does nothing - doesn't even show any errors. Could someone please advise me as to how to proceed?
First of all the command must be:
<?php
// Config
$db_user = "username";
$db_pass = "password";
exec("mysql -u " . $db_user . " -p " . $db_pass . " -h localhost databasename < restoreold.sql "); // Added an space before -h
?>
And second, you've right access to create/edit/append the file and exec command?
I think you have the good old problem wit the password.
<?php
// Config
$db_user = "username";
$db_pass = "password";
exec("mysql -u " . $db_user . " -p'" . $db_pass . "' -h localhost databasename < restoreold.sql 2>&1", $output);
echo nl2br($output);
?>
This will end up in mysql -u username -p'password' -h localhost databasename < restoreold.sql 2>&1
The last thing is to echo the error masseges or something else to HTML. This is done via 2>&1 and the $output reference variable which will be convertet from nl (new lines will end up with <br />) to HTML and echoed.
Hope it helps and works.

Mysqldump error

if($_POST['submit'])
{
$dbhost = 'localhost';
$DBUSER = 'dbuser';
$DBPASSWD = 'dbpass';
$DATABASE = 'db';
$backupPath = '/home/*****/public_html/admin/sql';
$filename = "backup-" . date("d-m-Y");
$cmd = "mysqldump --complete-insert --create-options --add-locks --disable-keys --extended-insert --quick --quote-names -u $user --password=$DBPASSWD $DATABASE|gzip --fast -c >{$backupPath}/$filename.sql.gz";
exec($cmd);
echo $cmd;
}
This is the code i want to use for dumping database. But i never get any backup to specified directory, it always show the error which i have mentioned on my code. Ca anyone tell me whats the problem? Thank you.
N.B
Update this one -u $user with this --user $DBUSER $user but nothing happened.
Here is the sol-
$filename = $backupPath . '/DB_' . time(). '.gz';
if (file_exists($filename))
{
unlink($filename);
}
$cmd = 'mysqldump --opt -h '.$dbhost.' -u ' . $DBUSER . ' -p\'' . $DBPASSWD. '\' ' . $DATABASE . ' | gzip > ' . $filename;
exec($cmd);
echo $cmd;
Thank you all for your help.

MysqlDump Return a Empty database backup file

I use this code for download a backup of MySql database but it download a empty file.
Please also guide for restore this backup.
<?php
//connect to database
include'connect.php';
$backupFile = $dbname . date("Y-m-d-H-i-s") . '.sql';
$command = "mysqldump -h$hostname -u$username -p$password $dbname > $backupFile";
system($command);
?>
Replace system with echo to show the generated command, and execute it on the command line manually. Errors are usually shown on STDERR, which isn't caught by the system call, and if you get an empty output that means it couldn't output anything. Fix the error and then fix your code. I'd also use passthru instead of system.
To restore the backup afterwards use (from the commandline):
mysql -u<user> -p <database> < myfile.sql
Your variables are sticked to the options. Try to change this:
$command = "mysqldump -h$hostname -u$username -p$password $dbname > $backupFile";
To this:
$command = "mysqldump -h $hostname -u $username -p $password $dbname > $backupFile";

Categories