May
1
2009
1
2009
Backup mysql database with php and zip it
PHP can be used to backup a mysql database and make a zip file of it.
mysqldump is the utility which can be used to perform this function of backing up a mysql database as sql file. The command would be like this :
mysqldump –user={$username} –password={$password} –quick –add-drop-table –add-locks –extended-insert –lock-tables –all {$db} > backups/backup.sql
$username – mysql username
$password – mysql password
$db – name of the database to backup
In the above example backups/backup.sql is the path to the sql file. Next this backup.sql file is zipped using the ZipArchive library class of php.
Code :
#Function to backup database to a zip file
function backup()
{
$suffix = time();
#Execute the command to create backup sql file
exec("mysqldump --user={$username} --password={$password} --quick --add-drop-table --add-locks --extended-insert --lock-tables --all {$db} > backups/backup.sql");
#Now zip that file
$zip = new ZipArchive();
$filename = "backups/backup-$suffix.zip";
if ($zip->open($filename, ZIPARCHIVE::CREATE) !== TRUE) {
exit("cannot open <$filename>\n");
}
$zip->addFile("backups/backup.sql" , "backup.sql");
$zip->close();
#Now delete the .sql file without any warning
@unlink("backups/backup.sql");
#Return the path to the zip backup file
return "backups/backup-$suffix.zip";
}
Popularity: 6% [?]
Related Posts
Leave a comment
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




