You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
prodwork01-mobene-deployment/templates/cm_pg_backup_scripts.yaml

112 lines
3.6 KiB
YAML

apiVersion: v1
data:
backup_retry.sh: |
#!/bin/bash
#
#
BASENAME=$(basename $0)
function log
{
touch /tmp/${BASENAME}.log
echo "$(date "+%Y-%m-%d %H:%M:%S.%3N") - $0 - $*" >> /tmp/${BASENAME}.log
}
log "INFO - script was executed"
LAST_BKP=$(envdir "/run/etc/wal-e.d/env" wal-g backup-list --detail --json | jq -r .[-1].finish_time)
LAST_BKP_DATE_IN_UNIXSEC=$(date -d ${LAST_BKP} +"%s")
NOW=$(date +%s -u)
if [ $((NOW-LAST_BKP_DATE_IN_UNIXSEC)) -lt 86400 ]; then
log "INFO - last backup created within 24h. no backup rescheduling needed"
exit 0
else
log "INFO - last backup created older than 24h. triggering backup..."
envdir "/run/etc/wal-e.d/env" /scripts/postgres_backup.sh "/home/postgres/pgdata/pgroot/data"
fi
postgres_backup.sh: |
#!/bin/bash
echo "Plz check DEBUG_LOG for debugging purpose. EVERY output will be redirected!"
# fgoerz DEV-1029
# pipe all output to file for debugging purpose
exec 3>&1
exec 1>>/tmp/pg_backup_`date +%F`.log
exec 2>&1
function log
{
echo "$(date "+%Y-%m-%d %H:%M:%S.%3N") - $0 - $*"
}
[[ -z $1 ]] && echo "Usage: $0 PGDATA" && exit 1
log "I was called as: $0 $*"
readonly PGDATA=$1
DAYS_TO_RETAIN=$BACKUP_NUM_TO_RETAIN
readonly IN_RECOVERY=$(psql -tXqAc "select pg_is_in_recovery()")
if [[ $IN_RECOVERY == "f" ]]; then
[[ "$WALG_BACKUP_FROM_REPLICA" == "true" ]] && log "Cluster is not in recovery, not running backup" && exit 0
elif [[ $IN_RECOVERY == "t" ]]; then
[[ "$WALG_BACKUP_FROM_REPLICA" != "true" ]] && log "Cluster is in recovery, not running backup" && exit 0
else
log "ERROR: Recovery state unknown: $IN_RECOVERY" && exit 1
fi
# leave at least 2 days base backups before creating a new one
[[ "$DAYS_TO_RETAIN" -lt 2 ]] && DAYS_TO_RETAIN=2
if [[ "$USE_WALG_BACKUP" == "true" ]]; then
readonly WAL_E="wal-g"
[[ -z $WALG_BACKUP_COMPRESSION_METHOD ]] || export WALG_COMPRESSION_METHOD=$WALG_BACKUP_COMPRESSION_METHOD
export PGHOST=/var/run/postgresql
else
readonly WAL_E="wal-e"
# Ensure we don't have more workes than CPU's
POOL_SIZE=$(grep -c ^processor /proc/cpuinfo 2>/dev/null || 1)
[ "$POOL_SIZE" -gt 4 ] && POOL_SIZE=4
POOL_SIZE=(--pool-size "$POOL_SIZE")
fi
BEFORE=""
LEFT=0
readonly NOW=$(date +%s -u)
while read -r name last_modified rest; do
last_modified=$(date +%s -ud "$last_modified")
if [ $(((NOW-last_modified)/86400)) -ge $DAYS_TO_RETAIN ]; then
if [ -z "$BEFORE" ] || [ "$last_modified" -gt "$BEFORE_TIME" ]; then
BEFORE_TIME=$last_modified
BEFORE=$name
fi
else
# count how many backups will remain after we remove everything up to certain date
((LEFT=LEFT+1))
fi
done < <($WAL_E backup-list 2> /dev/null | sed '0,/^name\s*\(last_\)\?modified\s*/d')
# we want keep at least N backups even if the number of days exceeded
if [ ! -z "$BEFORE" ] && [ $LEFT -ge $DAYS_TO_RETAIN ]; then
if [[ "$USE_WALG_BACKUP" == "true" ]]; then
$WAL_E delete before FIND_FULL "$BEFORE" --confirm
else
$WAL_E delete --confirm before "$BEFORE"
fi
fi
# push a new base backup
log "producing a new backup"
# We reduce the priority of the backup for CPU consumption
exec nice -n 5 $WAL_E backup-push "$PGDATA" "${POOL_SIZE[@]}" 2>&3
kind: ConfigMap
metadata:
name: pg-backup-script