aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDennis Eriksen <d@ennis.no>2020-04-17 09:24:59 +0200
committerDennis Eriksen <d@ennis.no>2020-04-17 09:24:59 +0200
commitb5f5ecfcff3ca73264b2290fbe698b9cb8f1c56d (patch)
tree5e5248b7559adb250b20f90056f37558abde03c2
parentadding pv progress to the backupjob (diff)
downloadmkosibuild-b5f5ecfcff3ca73264b2290fbe698b9cb8f1c56d.tar.gz
adding wrapper-script for cron
-rwxr-xr-xmkosibuild32
-rwxr-xr-xmkosibuild-cron44
2 files changed, 50 insertions, 26 deletions
diff --git a/mkosibuild b/mkosibuild
index 860c538..e086365 100755
--- a/mkosibuild
+++ b/mkosibuild
@@ -12,34 +12,17 @@ set -o nounset
set -o pipefail
-LOGDIR=/var/log/mkosibuild/
-BACKUPDIR=/var/lib/machines/.backup
-BACKUPNUM=5
-
-# Check if run by cron
-if [[ -t 1 ]]; then
- CRON=false
- SUFFIX="-cron"
-else
- CRON=true
- SUFFIX=""
-fi
+: "${BACKUPDIR:=/var/lib/machines/.backup}"
function die {
- echo $@
+ echo "$@"
exit 1
}
function main {
-
- # Check if logdir exists, and create it if not
- [[ -d "${LOGDIR}" ]] || mkdir "${LOGDIR}"
-
# Check if $1 exists
[[ "${1:-}" == "" ]] && die "You need to set the name of the machine you want to rebuild."
local NAME="${1}"
- local LOG="${LOGDIR}/${NAME}"
- if [[ ${CRON} = true ]]; then exec >> "$LOG"; exec 2>&1; fi
# Check if mkosi-template exists, and cd into it if it does
[[ -d /srv/mkosi/${NAME} ]] || die "/srv/mkosi/${NAME} does not exist"
@@ -77,17 +60,14 @@ function main {
echo ""
echo "Now zipping up the old machine, for backup"
cd "${BACKUPDIR}"
- if [[ ${CRON} = false ]] && [[ -x "$(command -v pv)" ]]; then
- tar c "${NAME}" | pv -s "$(du -sb "${NAME}" | awk '{print $1}')" | xz -0 - > "${BACKUPDIR}/${NAME}.tar.xz.$(date +%Y%m%d%H%M%S)${SUFFIX}"
+ if [[ "${CRON:=false}" = false ]] && [[ -x "$(command -v pv)" ]]; then
+ tar c "${NAME}" | pv -s "$(du -sb "${NAME}" | awk '{print $1}')" | xz -0 - > "${BACKUPDIR}/${NAME}.tar.xz.$(date +%Y%m%d%H%M%S)"
else
- tar c "${NAME}" | xz -0 - > "${BACKUPDIR}/${NAME}.tar.xz.$(date +%Y%m%d%H%M%S)${SUFFIX}"
+ tar c "${NAME}" | xz -0 - > "${BACKUPDIR}/${NAME}.tar.xz.$(date +%Y%m%d%H%M%S)${CRONSUFFIX:=-cron}"
fi
rm -r "${NAME}"
-
- # Delete old backups
- ls ${NAME}.*${SUFFIX} | tail -n $((BACKUPNUM + 1)) | xargs rm --
fi
}
-main $@
+main "$@"
diff --git a/mkosibuild-cron b/mkosibuild-cron
new file mode 100755
index 0000000..8b45a1a
--- /dev/null
+++ b/mkosibuild-cron
@@ -0,0 +1,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