Skip to content

Instantly share code, notes, and snippets.

@SahilC
Created November 28, 2012 22:26
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save SahilC/4165155 to your computer and use it in GitHub Desktop.
Save SahilC/4165155 to your computer and use it in GitHub Desktop.
Php Mysql Database backup and restore
<?php
$dump_path = ""; //input location for the backup to be saved
$host = ""; //db host e.g.- localhost
$user = ""; //user e.g.-root
$pass = ""; //password
$command=$dump_path.'mysqldump -h '.$host.' -u '.$user.' bank > bank.sql';
system($command);
?>
<?php
$dump_path = ""; //your backup location
$host = ""; //host
$user = ""; //username
$pass = ""; /passwprd
$command=$dump_path.'mysql -h '.$host.' -u '.$user.' bank < bank.sql';
system($command);
?>
@sagar03d
Copy link

sagar03d commented Apr 3, 2018

Restore not working, since 'mysql -h '.$host.' -u '.$user.' bank < bank.sql' command asking for password when entered in command prompt

@mariojrrc
Copy link

@sagar03d try this in the restore.php file.

$tempConfig = tmpfile();
fwrite($tempConfig, "[client]\nhost={$host}\nuser={$user}\npassword={$pass}");
$tempConfigPath = stream_get_meta_data($tempConfig)['uri'];
system("mysql --defaults-extra-file={$tempConfigPath} bank < bank.sql');

@alisarh
Copy link

alisarh commented Dec 13, 2018

hello ... the Backup.php returns an empty file ?? what is the problem (Testing on localhost wampserver)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment