#!/bin/sh # Roll back ~/bin/forge to ~/bin/forge.prev after a unit failed. # Usage: forge-rollback . Output goes to the journal. set -eu unit="${1:?usage: forge-rollback }" bin="$HOME/bin/forge" prev="$bin.prev" bad="$bin.bad" if [ ! -e "$prev" ]; then echo "forge-rollback: $unit failed and $prev does not exist, nothing to roll back" exit 0 fi sha=$(sha256sum "$bin" | cut -d' ' -f1) # Keep one hash per line, even if the file was edited by hand without a final newline. if [ -s "$bad" ] && [ -n "$(tail -c 1 "$bad")" ]; then echo >> "$bad" fi echo "$sha" >> "$bad" mv -f "$prev" "$bin" echo "forge-rollback: $unit failed, marked $sha bad, restored $prev" systemctl --user reset-failed "$unit" systemctl --user start "$unit" echo "forge-rollback: started $unit on the previous binary"