Last active
July 14, 2017 16:11
-
-
Save gajoseph/10fe04c91acfd126ca3fcbd67355ba13 to your computer and use it in GitHub Desktop.
postgres pg_dump to backup the db/ schema
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############################################################################################# | |
#--GEO--C-- (1)To run for a entire DB:: expdp.sh server1 mydb pguserid 5432 | |
#--GEO--C-- (2)To run for a entire schema call by schema names separated by commas | |
# :: bash expdp.sh server1 mydb pguserid 5432 l2l,gmm | |
#--GEO--C-- Script when run creates 3 files for case(1) | |
# File 1: Dump file w/ format serverName_dbName_ALL_%m%d%Y_%H%M%S.dump; where under $HOME folder; ALL means all schemas in db | |
# File 2: log/out file w/ format serverName_dbName_ALL_%m%d%Y_%H%M%S.out; script grep the log files for any errors/ failures | |
# File 3: serverName_dbName_ALL_.txt; this has the lastest dump file name; thought process is to use this file to refresh(see impdp.sh) the stage/dev on a nighly basis; ssuming the DB is not huge | |
#--GEO--C-- Script when run creates 3 files for case(2) | |
# there will be 3 files(dump, out, name) for each schema for example if you run expdp.sh server1 mydb pguserid 5432 l2l,gmm | |
# there will be server1_mydb_l2l_06152017_200410.dump,server1_mydb_l2l_06152017_200410.out, server1_mydb_l2l_.txt | |
# there will be server1_mydb_gmm_06152017_200410.dump,server1_mydb_gmm_06152017_200410.out, server1_mydb_gmm_.txt | |
############################################################################################# | |
echo "To run this script supply hostname, dbname, user, port, Scheam " | |
echo "E.g int201(host) catissuedb(db) postgres(user) (5432)port (schema1[;schema2]) schema " | |
echo "-------------------------------------------------------------- " | |
############################################################################################# | |
if [ -z "$1" ] | |
then echo "Paramter %1(host) is empty"; | |
exit 1 | |
else | |
bash hh $1; | |
#if [ $count -eq 0 ] | |
# then exit 1 | |
# else | |
if [ -z "$2" ] | |
then echo "Paramter %2(db) is empty" | |
exit 1 | |
else | |
if [ -z "$3" ] | |
then echo "Paramter %3(user) is empty" | |
exit 1 | |
else | |
if [ -z "$4" ] | |
then echo "Paramter %4(port) is empty" | |
exit 1 | |
else | |
if [ -z "$5" ] | |
then echo "Paramter %5(schema) is empty. So will run for whole DB:$2" | |
fi | |
fi | |
fi | |
# fi | |
fi | |
fi | |
############################################################################################# | |
# Set Variables | |
frmhost=$1 | |
frmdb=$2 | |
frmschema=$5 | |
user=$3 | |
port=$4 | |
export frmconn="-h $frmhost -d $frmdb -p $port -U $user" | |
############################################################################################# | |
#now call expdp recursiverly after splitting the schemas | |
# Split the string by delimter | |
declare -a myarr=(`echo $frmschema |sed 's/,/ /g'`) | |
ischemacnt=${#myarr[@]} | |
#filepath=$HOME | |
echo "Files generated @ : ${PWD}" | |
filepath=${PWD} | |
#### write to file the dump file location for restore touse | |
if [ -z "$5" ] | |
then | |
# no schema so run for all Db | |
dumpfilelocation=$filepath"/"$frmhost"_"$frmdb"_ALL_" | |
frmoutfile=$dumpfilelocation$(date +"%m%d%Y_%H%M%S") | |
echo $frmoutfile>$dumpfilelocation".txt" | |
pg_dump $frmconn -f $frmoutfile".dump" -c -C -F custom -v 2> $frmoutfile".out" | |
else | |
for ((i=0;i<$ischemacnt;i++)) | |
do | |
# here call expdp | |
echo " executing schema: "${myarr[$i]}" task $i of $ischemacnt." | |
echo "--------------------------------------------------------" | |
# ./expdp.sh $frmhost $frmdb $user $port ${myarr[$i]} | |
# frmoutfile=$filepath"/"$frmhost"_"$frmdb"_"${myarr[$i]}"_"$(date +"%m%d%Y_%H%M%S") | |
dumpfilelocation=$filepath"/"$frmhost"_"$frmdb"_"${myarr[$i]}"_" | |
frmoutfile=$dumpfilelocation$(date +"%m%d%Y_%H%M%S") | |
echo $frmoutfile>$dumpfilelocation".txt" | |
pg_dump $frmconn -f $frmoutfile".dump" -c -C -n ${myarr[$i]} -F custom -v 2> $frmoutfile".out" | |
echo "Finished with" $(cat $frmoutfile".out"| grep -i 'warning\|error\|FATAL\|fail' | wc -l ) " errors." | |
echo "Dump file:" $frmoutfile".dump" | |
echo "Log file:" $frmoutfile".out" | |
echo "---------------------------------------------------------" | |
done | |
fi | |
############################################################################################# | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment