Shell Scripts

Shell script to take PostgreSQL Server daily backup using pg_basebackup :

####################################################################
#       Script Name     :       pg_basebackup_daily.sh
#       Date            :       28/08/2019
#       Author          :       Database Team
#       Description     :       This script will take the backup of PostgreSQL server.
####################################################################
#!/bin/bash
#Location of the backup logfile.
logfile="/backup/logs"
#Location to place backups.
backup_dir="/backup/"

time=`date +%H-%M`
BACKUP_USER=bkpadm
RSTATUS=1

##Environment Variables##
export PATH=/edbas95/9.5AS/bin:$PATH #EDB PostgreSQL binaries location
export EDBHOME=/edbas95/9.5AS #EDB PostgreSQL Home Location
export PGDATA=/data/ #PostgreSQL Data Directory Location
#export PGDATABASE=edb
#export PGUSER=enterprisedb
export PGPORT=5432 #PostgreSQL Server PORT
export PGLOCALEDIR=/edbas95/9.5AS/share/locale
MAILTO="Your_Email_ID@gmail.com"

ipadr=$(/sbin/ifconfig  | grep 'inet'| grep -v '127.0.0.1' | awk 'FNR==2{print $0} ' |awk '{ print $2}')

if [ ! -d $backup_dir ];
then
  mkdir -p $backup_dir
fi

pg_bkp_log=$logfile/pgbkp_log_$time.log
echo >> $pg_bkp_log
chmod 666 $pg_bkp_log

echo ==$BACKUP_USER on $ipadr  == >> $pg_bkp_log
echo ==== started on `date` ==== >> $pg_bkp_log
#--xlog - This will include all transaction logs generated during the backup.  
pg_basebackup  --xlog   -U $BACKUP_USER -D $backup_dir/pgbkp-`date +%Y%m%d%H` --progress

tar -zcvf $backup_dir/pgbkp-`date +%Y%m%d%H`.tar.gz  $backup_dir/pgbkp-`date +%Y%m%d%H`

if [ $? -ne 0 ]
     then
      echo "PostgreSQL backup failed on $HOSTNAME  -- check $pg_bkp_log for errors"| mail -s "DBA Team ;$HOSTNAME:PostgreSQL Backup failed !" $MAILTO < $pg_bkp_log
   else
   echo "PostgreSQL Successfully completed on $HOSTNAME  -- check $pg_bkp_log for the same"| mail -s "PostgreSQL Successfully completed on $HOSTNAME !" $MAILTO < $pg_bkp_log
fi
Review
4

About the author

BestTechReads

Add Comment

Click here to post a comment

Categories

Categories