The source of mysql_backup.php (click to demo the file) viewed 503 times.
If I wrote this code, then it is licensed under the GPL. If someone else wrote it, then please ask them if you want to use the code.
<?php
//////////////////////////////////////////////////////
// Con MySQL Database Backup v1.0 //
// By sebflipper Copyright 2005 //
// http://www.sebflipper.com/ //
// //
// This script backs up a list of databases //
// or all databases can be used as a ConJob //
// //
// WinRAR required for viewing file on Windows //
// //
// You can read the dump file back into the //
// server like this: //
// shell> mysql db_name < backup-file.sql //
// //
//////////////////////////////////////////////////////
// Config Area
// MySQL connection details
$settings[server] = 'localhost';
$settings[db_user] = '';
$settings[db_pass] = '';
// Remove existing bz2 files in the folder? (Unix/Linux only)
$settings[remove_existing] = true; // true or false
// Enter a list of MySQL databases you want to backup
// Usage:
// $settings[dbs] = "all";
// $settings[dbs] = array("database1", "database2");
$settings[dbs] = "all";
// You don't need to edit anything else below this line
// Connect to database
$db = mysql_connect($settings[server], $settings[db_user], $settings[db_pass]) or die("Unable to connect to database on '".$settings[server]."' with user '".$settings[db_user]."'<br />"."Please check your settings in this file");
// Get todays date
$date = date("dmY_Hi");
// Remove existing backups
if ($settings[remove_existing] == true)
{
exec("rm *.bz2");
}
// Backup all or specified databases
if ($settings[dbs] == "all")
{
// Backup all databases
echo "Backing up: <b>All Databases</b>...<br>";
exec("mysqldump --opt --host=\"".$settings[server]."\" --user=\"".$settings[db_user]."\" --password=\"".$settings[db_pass]."\" --all-databases | bzip2 -c > full_backup_".$date.".sql.bz2");
}
else
{
// Start database loop
foreach ($settings[dbs] as $database)
{
// Change database
mysql_select_db($database,$db) or die("'".$database."' database does not exist"); ;
echo "Backing up: <b>".$database."</b>...<br>";
// Backup database
exec("mysqldump --opt --host=\"".$settings[server]."\" --user=\"".$settings[db_user]."\" --password=\"".$settings[db_pass]."\" $database | bzip2 -c > ".$database."_backup_".$date.".sql.bz2");
}
}
// Completed
echo "<br>MySQL Database Backup Complete";
?>
If you want to have a look at the source code, chose a file from this list:
To colour code your own PHP paste it here: