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.
Related
I'm trying to write a PHP code that will backup my SQL database daily. However, running it using a Cron Job spits out the above error in the title. My server is hosted on hostgator and its a shared server.
The dbuser has all privileges granted.
Here's my code:
$dbhost = 'localhost';
$dbuser = 'XXXX';
$dbpass = 'XXXX';
$backupFile = $dbname . date("Y-m-d-H-i-s") . '.gz';
$command = "mysqldump --opt -h $dbhost -u $dbuser -p $dbpass $dbname | gzip > $backupFile";
system($command);
Any help would be much appreciated!
The following code worked for me:
$DBUSER="XXX";
$DBPASSWD="XXXX";
$DATABASE="XXXXX";
$filename = "backup-" . date("d-m-Y") . ".sql.gz";
$cmd = "mysqldump -u $DBUSER --password=$DBPASSWD $DATABASE | gzip --best > $filename";
passthru( $cmd );
exit(0);
your problem is the space between "-p" and $dbpass. The command mysqldump hopes "-ppassword" together (the response is "using password: no" for this).
Try:
$command = "mysqldump --opt -h $dbhost -u $dbuser -p$dbpass $dbname | gzip > $backupFile";
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 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
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 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.