Skip to content

Instantly share code, notes, and snippets.

@pastranastevenaz
Forked from micc83/mysqldump.php
Last active August 5, 2020 15:52
Show Gist options
  • Select an option

  • Save pastranastevenaz/0158e5905f476dfd5935d1c058aeea9b to your computer and use it in GitHub Desktop.

Select an option

Save pastranastevenaz/0158e5905f476dfd5935d1c058aeea9b to your computer and use it in GitHub Desktop.
Simple PHP script to dump a MySql database
<?php
// This script will make a dump of your database more securely
// Instead of the PHP code calling the Mysql dump command, and passing the argument of username and
// Password to the Shell, this script creates a hidden configuration file called 'my.cnf' which the scrit will read from.
// It helps obfuscate the password. This is the recommended way to do this according to docs.
// Firts the script chacks if any leftover logfiles for the script exists, and deletes them.
// It does the same with any old dump files
// It then creates the configfile and appends the needed credentials in theere
// We then make that file read only to the user, and no one else 0400
// We can then run the mysqldump command
// when the dump is complete, we create a log file. Currently, all that log file has, is the time it took to run.
// With some exception handling, we can add more to those log files
// THe script then makes that log file read only for everyone.
// Finally, the script deletes (unlinks) the configuration file
date_default_timezone_set('UTC');
if($list = glob('dump.sql')){
echo "dump.sql exists on the machine";
unlink('dump.sql');
}else{
echo "gtg";
}
if($list2 = glob(dirname(__FILE__) . '/.' . '*_dmp.log')){
echo 'found logs';
foreach($list2 as $val){
unlink($val);
}
}else{
echo 'gtg on logs';
}
$timestamp = date("Y-m-d H:i:s");
$time_start = microtime(true);
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$content ='[mysqldump]';
$defaultfile = dirname(__FILE__) . '/.my.cnf';
// private function writeFileContent($file, $content)
// #################### IMPORTANT!!! ####################
// Make sure to change the database creds in the following few lines
// This data gets written to a temporary my.cnf file which gets removed after
// When the dump is complete, it will be removed. This is so the credentials won't appear in any logs
// REMEMBER TO DELETE THIS FILE WHEN DONE!!!
$fp = fopen($defaultfile, 'w');
fwrite($fp, $content);
fputs( $fp, "\n" );
// Edit the following line and change DB_USER to the DB user you want to use
fputs( $fp, "user=DB_USER\n" );
// Edit the following line and change DB_PASSWORD to the DB password you want to use
fputs( $fp, "password=DB_PASSWORD\n" );
fclose($fp);
chmod($defaultfile , 0400);
// You likely don't need to change this
$host="localhost";
// Modify the next line with the Database name
$database = "XXXXX";
$defaultfile = dirname(__FILE__) . '/.my.cnf';
$dir = dirname(__FILE__) . '/dump.sql';
echo "<h3>Backing up database to `<code>{$dir}</code>`</h3>";
exec("mysqldump --defaults-extra-file={$defaultfile} {$database} --host={$host} --result-file={$dir} 2>&1", $output);
var_dump($output);
$time_end = microtime(true);
$logfile = dirname(__file__) . '/.' . $timestamp . '_dmp.log';
//dividing with 60 will give the execution time in minutes otherwise seconds
$execution_time = $time_end - $time_start;
$fp2 = fopen($logfile, 'w');
fwrite($fp2, 'This took '.$execution_time .' sec.' );
fclose($fp2);
chmod($logfile, 0444 );
unlink($defaultfile );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment