Allow rolling backups to a limit

When `BACKUP_COUNT` is specified on the backup container, a backup job will ensure we keep the newest `BACKUP_COUNT` backups to preserve space in our target volume.
This commit is contained in:
Ben Langfeld 2019-07-11 15:56:07 -03:00 committed by GitHub
parent 6fffcb6754
commit 6a69cf5c88
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 6 additions and 1 deletions

View File

@ -6,6 +6,11 @@ set -eo pipefail
[[ -z "${BACKUP_DIR}" ]] && echo "Required 'BACKUP_DIR' env not set" && exit 1;
[[ -z "${JENKINS_HOME}" ]] && echo "Required 'JENKINS_HOME' env not set" && exit 1;
if [[ ! -z "${BACKUP_COUNT}" ]]; then
echo "Trimming to only $((BACKUP_COUNT-1)) recent backups in preparation for new backup"
ls -1t ${BACKUP_DIR} | tail -n +${BACKUP_COUNT} | xargs -I '{}' rm -f ${BACKUP_DIR}/'{}'
fi
backup_number=$1
echo "Running backup"
@ -14,4 +19,4 @@ tar -C ${JENKINS_HOME} -czf "${BACKUP_DIR}/${backup_number}.tar.gz" --exclude jo
[[ ! -s ${BACKUP_DIR}/${backup_number}.tar.gz ]] && echo "backup file '${BACKUP_DIR}/${backup_number}.tar.gz' is empty" && exit 1;
echo Done
exit 0
exit 0