Skip to content

Instantly share code, notes, and snippets.

@marcodebe
Last active July 5, 2019 15:26
Show Gist options
  • Save marcodebe/84ee019ca5831dce653d2a992362aa76 to your computer and use it in GitHub Desktop.
Save marcodebe/84ee019ca5831dce653d2a992362aa76 to your computer and use it in GitHub Desktop.
Backup via pg_dump{,all} of all clusters defined in barman
#!/bin/bash -eu
# Uses connection strings defined in /etc/barman.d/*.conf
# to dump each single database and global objects
# do not run multiple instances
[ "${FLOCKER:-''}" != "$0" ] && exec env FLOCKER="$0" flock -en "$0" "$0" "$@" || :
BASE=/var/lib/barman/dumps
VERBOSE=0
QUERYDB="select datname from pg_database where datallowconn
order by pg_database_size(oid) desc"
# Delete old backups
MAXAGE=3
dbdump() {
SERVER=$1
conninfo=$2
eval ${conninfo}
outputdir="${BASE}/${host}/$(date +%Y%m%d)"
mkdir -p ${outputdir}
for dbname in $(dblist)
do
printf "Creating ${outputdir}/${dbname}.dump "
# Just one pg_dump at a time per cluster
if [ ! -z ${pids[${SERVER}]+x} ]
then
printf "waiting for ${pids[${SERVER}]} "
wait ${pids[${SERVER}]}
fi
pg_dump \
--format=custom \
--host=${host} \
--port=${port:-5432} \
--dbname=${dbname} \
--user=${user} > ${outputdir}/${dbname}.dump &
pids[${SERVER}]=$!
echo "started with pid ${pids[${SERVER}]}"
done
}
globals() {
conninfo=$1
eval ${conninfo}
outputdir="${BASE}/${host}/$(date +%Y%m%d)"
mkdir -p ${outputdir}
echo ${outputdir}/globals.dump
pg_dumpall \
--globals-only \
--host=${host} \
--port=${port:-5432} \
--user=${user} > ${outputdir}/globals.dump
}
dblist() {
psql \
--host=${host} \
--port=${port:-5432} \
--dbname=${dbname} \
--user=${user} \
-t -A -c "${QUERYDB}"
}
removeold() {
conninfo=$1
eval ${conninfo}
olddir="${BASE}/${host}/$(date -d 'now -1 day -'$MAXAGE' days' +%Y%m%d)"
if [ -d ${olddir} ]
then
echo rm -f "${olddir}/*.dump"
echo rmdir "${olddir}"
fi
}
SERVER=0
for server in /etc/barman.d/*.conf
do
conninfo=$(grep "^conninfo" $server | sed 's,conninfo = ,,')
globals "${conninfo}"
dbdump $SERVER "${conninfo}" &
SERVER=$(( SERVER + 1 ))
#removeold "${conninfo}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment