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.
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";
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.
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
I'm very new to Linux and PHP so forgive me if this is a dumb question.
I need to create a PHP command line script that will add a user to the system, to be used by the ftp server. I have it set up at this point that it creates the account just fine. The problem I'm facing is that even though I pass through the password when I create the account ie. -p $password, I still have to run the "passwd" command on the account and set the password manually before proftpd will let that account log in.
For the life of me I can't figure out how to run the passwd command from within the php. Obviously I can run it, but I have no idea how to programatically enter the password when prompted.
Just for more info, this is the command I run when creating the account. $username and $ password are passed into the function.
shell_exec("useradd ".$username." -g ftpusers -m -p ".$password." -d
/home/ftp/".$username."/ -s /bin/false);
now im looking to add something along the lines of:
shell_exec("passwd ".$username);
echo $password;
echo $password;
Just for the record, this last example is just for illustration.
Any help appreciated.
You need to use the hashed password using useradd, e.g.
$password = escapeshellarg( crypt('password') );
shell_exec("useradd username -p $password");
-p, --password PASSWORD
The encrypted password, as returned by crypt(3). The default is
to disable the account.
(man useradd)
Can't guarantee it'll work, but on first thoughts, my solution would be:-
<?php
$fp = popen("/usr/bin/passwd " . $user, "w");
fwrite($fp, $password . "\n");
fwrite($fp, $password . "\n");
fclose($fp);
$connection = ssh2_connect('localhost', 22);
if (ssh2_auth_password($connection, 'root', '')) {
$username = '';
$password = '';
ssh2_exec($connection, "useradd $username -m -p $password -d /home/$username/ -s /bin/false");
} else {
die('Authentication Failed...');
}