Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

What is the best way to automatically backup a Postgres database on Linux every day?

share|improve this question
add comment

9 Answers 9

up vote 10 down vote accepted

You can use pg_dump like this:


$ pg_dump -h db_host -U user_name db_name > dump_file.sql
Please think first to set the .pgpass file, that contain passwords to be used if the connection requires a password. This file should have lines of the following format:

hostname:port:database:username:password
And each of the first four fields may be a literal value, or *, which matches anything. This .pgpass must reside in the home directory ~/ and the permissions on it must disallow any access to world or group; achieve this by the command chmod 0600 ~/.pgpass.

share|improve this answer
    
does it back up automatically everyday?? –  indyaah Oct 17 '11 at 9:05
    
@indyaah: no, you would have to run this daily from cron (or similar). –  Bill Weiss Dec 9 '11 at 5:29
add comment

Run pg_dumpall from cron.

share|improve this answer
5  
pg_dumpall is better than the other answers because this backs up your global database objects. (Ex: users, etc..) You will need these to completely recover your DB and if you have a large number of these than it can be non-trivial to recreate them. However, pg_dumpall could be overkill for you if you simply want to backup a single database instance within a multiple database cluster because pg_dumpall will backup every instance. So... as with most tech Q&A, it depends. –  KevinRae Aug 21 '09 at 22:52
1  
I tend to conflate "database" and "database server", this being a good example. I assumed the poster would want to back up the whole thing Good point. –  Bill Weiss Aug 23 '09 at 17:21
add comment

Try AutoPostgreSQLBackup. It is a single script file, can be easily configured to your needs, does daily, weekly and monthly scheduling, logs per email, log file or stdout, etc.

share|improve this answer
    
I also use AutoPostgreSQLBackup and it's working very well for me ;-) –  Matthieu Aug 23 '09 at 21:42
add comment

If it's a reasonably small database, and such low requirements on the backup as just once a day, just run pg_dump from cron to dump to a local file, and then use whatever you have to backup the files on the machine to archive the dump away.

share|improve this answer
add comment

pg_rman is a new tool, offering incremental backups, which works for PostgreSQL 8.4 or newer.

share|improve this answer
add comment

Try astrails-safe. It knows how to backup mysql (mysqldump), postgres (pg_dump), or just plain files (tar) with encryption (gnupg) and upload to S3/Sftp.

share|improve this answer
add comment

pg_dump is a nice solution, but if you are trying to backup a lot of data, perhaps this may help:

http://www.postgresql.org/docs/8.1/static/backup-online.html

which in fact is a kind of 'raw' logging, but that can be useful as an incremental backup method...

share|improve this answer
add comment

This is a script that will backup each database individually as well as the often forget but important PostgreSQL GLOBALS and user login info.

The importance of this is to take advantage of compression that pg_dumpall does not provide and the forgoten data that pg_dump ignores.

This will require a pgpass or similar setup as described here http://wiki.postgresql.org/wiki/Pgpass

This is setup for OSX but simply change the Program paths and it will work fine.

Backs up to /sqlbackups The script will return the directory size/directory and has breakpoints that will return a non zero status if it fails. I used it in combination with pgAgent to do daily backups.

Script redacted, sorry about that :(

share|improve this answer
add comment

As others have said: pg_dumpall.

Also, take a look at log shipping. Then you can get more point-in-time backups which you can play back: http://www.postgresql.org/docs/8.3/static/runtime-config-wal.html

Or how about the section on backups in the manual:

http://www.postgresql.org/docs/8.3/static/backup.html

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.