Subscribe to Tutorial Feed

Flash and PHP Bible

The Flash and PHP Bible has been released! The book can be found on Amazon or wherever fine books are sold in your area.

The Flash and PHP Bible has a forum for quick support.

Scriptplayground » tutorials » php » Backup Database

Backup Database

In this tutorial you will learn how to create a backup script for your database. This script cannot be copy and pasted onto your site, it needs to be edited and you need to gather the information regarding your server.

<?php

#!/usr/bin/php
require 'connectfile.php';
$backupdir = '/home/username/public_html/yourname/dbBackups';     
$today = getdate(); 
$month = $today[mon]; 
if ($month < 10)
	{ 
	$month = "0$month"; 
	} 
$day = $today[mday]; 
if ($day < 10)
	{ 
	$day = "0$day"; 
	}
$year = $today[year]; 
$run_command = sprintf("mysqldump --opt -h %s -u %s -p%s %s | 
gzip > %s/%s-%s-%s-%s.gz", $host, $user, $pass, $db, $backupdir, 
$db, $month, $day, $year);
system($run_command);

?>


Example One: #!/usr/bin/php
Example Two: #!/usr/local/bin/php

This is the path to PHP on your server. It may be Example One or Example Two! When all else fails ask your hosting provider.



require 'connectfile.php';

require or include the database connection file.



$backupdir = '/home/username/public_html/yourname/dbBackups';     

This is the server path to the directory you will want the backup files to be saved. All paths differ from servers, edit it to whatever path structure your server uses.



$today = getdate(); 

Attach the function getdate to the variable name $today.



$month = $today[mon]; 

Execute the function "getdate" and return the current month.



if ($month < 10)
	{ 
	$month = "0$month"; 
	} 

Since the file will be easier to view in a directory we will add a zero to single digits. This if statement basically says that any digit less than 10 add a zero to it.



$day = $today[mday]; 
if ($day < 10)
	{ 
	$day = "0$day"; 
	}

This is a repeat of the above 2 steps, but this time we are returning the current day. We also will add zeros to single digit days.



$year = $today[year]; 

Next we return the current year in 4 digit form (Ex: 2004).



$run_command = sprintf("mysqldump --opt -h %s -u %s -p%s %s |

We will break this line into two parts since this is the brains behind this script. Firstly this is a sprintf, which allows a placeholder of %s in the line, and then add all the variable names at the end. "mysqldump" is the backup command in MySql which allows us to dump or backup a database. --opt is passing options, these being -h=hostname -u=username -p=password and then the last %s is the database we are backing up. Now to part two.



gzip > %s/%s-%s-%s-%s.gz", $host, $user, $pass, $db, $backupdir, $db, $month, $day, $year);

this is a gzip or compressed filetype. This is the equivalent of Winzip for Unix. > character is stating the name and file path of the "gzip" file. $host,$user,$pass,$db are the four variables used in the previous steps which is the mysqldump variables. The final variables pass in the info that we gathered at the beginning of this script. An explanation for the above 2 lines of code are run a backup on the chosen database and pass the contents of that backup into a newly created gzip file, naming it the database name along with the date variables.



system($run_command);

This line runs or executes the above line which starts the backup process.



Closing Comments

That is the end of the tutorial. Now you have a script that when run will backup your sites database. Of course you could attach this to a cronjob or add a selector to backup multiple databases. The possibilities are endless. Have fun and I hope you enjoyed this tutorial.

Follow Scriptplayground on Twitter (@scriptplay)

Comments: Backup Database

 Keyur  Thu Sep 18, 2008 11:45 pm  
Awesome script!
 Emma  Tue Aug 10, 2010 3:14 am  
Thanks,
Got what we were looking for.
 Used Cars For Sale  Sun Sep 4, 2011 10:52 pm  
Great Script, thank you for sharing ...
 Sidra Sultana  Mon Nov 14, 2011 7:45 pm  
i get the backup file of 4KB in zip format(.gz) and on extraction got an empty file
Add a comment
The ability to add new comments has been disabled in this archived version.

Loading
Main | Tutorials | Articles