Monday, December 9, 2013

Backup Script for Postgresql in Linux

#!/bin/bash

# Location to place backups.
BACKUP_DIR="/home/adempiere-backup/"

# Database User
PGUSER=postgres

#PGPASSWORD
PGPASSWORD=password

#SET User name and Password
export PGUSER PGPASSWORD

#String to append to the name of the backup files
BACKUP_DATE=`date +%Y%m%d%H%M%S`.dump

#Numbers of days you want to keep of copy databases
#NO_OF_DAY=30

#LIST DATABASES
DATABASE=`psql -l -t | cut -d'|' -f1 | sed -e 's/ //g' -e '/^$/d'`
for i in $DATABASE; do
  if [ "$i" != "template0" ] && [ "$i" != "template1" ] && [ "$i" != "postgres" ]; then
    echo Dumping $i to $BACKUP_DIR$i\_$BACKUP_DATE
    pg_dump -Fc $i > $BACKUP_DIR$i\_$BACKUP_DATE
  fi
done
#find $BACKUP_DIR -type f -prune -mtime +$NO_OF_DAY -exec rm -f {} \;

#Clear PGUSER and PGPASSWORD
PGUSER=""
PGPASSWORD=""
export PGUSER PGPASSWORD
#End

No comments:

Post a Comment