Skip to content

Instantly share code, notes, and snippets.

@yookoala
Created May 21, 2019 10:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yookoala/808ec70ffefb8e5a6669868d48d98efe to your computer and use it in GitHub Desktop.
Save yookoala/808ec70ffefb8e5a6669868d48d98efe to your computer and use it in GitHub Desktop.
A very efficient way to get mysql dump from remote PHP server.
<?php
// Note: Some authentication logics should be here!
// Setup enviroment
header('Content-Disposition: attachment; filename="backup.sql"');
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
ini_set('max_execution_time', 0); // disable execution time limit
error_reporting(E_ALL);
// Database information (please fill in your own)
$dbname = '...';
$user = '...';
$pass = '...';
$host = '...';
$port = '...';
// Prepare proc context
$descriptorspec = [
0 => array("pipe", "r"), // stdin is a pipe that the child will read from
1 => array("pipe", "w"), // stdout is a pipe that the child will write to
2 => array("pipe", "w") // stderr is a pipe that the child will write to
];
flush();
// Run the mysqldump command then passthrough the output
$cmd = "mysqldump --user={$user} --password={$pass} --host={$host} --port={$port} {$dbname}";
$process = proc_open($cmd, $descriptorspec, $pipes, realpath('./'), array());
if (is_resource($process)) {
while ($s = fgets($pipes[1])) {
print $s;
flush();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment