#!/usr/bin/php
<?php
$password = $argv[3];
$host = $argv[1];
$user = $argv[2];
if (!$password)
{
    echo "pty_mysql_backup_all_each_db.php 1.0\n";
    echo "Copyright (C) 2019 by cpueblo, PlatyHouse Co.,LTD.\n";
    echo "Web site: https://www.platyhouse.com/\n\n";
    echo "Usage php pty_mysql_backup_all_each_db.php <HOST> <USER> <PASSWORD>\n";
    exit;
}

if (!$host)    $host = "localhost";
if (!$user)    $user = "root";

$connection = mysqli_connect($host, $user, $password);
if (!$connection) {
    die ('Could not connect database.' . mysqli_error()."\n");
}

$databases = mysqli_query($connection, "SHOW databases");
while ($databaseInfo = mysqli_fetch_array($databases)) { // go through each row that was returned in $result
    $dbName = $databaseInfo[0];

    if ($dbName == "performance_schema" || $dbName == "information_schema")
        continue;

    echo "====================================\ndatabase = $dbName\n====================================\n";
    mysql_backup($host, $user, $password, $dbName);
}

echo "finished\n";
mysqli_close($connection);

function mysql_backup($host, $user, $password, $dbName)
{
    $cmd = "mysqldump -u $user -p$password -h $host $dbName --default-character-set=utf8mb4 --routines --events --add-drop-table --add-drop-database --complete-insert --extended-insert=FALSE --single-transaction --ignore-table=$dbName.t_pty_logs --ignore-table=$dbName.t_pty_inbounds > $dbName.sql";
    #$cmd = "mysqldump -u $user -p$password -h $host $dbName --default-character-set=utf8mb4 --routines --events --add-drop-table --add-drop-database --complete-insert --extended-insert=TRUE --single-transaction --ignore-table=$dbName.t_pty_logs --ignore-table=$dbName.t_pty_inbounds > $dbName.sql";
    echo $cmd;
    system($cmd);
}

?>
