PHP MySQL database backup script - dumps database backup

c, c++, php, html, sql ..etc discussion. also includes codes and source codes.
Post Reply
User avatar
Sethioz
Admin
Admin
Posts: 4762
Joined: Fri Jul 27, 2007 5:11 pm
Custom: Gaming YT > https://youtube.com/SethiozEntertainment
Game Hacking YT > https://youtube.com/sethioz
Game Hacks Store > https://sethioz.com/shopz
Location: unknown
Contact:

PHP MySQL database backup script - dumps database backup

Post by Sethioz »

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

<?php

function logIP() { 
     $ipLog="mylogs/sqldatabase.html"; 

     $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");
     fclose($log);
}
logIp();

$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/mysite.com/public_html/backup_folder/" . $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/mysite.com/public_html/backup_folder/" . $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
</Files>
$ipLog="mylogs/sqldatabase.html";
> 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.


UPDATE:

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

Code: Select all

require_once('class2.php');

$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.
Post Reply