20

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

10 Answers 10

11

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. For example: *:*:*:postgres:pg_password.

This .pgpass file 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.

2
  • does it back up automatically everyday??
    – Anuj Patel
    Oct 17, 2011 at 9:05
  • @indyaah: no, you would have to run this daily from cron (or similar).
    – Bill Weiss
    Dec 9, 2011 at 5:29
12

Run pg_dumpall from cron.

2
  • 7
    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, 2009 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, 2009 at 17:21
6

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.

1
  • I also use AutoPostgreSQLBackup and it's working very well for me ;-)
    – Matthieu
    Aug 23, 2009 at 21:42
4

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.

4

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

2

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.

1

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...

1

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 :(

0
1

Why settle down with a daily backup when you can easily have point-in-time recovery with barman?

3
  • I'll let the moderators decide, but I believe resurrecting 5 years old questions to plug commercial products is bad form here.
    – gparent
    Oct 16, 2014 at 16:31
  • ehm, it's open source: sourceforge.net/projects/pgbarman/files Oct 16, 2014 at 16:32
  • You're right, I googled the wrong thing. My main concern wasn't that, anyway, it was about suggesting an answer that's not really what the user asked 5 years afterwards. However, after reading the other answers, they also seem to suggest similar back up solutions so I guess it isn't that bad. My flag will probably be declined and we'll call it a day :D
    – gparent
    Oct 16, 2014 at 16:37
0

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .