9, November 2009

Webdigity tutorials : Backup all your databases with one click

 
Webdigity webmaster forums
[ Home | Help | Search | Forum's Shop | Archive | Login | Register | Webmaster Directory ]
Tutorials @ webDigity
Backup all your databases with one click

Subscribe to our tutorials feed!

This is a script usefull mostly to server admins, which will automatically detect all your mysql databases and backup/compress them.

By the way, I designed this script to use it in console(CLI). If you try to execute from web, please let me know how it works.

Code:
<?php
 
//================================================
 //
 // Auto backup your databases
 // Author: Nick Papanotas
 // Comments/feedback/troubleshooting:
 // http://www.webdigity.com/
 //
 // 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());
 echo 
'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 --default-character-set=greek --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);
  echo 
'Parsed in ' $total_time ' secs' "\nStarting with compression\n";
 }
 
//Let's tar those backups :
 
shell_exec'tar cvf '.$backup_dir.date('Y-m-d').'.tar '.$backup_dir.'*.sql' );
?>

Do you like this tutorial?
Feel free to share it with others using Dzone or your favorite bookmarking service. You can see related discussions in our php forum
This tutorial posted by Nikolas from Sublime Directory

Sponsors :

Code library menu :
[ Tutorials home | Submit a tutorial ]

User Area
Welcome, Guest. Please login or register.
Did you miss your activation email?
Nov 09, 2009, 06:43:28 am





Login with username, password and session length

Donate to our community, and get a permanent link back to your site!

Donate to our community, and get a permanent link back to your site!


Forum Statistics
Total Posts: 42.741
Total Topics: 8.375
Total Members: 6.939
Tutorials : 58
Resources : 929
Designs : 341
Latest Member: Auguste Dubuisson

22 Guests, 3 Users online :

15 users online today:




Web Design Gallery · Whois Lookup · Pagerank · Tag Browsing · Lo-fi version · Syndication · Webmaster forum history · Advertise
Developed by HumanWorks © 2005 - 2009 Webdigity webmaster community · sublime directory
Webdigity Webmaster Forums | Powered by SMF 1.0.12. © 2001-2005, Lewis Media. All Rights Reserved.