Home | Demo | Download source | Help

Database Backup Tool - goranpilipovic.com

A Database backup tool that should make you sleep better at night :)

DB Backup is a PHP class to help you automatically backup mysql databases, compress and upload on FTP server or email.




Tested on na Linux (Fedora Core) i Windows Vista with apache2 servers, but there should be no problem on any other standard system.

Compression options


RAR is free compression software and there are both linux and Windows (WinRAR) versions available. Installation is very fast and easy and most commercial servers already have it installed.

Linux installation.

To install free RAR, access shell and enter:

apt-get install rar

Windows installation.

Visit rarlab.com, download WinRAR and install it. Just pay attention on the installation directory, if you don't add it to the PATH environment variable, then you have to enter full path to rar.exe, for example:

$cfg->rar['exec'] = 'c:/Progra~1/WinRAR/rar mf -v[CHUNK_SIZE] -ep1 -idp -o+ ';


Gzip option from mysqldump is available on linux systems only, simply set:

$cfg->dump['gzip'] = true;

That way mysqldump sends output directly to gzip which is much faster than compressing using PHP, and file size won't be affected with memory_limit setting in php.ini. If you use gzip option, ne need to use any other compression because you won't get significantly smaller files.


Use it only if you don't have zip installed. Dump file is compressing using PHP Zip class, therefore big files can cause problems with memory_limit setting in php.ini. Recommended only for smaller files (few MB).


This option is already available on almost all Linux systems. On Windows you will need to install manually some command line zip program.

BZIP2 (.bz2)

BZIP2 is free compression software and you can find both windows and linux versions. Installation is very easy and most commercial servers already have it installed.

DB Backup Installation

Installation is very easy and there are no special procedures or additional modules required.

  1. Download latest version on your computer
  2. Unpack (decompress) archive in some directory
  3. Create directory on the server where you want to upload it (for example /db-backup/)
  4. Upload on server, for example www.mydomain.com/db-backup/
  5. Setup backupdir permsissions, allow write (linux only)
  6. Modify config.php and enter your parameters and options (database, email, ftp...)
  7. Run index.php on your server, for example www.mydomain.com/db-backup/index.php
  8. Directory password protection is highly recommended

Sample config file

 * DB_Backup config file

$cfg = new DB_Backup_Config;

$cfg->db = array					// Database connection settings
	'hostname'	=> 'localhost',			// mysql server
	'database'	=> 'test',			// mysql database name
	'username'	=> 'root',			// mysql username
	'password'	=> 'password',			// mysql password

$cfg->dump = array					// MySQLdump settings
	'mysqldump'	=> 'mysqldump',			// location of mysqlump
	'path'		=> '/db-backup/backupdir/',	// where to put backup files
	'prefix'	=> 'dbbackup.',			// prepend to dump filename
	'extension'	=> '.sql',			// final file extension
	'gzip'		=> false,			// if you want to send it straight to gzip instead of stdout (LINUX ONLY)
	'use_date'	=> true,			// use date YYYY-mm-dd in filename
	'use_time'	=> false,			// use time HH-mm-ss in filename

$cfg->use_compression = 'rar';				// Which compression to use? Available: none, gzip, rar, phpzip, zip, bzip2

$cfg->rar = array					// RAR settings
	'password'	=> 'dump',			// rar password (leave empty for no password)
	'exec'		=> 'rar mf -ep1 -idp -o+ ',	// rar command string (include full path if neccessary)
	'chunk_size'	=> '100k',			// rar chunk size (leave empty if you don't want chunks but single file)

$cfg->zip = array					// ZIP settings
	'password'	=> 'dump',			// rar password (leave empty for no password)
	'exec'		=> 'zip -m -q ',		// zip command 

$cfg->bzip2 = array					// BZIP2 settings
	'exec'		=> 'bzip2 -zq ',		// bz2 command

$cfg->use_ftp = true;					// To send file on FTP server set use_ftp to true and modify FTP settings below
$cfg->ftp = array
	'hostname'	=> 'www.myserver.com',		// FTP server (no / at the end)
	'username'	=> 'ftp_username',		// FTP username
	'password'	=> 'ftp_password',		// FTP password
	'path'		=> '/public_html/some/dir/',	// Directory to upload on ftp server
	'port'		=> 21,				// FTP port (usually 21)
	'passive'	=> true,			// Use passive mode as web browser
	'move'		=> true,			// Move file (delete local file after successfull upload)
	'report'	=> true,			// Send email summary report after FTP upload

$cfg->use_email = true;				// To send dump file to email set use_email true and modify $cfg->email parameters
$cfg->email = array
	'to'		=> 'email@example.com',		// where to send email
	'subject'	=> 'Auto database dump: ',	// subject
	'from'		=> 'webmaster@example.com',	// sender


What about automatic Run?

Yes, there is example of automated backup, simply setup cron to run php script called autobackup.php. Of course you can change file name and interval (daily, hourly ...). If you simply call autobackup.php it will dump database specified in your config file, but if you want to override database name, simply use: autobackup.php?database=your_database_name


Keep only last 7 backups

If you don't want to pile backup files on your FTP server, and keep backup for the last 7 days only, there's small trick. In language file there's constant called FORMAT_DATE_FILE and it's set as current date "Y-m-d". You chan change it to "l" (lowercase L) and it will add Monday, Tuesday, ... Sunday to your backup file name, instead of having different one for each date. That way, next Monday will overwrite your 7 days old Monday backup. So you get fresh copy while your FTP server is nice and clean.

Known Bugs

Problem:: There is a problem with large dump files on some systems. Even if you set enough memory available using ini_set ('memory_limit', '128M'); it has problems to compress or attach large file and you get "memory exhausted" PHP error.

Solution: Use any compression other than PHPZIP and use FTP upload instead of email, that way size does not matter.

Questions, suggestions and insults

If you have any question or anything you want to say, please contact us on contact at utpcode.com.