<?php
// ----------------------------------------------------------------------------
// Script Author: Robert Holland
// Script Name: db_backup_script.php
// Creation Date: Fri Nov 17 2023 07:03:27 GMT-0700 (MST)
// Last Modified: 2025-06-12 10:20:27
// Version: 1.0.2
// Purpose: Generate MariaDB database backup statements (This script only generates non-system database backup statements. However,
// you can modify it to include the system databases (information_schema, performance_schema, mysql, sys)).
// ----------------------------------------------------------------------------

include't4.php';

$dbshow = new Connection(); //Instantiate a new connection.
$dbshow->myQuery("show databases;"); //The databases shown will depend on the privileges of the user running the statement.
$rows = $dbshow->All();

echo "The following system databases (information_schema, performance_schema, mysql, sys) are <b>NOT</b> included in this mysqldump script.<br /><br />";
echo "Dump the output of this PHP script into a shell script (php -f db_backup_script.php > db_backup.sh).<br /><br />";
echo "TimeStamp=`date +\"%Y%m%d%H%M%S%Z\"`<br />";

$dbusername = "TheDB_Username";
$ThePassword = "The Password";
function myfunction($value,$key)
{
global $dbusername; //Variable is defined outside of the function so it has to be global to work inside the function.
global $ThePassword;
echo "mysqldump -u " . $dbusername . " -p\"". $ThePassword . "\" -c -e " . $value . " > \$TimeStamp.DBDump." . $value . ".`hostname`.sql<br />";
echo "tar -zcvf \$TimeStamp.DBDump." . $value . ".`hostname`.sql.gz \$TimeStamp.DBDump." . $value . ".`hostname`.sql<br />";
}

// Remove these system databases from the backup script.
// information_schema, mysql, performance_schema, sys
$i=count($rows);
for($x = 0; $x < $i; $x++){
$y=$rows[$x];
// array_diff to exclude system databases from the generated output.
// If you would like to include the system databases in the generated mysqldump statements, simply delete the desired database name from the array below.
$y = array_diff($y, array("information_schema", "performance_schema","mysql","sys"));
array_walk($y,"myfunction");
}
?>