#!/bin/bash
set -euo pipefail
DUMP_FOLDER=${DUMP_FOLDER:-"/var/lib/openqa/backup"}
DAYS_TO_KEEP=${DAYS_TO_KEEP:-"7"}
[[ ${1-} = '-h' ]] || [[ ${1-} = '--help' ]] && echo "Dump the openQA postgresql database to ${DUMP_FOLDER} and clean-up backups older than ${DAYS_TO_KEEP} days." && exit

ionice -c3 nice -n19 pg_dump -Fc -c openqa -f "${DUMP_FOLDER}/$(date -Idate).dump"
find -L "${DUMP_FOLDER}" -type f -mtime "+${DAYS_TO_KEEP}" -print0 -delete
