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);
?>
Related
I trying to backup my database using mysqldump. The file is being generated but the file is empty. How can I fix this?
PHP Code:
$dbuser = 'demo';
$dbpass = 'demo';
$host = 'localhost';
$dbname = 'demo';
$filename = __DIR__ . '/backup/'. $dbname . '_'.$date.'.sql';
exec("mysqldump --user={$dbuser} --password={$dbpass} --host={$host} $dbname > {$filename}");
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 am trying to use PHP to backup and restore a MySQL database:
Backup:
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'dbpass';
$dbname = 'test';
$output = "D:/backup/test.sql";
exec("D:/xampp/mysql/bin/mysqldump --opt -h $dbhost -u $dbuser -p $dbpass $dbname > $output");
echo "Backup complete!";
Restore:
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'dbpass';
$dbname = 'test';
$output = "D:/restore/test.sql";
exec("D:/xampp/mysql/bin/mysql --opt -h $dbhost -u $dbuser -p $dbpass $dbname < $output");
echo "Restore complete!";
But both are not working. When Backup is complete then I check test.sql file that is blank. When the restore is complete, the database is still blank.
How can I fix this?
Script to backup using Php
<?php
define("BACKUP_PATH", "/home/abdul/");
$server_name = "localhost";
$username = "root";
$password = "root";
$database_name = "world_copy";
$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);
?>
Script to restore
<?php
$restore_file = "/home/abdul/20140306_world_copy.sql";
$server_name = "localhost";
$username = "root";
$password = "root";
$database_name = "test_world_copy";
$cmd = "mysql -h {$server_name} -u {$username} -p{$password} {$database_name} < $restore_file";
exec($cmd);
?>
I want backup my local database and import that database from live website. I'm tried something. please see below..
My Backup code
<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = 'password';
$dbname = "database";
$backupfile ='database.sql';
$backupdir = dirname(__FILE__);
$source = $backupdir.'/'.$backupfile;
system("mysqldump -h $dbhost -u $dbuser --password='$dbpass' $dbname > $backupfile");
?>
<form action="http://www.example.com/restore_database.php" method="post">
<input type="text" name="backup_file" value="<?php echo $source; ?>"/>
<input type="submit" />
</form>
my restore_database.php (example.com/restore_database.php)
<?php
$dbhost = "localhost";
$dbuser = "username";
$dbpass = 'password';
$dbname = "database";
$filename = $_POST['backup_file'];
mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to MySQL server: ' . mysql_error());
mysql_select_db($dbname) or die('Error selecting MySQL database: ' . mysql_error());
$templine = '';
$lines = file($filename);
foreach ($lines as $line) {
if (substr($line, 0, 2) == '--' || $line == '')
continue;
$templine .= $line;
if (substr(trim($line), -1, 1) == ';') {
mysql_query($templine) or print('Error performing query \'<strong>' . $templine . '\': ' . mysql_error() . '<br /><br />');
$templine = '';
}
}
?>
In my localhost , I'm successfully tested this. for my live site I think file path not detect correctly. I have no idea about this. please help me. Thanks.
assuming that the live site lay on the ip 134.122.12.109
and you are using UNIX
on your local host create these files and ~/backupdir directory
backup.sh
#!/bin/bash
mysqldump -uusername -pPassword database > ~/backupdir/backupFile.sql
restore.sh
#!/bin.bash
mysql -h 134.122.12.109 -uliveSiteUsername -pLiveSitePassword database < ~/backupdir/backupFile.sql
now you can backup your localhost by running ./backup.sh
and you can restore the live site by using ./restore.sh
hope that demonstrated the idea.
responding to your comment
let the restore_database.php be
<?php
$dbhost = "localhost";
$dbuser = "username";
$dbpass = 'password';
$dbname = "database";
$filename = $_POST['backup_file'];
system("mysql -h $dbhost -u $dbuser --password='$dbpass' $dbname < $filename");
?>
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.