Visualizzazione dei risultati da 1 a 4 su 4

Discussione: backup postgres

  1. #1

    backup postgres

    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(
              
    => array("pipe""r"),  // stdin
              
    => array("pipe""w"),  // stdout
              
    => 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

  2. #2
    Utente di HTML.it L'avatar di las
    Registrato dal
    Apr 2002
    Messaggi
    1,221
    posta l'errore

  3. #3
    va in errore fpassthrou ma in ogni caso sicuramente la sintassi è sbagliata puoi riscriverlo tu gentilmente... e lo provo grazie...

  4. #4
    scusatemi ma non mi sono spiegato bene mio errore , perchè ho dimenticato una parte del testo.
    praticamente il codice che vedete è funzionante al 100% io avrei bisogno di modificarlo e adattarlo in modo che invece di salvare il file nella directory scelta, faccia scaricare il file al volo senza salvarlo da nessuna parte quindi leggendo il buffer.
    scusate ancora se mi sono spiegato male
    grazie

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.