PostgreSQL BackingUp and Restore PHP script

Discussion in 'PHP' started by SanjaySL, May 22, 2006.

  1. #1
    Hi,

    I need to write a php script which can Backup and restore a PostgreSQL database.The database is stored in a Linux server.
    Can anyone give me a code example using php to do the above.

    eg : backup.php file

    $datbaseName = DB_NAME;
    $userName = DB_USER;
    $password = DB_PASSWORD;
    $timeStampValue = time();
    $saveLocation = "/tmp/";

    $createBackup = "pg_dump -U" . $userName. "-Fp " .$datbaseName. " > " . $saveLocation.$timeStampValue."."."sql";

    I have written above coding.How can I excute the $createBackup in PHP so it will backup the database(both data and the database strcuture).
    Actually the backup is done when a link in our website is clicked.When its clicked backup.php should be called and the above coding should get excuted.

    **Have I correctly written the above coding.?
    By the way I need to append the time stamp to the name of the backup file cuz we create 5 backup files and later going to delete the oldest file and get the latest backup file.(5 files for 5days cuz backups are taken from Mon-Fri)

    *** Isnt there anyway to password also.Cuz I have found that using the
    -W option, a prompt will appear to enter a password which I dont want to happen.Can I pass the password also like the database name,username etc...

    Kind Regards
    Dreamer
     
    SanjaySL, May 22, 2006 IP
  2. Dan Grossman

    Dan Grossman Peon

    Messages:
    177
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Use exec() to run a shell command.

    As for your question about passing the password.. that depends on the syntax of the pg_dump command. Run it with the --help option to see if it gives you a list of available parameters.
     
    Dan Grossman, May 23, 2006 IP