May
1
2009
1
2009
Restore Mysql Database from a sql or zip file using PHP
In the previous post we saw how the backup of a database can be taken in the form of a sql file and then zipped.
In this example the same zip file would be used to restore the database from it.
Code :
#Function to restore from a file
function restore($path) {
$f = fopen('restore/temp.sql' , 'w+');
if(!$f) {
echo "Error While Restoring Database";
return;
}
$zip = new ZipArchive();
if ($zip->open($path) === TRUE) {
#Get the backup content
$sql = $zip->getFromName('backup.sql');
#Close the Zip File
$zip->close();
#Prepare the sql file
fwrite($f , $sql);
fclose($f);
#Now restore from the .sql file
$command = "mysql --user={$username} --password={$password} --database={$db} < restore/temp.sql";
exec($command);
#Delete temporary files without any warning
@unlink('restore/temp.sql');
}
else {
echo 'Failed';
}
}
Popularity: 4% [?]
Related Posts
Subscribe
Recent Posts
- Compile wxwebconnect on Ubuntu 11.04 64 bit
- Disqus Comments Importer Script in PHP
- Beginners’ guide to socket programming with winsock
- Handle multiple socket connections with fd_set and select on Linux
- Beginners guide to socket programming in C on Linux
- Gui whois client in python with wxpython
- Whois client code in C with Linux sockets
- str_replace for C
- Easy to use C/C++ IDE for Ubuntu Linux
- Get local ip in C on linux
Binarytides
Tags
apache
applications
box2d
bsnl
c
chrome
cron
css
database
dns
firefox
flash
freelance
game programming
gd
graphs
hacking
htaccess
html
html5
imagemagick
java
javascript
libpcap
linux
mod rewrite
moneybookers
mootools
mvc
mysql
networking
payment
paypal
php
phpmyadmin
python
ruby
security
Sockets
software
swing
ubuntu
winpcap
winsock
xdebug
An article by Binary Tides





Great site. The link to: ” the In the previous post we saw how the backup of a database can be taken in the form of a sql file and then zipped.”
Is broken, hope to see it soon. Thanks
Thanks for pointing that out. Its fixed now.