Here is my code:
<?php
$backupfile = 'bkp_dbcreditors_' . date("Y-m-d-H-i-s") . '.sql';
$command = "C:\wamp\bin\mysql\mysql5.6.17\bin\mysqldump -u root -pare048 dbcreditors > $backupfile";
system($command);
echo "Backup taken.";
exit();
?>
Is there a way to confirm that backup is not taken, if such happens?
Read the return value of the system($command). http://php.net/manual/en/function.system.php
It returns false on failure.
Related
I'm trying to restore my database using this code and it doesn't work
$server_name = "localhost";
$username = "root";
$password = "admin";
$database_name = "uvatravelclone";
$file = basename($_POST["file_open"]);
//$cmd = "mysql -h {$server_name} -u {$username} -p{$password} {$database_name} $file";
$cmd = "mysql -e {$file}";
if (exec($cmd) == true){
echo "succes";
}
else{
echo " error";
}
i need to get the file name or file path of the data because i'm using
<input type=file>
to open the file.
When you use <input type="file" name="file_open">, the information about the uploaded file is put in $_FILES, not $_POST. The file contents are put into a temporary file, and $_FILES['file_open']['tmp_name'] contains the pathname to this temporary file. You can then redirect this to the mysql command, using < filename for shell input redirection.
$server_name = "localhost";
$username = "root";
$password = "admin";
$database_name = "uvatravelclone";
$file = $_FILES['file_open']['tmp_name']
$cmd = "mysql -h {$server_name} -u {$username} -p{$password} {$database_name} < $file";
if (exec($cmd)){
echo "success";
}
else{
echo " error";
}
Note that the return value of exec() is the last line of output of the command, not a true/false value that indicates success.
Things to be known, when you have a type=file in your HTML Form, its necessary to use method="post" and enctype="multipart/form-data"
Example
<form method="post" enctype="multipart/form-data">
<input type="file" name="filename" />
<button>Upload Now</button>
</form>
Render Using PHP
<?php
$getFile = $_FILES["filename"];
var_dump($getFile); //Prints uploaded file details
//Use copy or move_uploaded_file to store the uploaded file on server
//its always better to use move_uploaded_file for the purpose.
?>
Know more it in this link http://www.w3schools.com/php/php_file_upload.asp.
+1 to Barmar's answer, but here's another tip: exec() can fetch the exit status of the command you execute, and this is a more accurate indicator of success or failure.
Traditionally, when a command has an exit status of 0 it means success, and any nonzero value means some error.
exec($cmd, $output, $exitStatus);
if (0 == $exitStatus)){
echo "success";
}
else{
echo " error";
}
See http://php.net/manual/en/function.exec.php
I aleady have a code here and it works when I use it on the command prompt directly using this cd C:\wamp...... to point out the bin and use the following code below to dump:
<?php
DEFINE("HOST","localhost"); // Host Name
DEFINE("USER","root"); // Database Username
DEFINE("PASSWORD",""); // Database Password
DEFINE("DATABASE","db_osgamdrp"); // Database Name
DEFINE("TIMEZONE","Asia/Manila"); // Server Timezone
DEFINE("BIN","C:\wamp\bin\mysql\mysql5.6.12\bin") // MySQLi BIN
$Status = 1;
$Message = "";
$Command = "mysqldump --opt -h " . HOST . " -u " . USER . " -p " . PASSWORD . "" . DATABASE . " > ../../../../../resources/database_backup/" . DATABASE . date("Y-m-d-H-i-s") . ".gz";
if (!exec($Command)) {
$Status = 0;
$Message = "Something Went Wrong. Please Try Again";
}
?>
Whats wrong with this code? Its not working.
I have had written the following snippet that generates the mysql database dump and saves it on the server:
public function save_db_backup()
{
$DBUSER=$this->db->username;
$DBPASSWD=$this->db->password;
$DATABASE=$this->db->database;
$filename = $DATABASE . "-" . date("Y-m-d_H-i-s") . ".sql.gz";
$save_path = $_SERVER['DOCUMENT_ROOT'] . '/application/assets/db_backups/' . $filename;
$cmd = "mysqldump -u $DBUSER --password=$DBPASSWD $DATABASE | gzip --best > " . $save_path;
exec( $cmd );
}
It has been working fine on my other server. But after moving my site to this new server, it has suddenly stopped working i.e. database backup file is not being saved at the path specified. Also, I have checked exec is enabled on the server plus the directory is readable and also writeable:
is_readable($_SERVER['DOCUMENT_ROOT'] . '/application/assets/db_backups/') // true
is_writable($_SERVER['DOCUMENT_ROOT'] . '/application/assets/db_backups/') // true
I have checked and the database credentials are alright. I have tried with the path to mysqldump and that didn't work either:
$cmd = "/usr/bin/mysqldump -u $DBUSER --password=$DBPASSWD $DATABASE | gzip --best > " . $save_path;
What problem could there possibly be?
You should check what user launches the command, and then if the dir is readable and also writeable by THAT user.
If you are launching it from a PHP script, it's the probable cause.
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";
I'm trying to backup one of my databases with mysqldump and I'm trying to export it to an xml file which already exists. Here is what I have:
<?php
$dbUser = 'user'; // db User
$dbPass = 'pass'; // db User Password
$dbName = 'db'; // db name
$dest = $_SERVER['DOCUMENT_ROOT'] . 'backup'; // Path to directory
class MySQLDump {
private $cmd;
function MySQLDump($dbUser,$dbPass,$dbName,$dest) {
$fname = $dbName.'.xml';
$this->cmd='mysqldump -X -u'.$dbUser.' -p'.$dbPass.' '.$dbName.
' >'.$dest.'/'.$fname;
}
public function backup() {
system($this->cmd, $error);
if($error) {
trigger_error ('Backup failed: ' . $error . '<br />Attempted: ' . $this->cmd);
}
}
} // end class def
$mysqlDump = new MySQLDump($dbUser, $dbPass, $dbName, $dest);
$mysqlDump->backup();
?>
This always generates the error thrown in the backup function. Here is a sample of $cmd's output:
mysqldump -X -udan -pdanPass danDB >/var/www/prod/dan/web/backup/danDB.xml
Any idea what's going wrong? I've never really used mysqldump so I'm not sure if I'm approaching this correctly. Any help is greatly appreciated.
try adding a space between -u and your $dbUser.
According to Does mysqldump return a status? something is going wrong, yes. We can not say if a warning or an error because you didn't share the return code.
Apart from that you can easily learn what exactly goes wrong by looking into STDERR output returned from the mysqldump command. E.g. you can pipe it into database.err:
mysqldump -X -udan -pdanPass danDB >/path/to/backup/danDB.xml 2>database.err
^^^^^^^^^^^^^^
This one work for me
mysqldump --user=xxx --password=xxx databasename --xml > file.xml