aboutsummaryrefslogtreecommitdiffstats
path: root/mkosibuild-cron
blob: 8b45a1ad4ef99dc701ffbf4d55d002de3cdfe9c3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env bash
#
# made by Dennis Eriksen <dnns.no> 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