#!/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 if [[ -r /etc/mkosibuild ]]; then . /etc/mkosibuild crondir="$(echo "$0" | awk -F'/' '{print $3}')" if [[ "${crondir}" == "cron.hourly" ]] && [[ -n "${HOURLY_MACHINES+x}" ]]; then MACHINES=("${HOURLY_MACHINES[*]}") elif [[ "${crondir}" == "cron.daily" ]] && [[ -n "${DAILY_MACHINES+x}" ]]; then MACHINES=("${DAILY_MACHINES[*]}") elif [[ "${crondir}" == "cron.weekly" ]] && [[ -n "${WEEKLY_MACHINES+x}" ]]; then MACHINES=("${WEEKLY_MACHINES[*]}") elif [[ "${crondir}" == "cron.monthly" ]] && [[ -n "${MONTHLY_MACHINES+x}" ]]; then MACHINES=("${MONTHLY_MACHINES[*]}") fi fi # Check if logdir exists, and create it if not [[ -d "${LOGDIR}" ]] || mkdir "${LOGDIR}" chmod 750 "${LOGDIR}" LOG="${LOGDIR}/mkosibuild.log" touch "${LOG}" && chmod 640 "${LOG}" for machine in ${MACHINES[*]}; do BUILDLOG="${LOGDIR}/${machine}.buildlog.$(date +%Y%m%d%H%M%S)" touch "${BUILDLOG}" && chmod 640 "${BUILDLOG}" #exec >> "${LOG}" echo "" >> "${LOG}" echo "$(date '+%Y-%m-%d %H:%M:%S') - Building $machine" >> "${LOG}" ${EXEC} "$machine" &>> "${BUILDLOG}" && echo "$(date '+%Y-%m-%d %H:%M:%S') - Finished $machine" >> "${LOG}" # Delete old backups find "${BACKUPDIR}" -name "${machine}.*${CRONSUFFIX}" | tail -n +$((BACKUPNUM + 1)) | xargs rm -fv -- >> "${LOG}" done