Hi there everyone!

I would like to use this script to backup my site's db's so my automated file backup solution will download them along with the files.

https://github.com/daniloaz/myphp-backup

So the magic bits are:


define("DB_USER", 'your_username');
define("DB_PASSWORD", 'your_password');
define("DB_NAME", 'your_db_name');

$backupDatabase = new Backup_Database(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME, CHARSET);
$result = $backupDatabase->backupTables(TABLES, BACKUP_DIR) ? 'OK' : 'KO';
$backupDatabase->obfPrint('Backup result: ' . $result, 1);

// Use $output variable for further processing, for example to send it by email
$output = $backupDatabase->getOutput();


My thinking is that I need to make the db credentials an array and I need to convert the last bit to work through it. The issue I have is that sometimes, there will be multiple DB names attached to the same user, so I need to set that up as a subarray of some sort.

{
User: bob
pass: password
db names: cats, dogs, dolphins
}
{
User: john
pass: 11111
db names: bikes, cars, boats
}
{
User: jane
pass: 123456
db names: pizza
}

But I'm having problems figuring out how to convert this script to work through all available databases. Any help on doing this would be greatly appreciated.

Thanks for your time!

    If you make each database an array entry (versus them being a comma-separated list -- if that's what you're thinking), then it could just be an instance of two nested foreach() loops: the outer to get the user name and password, the inner to iterate over each DB name. In JSON terms:

    $data = <<<EOD
    [
      {
        "user": "john",
        "pass": "12341234",
        "databases": [
          "cats",
          "dogs"
        ]
      },
      // repeat for next user...
    ]
    EOD;
    
    $config = json_decode($data, 1);
    foreach($config as $user) {
      $name = $user['user'];
      $pass = $user['pass'];
      foreach($user['databases'] as $db) {
        // call your function with $name, $pass, and $db
      }
    }
    
      Write a Reply...