diff options
author | Dennis Eriksen <d@ennis.no> | 2020-03-29 14:29:41 +0200 |
---|---|---|
committer | Dennis Eriksen <d@ennis.no> | 2020-03-29 14:29:41 +0200 |
commit | 10ed5dadd49a74051a1e4ea05bdbd3fe7017bb74 (patch) | |
tree | 0a2443393b4c63ad21dbbf7a8470ae9981f63a49 | |
parent | renaming project (diff) | |
download | mkosibuild-10ed5dadd49a74051a1e4ea05bdbd3fe7017bb74.tar.gz |
contiguing work. In time I want to be able to run this in cron
-rwxr-xr-x | mkosibuild | 51 |
1 files changed, 39 insertions, 12 deletions
@@ -11,6 +11,20 @@ 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 + +# Check if run by cron +if [[ -t 1 ]]; then + CRON=false + SUFFIX="-cron" +else + CRON=true + SUFFIX="" +fi + function die { echo $@ exit 1 @@ -18,23 +32,29 @@ function die { 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 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" cd /srv/mkosi/"${NAME}" - mkosi -o "/var/lib/machines/${NAME}.new" + /bin/mkosi -o "/var/lib/machines/${NAME}.new" # Stop machine if it is running - if machinectl status "${NAME}" >/dev/null; then - machinectl stop "${NAME}" + if /bin/machinectl status "${NAME}" >/dev/null; then + /bin/machinectl stop "${NAME}" + local START=true fi cd /var/lib/machines if [[ -e "${NAME}" ]]; then - mv "${NAME}" .backup/ + mv "${NAME}" "${BACKUPDIR}/" rm -f "${NAME}.nspawn" fi mv "${NAME}.new" "${NAME}" @@ -42,19 +62,26 @@ function main { echo "Done building." echo "" sleep 1 - echo "Starting" - machinectl start "${NAME}" - sleep 2 - machinectl + # Start machine if it was running before + if [[ ${START:=false} = true ]]; then + echo "Starting" + /bin/machinectl start "${NAME}" + sleep 2 + /bin/machinectl + fi + # Backup old machine + [[ -d "${BACKUPDIR}" ]] || mkdir -p "${BACKUPDIR}" if [[ -e ".backup/${NAME}" ]]; then echo "" echo "Now zipping up the old machine, for backup" - cd .backup - tar c "${NAME}" | xz -0 - > "/var/lib/machines/.backup/${NAME}.tar.xz.$(date +%Y%m%d%H%M%S)" + cd "${BACKUPDIR}" + tar c "${NAME}" | xz -0 - > "${BACKUPDIR}/${NAME}.tar.xz.$(date +%Y%m%d%H%M%S)${SUFFIX}" rm -r "${NAME}" - cd .. + + # Delete old backups + ls ${NAME}.*${SUFFIX} | tail -n $((BACKUPNUM + 1)) | xargs rm -- fi } |