Last active
July 28, 2016 14:04
-
-
Save matgou/e57ce6a8e7ac2e17ea94aa7e70928333 to your computer and use it in GitHub Desktop.
Backup of postgres
This file contains hidden or 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
#!/bin/bash | |
#@**************************************************************************** | |
#@ Author : Mathieu GOULIN ([email protected]) | |
#@ Organization : Gadz.org (www.gadz.org) | |
#@ Licence : GNU/GPL | |
#@ | |
#@ Description : | |
#@ | |
#@ Prerequisites : | |
#@ Arguments : | |
#@ | |
#@**************************************************************************** | |
#@ History : | |
#@ - Mathieu GOULIN - 2015/12/26 : Initialisation du script | |
#@**************************************************************************** | |
# Static configuration | |
cd `dirname $0` | |
script=`basename "$0" | cut -f1 -d"."` | |
log_file=`pwd`/$script.log | |
# Usage function | |
function usage () { | |
# TODO - Write the good stuff here... | |
echo "$0 [start|stop|restart]" | |
} | |
# Help function | |
function help () { | |
usage | |
echo | |
grep -e "^#@" $script.sh | sed "s/^#@//" | |
} | |
# Log function | |
write_log () { | |
log_state=$1 | |
shift; | |
log_txt=$* | |
log_date=`date +'%Y/%m/%d %H:%M:%S'` | |
case ${log_state} in | |
BEG) chrtra="[START]" ;; | |
CFG) chrtra="[CONF ERR]" ;; | |
ERR) chrtra="[ERROR]" ;; | |
END) chrtra="[END]" ;; | |
INF) chrtra="[INFO]" ;; | |
*) chrtra="[ - ]" ;; | |
esac | |
echo "$log_date $chrtra : ${log_txt}" | tee -a ${log_file} 2>&1 | |
} | |
test_rc () { | |
if [ "x$1" != "x0" ] | |
then | |
echo $backup_sql_stop | psql -h /appli/postgres/${instancename}/run/ -v ON_ERROR_STOP=1 -p ${pg_port} -U postgres postgres | |
write_log ERR "Erreur RC=$1" | |
exit $1 | |
fi | |
} | |
. `dirname $0`/../config/config.sh | |
TH=`date +%Y%m%d%H%M%S` | |
backup_sql_start="SELECT pg_start_backup('${instancename}_backup_${TH}', true); SELECT pg_switch_xlog();" | |
backup_sql_stop="SELECT pg_stop_backup();" | |
echo $backup_sql_start | psql -h /appli/postgres/${instancename}/run/ -v ON_ERROR_STOP=1 -p ${pg_port} -U postgres postgres | |
test_rc $? | |
cd /appli/postgres/${instancename}/ | |
tar -czvf data/backup_$instancename_$TH.tar.gz postgres-archivelog postgres-datafile --exclude="postgres-datafile/pg_xlog" | |
test_rc $? | |
echo $backup_sql_stop | psql -h /appli/postgres/${instancename}/run/ -v ON_ERROR_STOP=1 -p ${pg_port} -U postgres postgres | |
test_rc $? | |
cd /appli/postgres/${instancename}/data/ | |
ls -tr | head -n $((`ls -tr | wc -l` - 5)) | xargs rm -rfv | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment