note 69530 added to function.proc-open

From: Date: Fri, 08 Sep 2006 22:02:07 +0000
Subject: note 69530 added to function.proc-open
Groups: php.notes 
Request: Send a blank email to php-notes+get-116946@lists.php.net to get a copy of this message
Here's an extremely useful function to perform data dumps (pg_dump) on a PostgreSQL database. It could be easily modified to zip the data into a zip or gz file and force a browser download window. Or it could be used as in a nightly backup script. <?php /** * postgresBackup: Creates a backup of a postgresql database * @author Michael Honaker - Symmetry Technical Consultants, LLC * @date 2006-06-15 * @param - string - DB name * @param - string - User name * @param - string - Password * @param - string - Backup directory * @return - bool - Success or failure **/ 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/"); ?> ---- Server IP: 216.194.113.175 Probable Submitter: 161.97.2.175 ---- Manual Page -- http://www.php.net/manual/en/function.proc-open.php Edit -- https://master.php.net/note/edit/69530 Del: integrated -- https://master.php.net/note/delete/69530/integrated Del: useless -- https://master.php.net/note/delete/69530/useless Del: bad code -- https://master.php.net/note/delete/69530/bad+code Del: spam -- https://master.php.net/note/delete/69530/spam Del: non-english -- https://master.php.net/note/delete/69530/non-english Del: in docs -- https://master.php.net/note/delete/69530/in+docs Del: other reasons-- https://master.php.net/note/delete/69530 Reject -- https://master.php.net/note/reject/69530 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#116946) next »