script to take the dump backup in postgresql

Before using the script create the below directory structure

Create directory : /home/postgres/BACKUP

create below folders inside BACKUP

BASEBACKUPS
DUMPS
scripts

inside dumps create logs , PGDUMP folders.
inside scripts put the below script with name you like and give execution permissions. I gave name dump_backup.sh

chmod +x dump_backup.sh

#!/bin/bash
LOG_LOC=/home/postgres/BACKUP/DUMPS/logs
BACKUP_LOC=/home/postgres/BACKUP/DUMPS/PGDUMP
DATABASE=$1
DUMP_FILE=$BACKUP_LOC”/”$DATABASE”_”$(date +%d%m%Y%H%M).custom
LOG_FILE=$LOG_LOC”/”$DATABASE”_”$(date +%d%m%Y%H%M).log

pg_dump –file=$DUMP_FILE –format=c –verbose –blobs –create $DATABASE >> $LOG_FILE 2>&1
if [ $? -eq 0 ]
then
echo “Backup is successful” >> $LOG_FILE
else
echo “Backup failed” >> $LOG_FILE
fi

Execute script using below command:

sh dump_backup.sh postgres ==> This will take the dump of postgres database in custom format.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s