You are here: Home Database PostgreSQL Basics Backup database with cronjob
Search
Advanced Search…
E-Mail

Webmail: webmail.wyden.com

E-Mail Preferences: postfix.wyden.com/users

E-Mail Administration: postfix.wyden.com

Statistics
Total: 473
Total Pages: 286
Total Folders: 87
Total Files: 18
Total Links: 26
Last modification: 19.04.2012 15:21
 

Backup database with cronjob

by Wyden Silvan last modified 22.06.2010 11:01

1) Connect to postgres user
su - postgres
 
2) Create a .pgpass file
vi .pgpass
*:5432:database:username:password
 
chmod 600 .pgpass
 
3) The postgres dump command script that you can call with the cronjob:

#!/bin/sh
#(c)2010 brain-tec.ch - Wyden Silvan

#CONFIG

databases=(db1 db2)
prefix=backup
backupfolder='/var/lib/postgresql/backup/'
days=30


#DO NOT MAKE CHANGES AFTER THAT LINE!!!

#delete all the files older then days
find $backupfolder -type f -mtime +$days -exec rm '{}' \;

#actual date
datum=$(date +"%Y")-$(date +"%m")-$(date +"%d")

#backup the selected databases
for database in ${databases[@]}
do
pg_dump $database > $backupfolder$prefix-$database-$datum.sql
done