Backup all your databases with one click


Bellow is a php script that I made for taking backups of my mySql databases. The script is designed to run under console(CLI) in root mode, so I don’t think that it will work for shared hosting accounts, but you can try it, or try to modify the exec commands with SUDO.

// Auto backup your databases
// Author: Nick Papanotas
// Comments/feedback/troubleshooting:
// This script propably wont run from web, except
// if you modify it to add SUDO.

$backup_dir = dirname( __FILE__ ) . ‘/backups/’;

$u = ‘User’;
$p = ‘XXXX’;
$db = ‘Adatatbase’;//This is just the name of a database just to make the query work. The script will backup all the databases that your use has access to.

$db_link = mysql_connect($h,$u,$p);
$res = mysql_db_query($db, ‘SHOW DATABASES’, $db_link) or die(‘Could not connect: ‘ . mysql_error());
‘Found ‘. mysql_num_rows($res) . ‘ databases’ . “\n”;

while ( $rec = mysql_fetch_array($res) )
//Parse time :
$time = microtime();
$time = explode(‘ ‘, $time);
$time = $time[1] + $time[0];
$start = $time;

echo $rec[0] . “\n”;
shell_exec( ‘mysqldump –result-file=’.$backup_dir.$rec[0].‘.’.date(‘Y-m-d’).‘.sql –password=’.$p.‘ ‘.$rec[0] );
//Parse time :
$time = microtime();
$time = explode(‘ ‘, $time);
$time = $time[1] + $time[0];
$finish = $time;
$total_time = round(($finish $start), 6);
‘Parsed in ‘ . $total_time . ‘ secs’ . “\nStarting with compression\n”;
//Let’s tar those backups :
shell_exec( ‘tar cvf ‘.$‘Y-m-d’).‘.tar ‘.$backup_dir.‘*.sql’ );

This script has been originally posted to webdigity’s code library.

2 responses »

  1. Pingback: Web 2.0 Announcer

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s