Create Nightly Postgres Backups with logrotate

By | 2014/11/05

Logrotate can do more than just rotate logs. Check out how to use logrotate to perform nightly postgres backups!


This example is for Debian Linux. Alter as desired of course!


1. Create a logrotate config file in /etc/logrotate.d/

Example:

/etc/logrotate.d/pgdump-bkup

Here is my pgdump-bkup file:

/var/backups/postgres.pgdump.gz {
daily
rotate 8
nocompress
create 640 root adm
postrotate
sudo -u postgres pg_dumpall > /var/backups/postgres.pgdump 
gzip -9f /var/backups/postgres.pgdump
endscript
}


2. Create initial backup file

So logrotate will be happy, create an initial file of your backup. (Or alternatively, run your backup manually once.)

$ sudo touch /var/backups/postgres.pgdump.gz

3. Done! Your /var/backups directory will now have timestamped postgres database backups.
stmiller@chicago:/var/backups$ ls -l
total 14744
-rw-r--r-- 1 root root   5881296 Nov  4 18:22 aptitude.pkgstates.0
-rw------- 1 root root       730 Nov  4 20:15 group.bak
-rw------- 1 root shadow     606 Nov  4 20:15 gshadow.bak
-rw------- 1 root root      1405 Nov  4 20:15 passwd.bak
-rw-r--r-- 1 root root   9197846 Nov  5 09:55 postgres.pgdump.gz
-rw------- 1 root shadow    1007 Nov  4 20:15 shadow.bak
stmiller@chicago:/var/backups$ 

Leave a Reply

Your email address will not be published. Required fields are marked *

Notify me of followup comments via e-mail. You can also subscribe without commenting.