Skip to content

Instantly share code, notes, and snippets.

@hparadiz
Created August 3, 2020 07:01
Show Gist options
  • Save hparadiz/2bc9fff40ec48d85867889ef3debb925 to your computer and use it in GitHub Desktop.
Save hparadiz/2bc9fff40ec48d85867889ef3debb925 to your computer and use it in GitHub Desktop.
PHP Database Backup with SSH Tunneling
#!/usr/bin/env php
<?php
require_once(getenv('PHP_LIBS_AUTOLOADER'));
(new \NunoMaduro\Collision\Provider)->register();
class Config extends BackupManager\Config\Config
{
public static function fromArray(array $array)
{
return new static($array);
}
}
$filesystems = new BackupManager\Filesystems\FilesystemProvider(Config::fromArray([
'local' => [
'type' => 'Local',
'root' => '/home/akujin',
]
]));
$filesystems->add(new BackupManager\Filesystems\LocalFilesystem);
$databases = new BackupManager\Databases\DatabaseProvider(Config::fromArray([
'development' => [
'type' => 'mysql',
'host' => 'localhost',
'port' => '3307',
'user' => 'root',
'pass' => '',
'database' => 'technexus',
'singleTransaction' => true,
'ignoreTables' => [],
'ssl'=>false,
]
]));
$databases->add(new BackupManager\Databases\MysqlDatabase);
$compressors = new BackupManager\Compressors\CompressorProvider;
$compressors->add(new BackupManager\Compressors\GzipCompressor);
$compressors->add(new BackupManager\Compressors\NullCompressor);
$loop = React\EventLoop\Factory::create();
$process = new React\ChildProcess\Process("ssh -fN -L 3307:127.0.0.1:3306 akuj.in");
$process->start($loop);
try {
if (!file_exists($path = 'Dropbox/DigiNet/Backups/technexus')) {
mkdir($path, 0755, true);
}
(new BackupManager\Manager($filesystems, $databases, $compressors)) ->makeBackup() ->run('development', [
new \BackupManager\Filesystems\Destination('local', $file = sprintf('%s/backup-%s.sql', $path, date('U'))),
], 'gzip');
(new League\CLImate\CLImate())->info(sprintf('Saved %s.gz', $file));
} catch (\Exception $e) {
dump($e);
}
$process->terminate(SIGKILL);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment