Skip to content

Instantly share code, notes, and snippets.

@LukeTowers
Last active January 31, 2021 05:46
Show Gist options
  • Save LukeTowers/b97607cde8931d19e5bb3d110c07d7bc to your computer and use it in GitHub Desktop.
Save LukeTowers/b97607cde8931d19e5bb3d110c07d7bc to your computer and use it in GitHub Desktop.
Homestead Helpers
<?php
$user = 'homestead';
$pass = 'secret';
$backupDir = '/vagrant/.backup';
$dbToSkip = ["information_schema", "mysql", "performance_schema", "sys"];
$driver = new mysqli_driver();
$driver->report_mode = MYSQLI_REPORT_ALL & MYSQLI_REPORT_STRICT;
$conn = new mysqli('127.0.0.1', $user, $pass);
$result = $conn->query("show databases");
$dbs = [];
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
if (!in_array($row['Database'], $dbToSkip)) {
$dbs[] = $row['Database'];
}
}
foreach ($dbs as $db) {
echo "Backing up $db...\r\n";
echo exec("mysqldump -u {$user} -p{$pass} \"{$db}\" > \"/vagrant/.backup/$db.sql\"");
}
<?php
$user = 'homestead';
$pass = 'secret';
$backupDir = '/vagrant/.backup';
$backedUpDbs = glob($backupDir . '/*.sql');
$driver = new mysqli_driver();
$driver->report_mode = MYSQLI_REPORT_ALL & MYSQLI_REPORT_STRICT;
$conn = new mysqli('127.0.0.1', $user, $pass);
foreach ($backedUpDbs as $path) {
$db = pathinfo($path, PATHINFO_FILENAME);
echo "Restoring $db...\r\n";
if (!$conn->query("CREATE DATABASE IF NOT EXISTS `{$db}`")) {
echo "Error: " . $conn->error . "\r\n";
}
echo exec("mysql -u {$user} -p{$pass} \"{$db}\" < \"{$path}\"");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment