#!/usr/bin/env bash # # made by Dennis Eriksen in 2020. # Exit on error. Append "|| true" if you expect an error. set -o errexit # Exit on error inside any functions or subshells. set -o errtrace # Do not allow use of undefined vars. Use ${VAR:-} to use an undefined VAR set -o nounset # Catch the error in case mysqldump fails (but gzip succeeds) in `mysqldump |gzip` set -o pipefail LOGDIR=/var/log/mkosibuild/ BACKUPDIR=/var/lib/machines/.backup BACKUPNUM=5 EXEC=mkosibuild # This provides the exec is in your $PATH CRONSUFFIX="-cron" # Suffix for backups made through cron export CRON=true export CRONSUFFIX MACHINES=() # Import config to override above config [[ -r /etc/mkosibuild ]] && . /etc/mkosibuild # Check if logdir exists, and create it if not [[ -d "${LOGDIR}" ]] || mkdir "${LOGDIR}" for machine in ${MACHINES[*]}; do LOG="${LOGDIR}/${machine}.log" BUILDLOG="${LOGDIR}/${machine}.buildlog.$(date +%Y%m%d%H%M%S)" touch "${LOG}" && chmod 600 "${LOG}" #exec >> "${LOG}" echo "" >> "${LOG}" echo "Starting build $(date +%Y-%m-%d_%H:%M:%S)" >> "${LOG}" ${EXEC} "$machine" &>> "${BUILDLOG}" # Delete old backups find "${BACKUPDIR}" -name "${machine}.*${CRONSUFFIX}" | tail -n +$((BACKUPNUM + 1)) | xargs rm -fv -- >> "${LOG}" done