tags:

views:

406

answers:

2

I have the following situation.

I have a PHP script that imports a CSV file and then updates the postgres database

Now I need to create a backup of the database before the importing occurs

The PHP files are running on one server and the postgres database on another server

I tried exec(pg_dump db_name -CdiOv > /tmp/db_name_backup.sql) but don't think this will work since the db is on another server.

I'm not sure how to do this, I can right code in PHP performing a backup but that takles ages to run.

Any adwise will be appreciated

+1  A: 

pg_dump can easily connect to remote host - just check -h option.

Also - what do you want to achieve by the "-CdiOv" thing?

depesz
# -C = Begin the output with a command to create the database itself and reconnect to the created database.# -d = Dump the data as INSERT commands# -i = Ignores version mismatch between the old database server and the new database server.# -O = No owner, the database does not belong to any specific owner that was specified in the database# -v = Verbose, will output a detailed report of pg_dump if ran in command line.
Roland
So It will also work if I do something like this: pg_dump domains -CdiOv -h server -u > pg_dump.sql , the only problem here is it prompts for username and password, can I include that in file to since this is an internal system in can be save to send usernames and passwords
Roland
Proper way to handle it is to use .pgpass file ( http://www.postgresql.org/docs/current/interactive/libpq-pgpass.html )
depesz
+3  A: 

As depesz said you need to use the -h option to define the remote host but it will still prompt for a password which is problem. Try:

exec("export PGPASSWORD=mypassword && export PGUSER=myuser && pg_dump -h yourremotehost db_name -CdiOv > /tmp/db_name_backup.sql && unset PGPASSWORD && unset PGUSER");

Alternatively you can use a ~/.pgpass file but I've never tried this. Check out http://www.issociate.de/board/post/43225/pg%5Fdump%5F+%5Fcronjob.html and http://forum.soft32.com/linux/Backup-Postgressql-ftopict460054.html

Kyle Kochis