Backup database with cronjob
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
