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";
Related
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?
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.
when i run the following command from the cmd box, it works fine; creates the file with my database data inside it:
"\Program Files\EasyPHP-5.3.8.1\mysql\bin\mysqldump" -u root -p databasename > D:\path\dumpedfile.sql
but then when i run this one from a php script, it makes the file, but its empty:
shell_exec('"\Program Files\EasyPHP-5.3.8.1\mysql\bin\mysqldump" -u root -p databasename > D:\path\dumpedfile.sql');
(note: i have no password setup)
what am i doing wrong?
please help
thanks
If you don't have any password on your user account, you should try without the -p argument.
shell_exec('"\Program Files\EasyPHP-5.3.8.1\mysql\bin\mysqldump" -u root databasename > dumpedfile.sql');
Try something like this:
<?php
$User = "root";
$Password = "mypass";
$DatabaseName = "bcms6"
$File = "mydump.sql";
shell_exec("fullpath\mysqldump --allow-keywords --opt -u $User -p $Password $DatabaseName > $File");
//Or this way
shell_exec('"C:\Program Files (x86)\EasyPHP-5.3.9\mysql\bin\mysqldump.exe" -u root -p database > database_dump.sql'); //second way with full path.
?>
You can try this way too:
$command = 'mysqldump -u root -ppassword database > /path/database.sql';
system($command, $output);
if($output != 0) {
echo 'Error during backup';
else {
echo 'Database dumped';
}
EDITED..
The password has to be hardcoded.
I'm using this code to backup my mysql database, and it works fine for my purposes.This script save my backup as .sql file how can I zip that sql file.Here's the code
$username = "***";
$password = "***";
$hostname = "***";
$database = "***";
$username =escapeshellcmd($username);
$password =escapeshellcmd($password);
$hostname =escapeshellcmd($hostname);
$database =escapeshellcmd($database);
$backupFile=''.date("Y-m-d-H-i-s").$database.'.sql';
$command = "mysqldump -u$username -p$password -h$hostname $database > $backupFile";
system($command, $result);
echo $result;
Just pipe the output to gzip or similar
mysqldump -uroot | gzip > backupfile.zip
http://mediakey.dk/~cc/compressing-mysqldump-output-mysql-gzip-bzip2-and-lzma-7z/
For a pure PHP solution, I believe this should do it: (from http://www.php.net/manual/en/ziparchive.addfile.php
$z = new ZipArchive();
$z->open("sqldump.zip", ZIPARCHIVE::CREATE);
$z->addFile($backupFile);
$z->close();
You could use tar: tar -cf archive.tar yourfile.sql using exec()
Just change
`$backupFile=''.date("Y-m-d-H-i-s").$database.'.sql';`
to
`$backupFile=''.date("Y-m-d-H-i-s").$database.'.zip';`
or to any other zip file type. It is a shell command and can save in many different compressed formats.
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.