Salve ragazzi ho trovato su un sito questo script che effettua backup di postgres usando proc_open e pg_dump e utilizzando queste variabili $dbName, $dbUser, $dbPwd, $backupPath funziona benissimo e salva il file nella directory $backupPath.
Codice PHP:
<<?php
function postgresBackup($dbName, $dbUser, $dbPwd, $backupPath)
{
$fSuccess = FALSE;
// get rid of try..catch for PHP 4 or less
try {
ignore_user_abort(TRUE);
$file = date('YmdHis') . "_pgDBBackup.sql";
$buffer = '';
$logFile = "/tmp/pgdump-error-output.txt";
$descriptorspec = array(
0 => array("pipe", "r"), // stdin
1 => array("pipe", "w"), // stdout
2 => array("file", "$logFile", "a") // stderr
);
// may need entire path to pg_dump
$cmd = "pg_dump -c -D -U {$dbUser} {$dbName}";
$process = proc_open("$cmd", $descriptorspec, $pipes);
if (is_resource($process)):
// $pipes now looks like this:
// 0 => writeable handle connected to child stdin
// 1 => readable handle connected to child stdout
// Any error output will be written to log file
// send the password and close the stdin
fwrite($pipes[0], "{$dbPwd}\n");
fclose($pipes[0]);
// read in the dump data and close stdout
while(!feof($pipes[1])):
$buffer .= fgets($pipes[1], 1024);
endwhile;
fclose($pipes[1]);
// close any pipes before calling
// proc_close to avoid a deadlock
$return_value = proc_close($process);
// write the file to disk and return true
if($return_value == 0):
// this could be modified to automatically
// force a file download if run from a web page
// may need to change this line for PHP 4 or less
file_put_contents($backupPath . $file, $buffer);
// successfully created backup
$fSuccess = TRUE;
endif;
endif;
} catch(Exception $ex) {
error_log($ex->getMessage(), 3, $logFile);
return FALSE; // return so log file is not deleted
}
// comment out the following for debug information if backup fails
@unlink($logFile);
return $fSuccess;
}
// example usage
postgresBackup('mydb','myuser','pwd',"/home/db/");
?>
e poi utilizzando il comando fpassthru per leggere il buffer. ma il tutto va in errore evidentemente sbaglio qualcosa.
Qualcuno può aiutarmi please.
Grazie in anticipo