PHP MySQL database backup script - dumps database backup

c, c++, php, html, sql ..etc discussion. also includes codes and source codes.

PHP MySQL database backup script - dumps database backup

Postby Sethioz » Fri May 28, 2010 8:14 pm

This php script will dump SQL database backup locally (into server). It will not be accessable from web, it appears as blank page, but it will execute it and grabs the backup. it also logs the ip whoever loads this page.

Code: Select all

function logIP() {

     $register_globals = (bool) ini_get('register_gobals');
     if ($register_globals) $ip = getenv(REMOTE_ADDR);
     else $ip = $_SERVER['REMOTE_ADDR'];
     $date=date ("l dS of F Y h:i:s A");
     $log=fopen("$ipLog", "a+");

     if (preg_match("/\bhtm\b/i", $ipLog) || preg_match("/\bhtml\b/i", $ipLog)) {
          fputs($log, "IP: $ip - Date: $date<br>");
     } else fputs($log, "IP: $ip - Date: $date\n");

$dbhost = 'localhost';
$dbuser = 'db_user';
$dbpass = 'db_password';
$dbname = 'db_name';
$date = date("mdy-hia");

//where to put the backup
$filename = ("db-" . $dbname . ".sql");
$dumpfilepath = ("/home/sites/" . $filename);

//backing it up
exec("mysqldump --opt --quick --compact -h$dbhost -u$dbuser -p$dbpass $dbname >$dumpfilepath");

//compress your backup
exec("gzip -qf " . $dumpfilepath);


you need to modify few places:

$dumpfilepath = ("/home/sites/" . $filename);

> you need the full path of your directory, where you want to save backups, also you need to include a .htaccess file that denys access to whole directory from web, otherwise anybody is able to download your backups.
content of .htaccess should be this :

Code: Select all
<Files *>
   Order Allow,Deny
   Deny from All

> where to save the ip log, must be .html file !

$dbhost = 'localhost';
$dbuser = 'db_user';
$dbpass = 'db_password';
$dbname = 'db_name';

replace with your database info.

made by SomaFM, Luigi and me.


if it is used with e107, then i have better method, i use this instead:

Code: Select all

$dbuser              = $mySQLuser;
$dbpwd                  = $mySQLpassword;
$dbname              = $mySQLdefaultdb;
$dbhost              = $mySQLserver;

replace the other ones with this, do not replace anything, exept the "class2.php" path, it has to lead where your class2.php is located, everything else is left same, it grabs the database using class2.php, which is e107 file.
User avatar
Posts: 4757
Joined: Fri Jul 27, 2007 5:11 pm
Location: unknown

Return to Programming / Coding / Debugging

Who is online

Users browsing this forum: No registered users and 4 guests