Remote trigger a postgres database backup

2.4k views Asked by At

I would like to backup my production database before and after running a database migration from my deploy server (not the database server) I've got a Postgresql 8.4 server sitting on a CentOS 5 machine. The website accessing the database is on a Windows 2008 server running an MVC.Net application, it checks out changes in the source code, compiles the project, runs any DB Changes, then deploys to IIS.

I have the DB server set up to do a crontab job backup for daily backups, but I also want a way of calling a backup from the deploy server during the deploy process. From what I can figure out, there isn't a way to tell the database from a client connection to back itself up. If I call pg_dump from the web server as part of the deploy script it will create the backup on the web server (not desirable). I've looked at the COPY command, and it probably won't give me what I want. MS SQLServer lets you call the BACKUP command from within a DB Connection which will put the backups on the database machine.

I found this post about MySQL, and that it's not a supported feature in MySQL. Is Postgres the same? Remote backup of MySQL database

What would be the best way to accomplish this? I thought about creating a small application that makes an SSH connection to the DB Server, then calls pg_dump? This would mean I'm storing SSH connection information on the server, which I'd really rather not do if possible.

4

There are 4 answers

1
Tometzky On BEST ANSWER

Create a database user pgbackup and assign him read-only privileges to all your database tables.

Setup a new OS user pgbackup on CentOS server with a /bin/bash shell.

Login as pgbackup and create a pair of ssh authentication keys without passphrase, and allow this user to login using generated private key:

su - pgbackup
ssh-keygen -q -t rsa -f ~/.ssh/id_rsa -N ""
cp -a ~/.ssh/.id_rsa.pub ~/.ssh/authorized_keys

Create a file ~pgbackup/.bash_profile:

exec pg_dump databasename --file=`date +databasename-%F-%H-%M-%S-%N.sql`

Setup your script on Windows to connect using ssh and authorize using primary key. It will not be able to do anything besides creating a database backup, so it would be reasonably safe.

1
Tometzky On

I'm not sure what you mean but I think you can just use pg_dump from your Windows computer:

pg_dump --host=centos-server-name > backup.sql

You'd need to install Windows version of PostgreSQL there, so pg_dump.exe would be installed, but you don't need to start PostgreSQL service or even create a tablespace there.

1
oguri On

Hi Mike you are correct,

Using the pg_dump we can save the backup only on the local system. In our case we have created a script on the db server for taking the base backup. We have created a expect script on another server which run the script on database server.

All our servers are linux servers , we have done this using the shell script.

1
nnyby On

I think this could be possible if you create a trigger that uses the PostgreSQL module dblink to make a remote database connection from within PL/pgSQL.