basforge

Search titles, text and the “If it doesn’t work” cases.

    RU

    An edit script with automatic rollback

    An edit script makes a copy, edits, checks, and if the check fails, puts the copy back by itself. Copies go to /root and are deleted at the end.

    The script

    An edit that shouldn’t change the final config (moving a value into .env, say) is checked with the config hash (The compose config hash):

    (
    cd /opt/app || exit 1
    before=$(docker compose config --hash '*'); [ -n "$before" ] || exit 1
    cp -p docker-compose.yaml /root/app.compose.bak && cp -p .env /root/app.env.bak || exit 1
    # … the edit …
    if [ "$before" = "$(docker compose config --hash '*')" ]; then echo "OK: config unchanged"
    else cp /root/app.compose.bak docker-compose.yaml && cp /root/app.env.bak .env; echo "MISMATCH: restored"; fi
    rm -f /root/app.compose.bak /root/app.env.bak
    )

    The parentheses ( … ) make a subshell: an exit inside won’t close your SSH session.

    Copies go to /root, not /tmp: every user on the server can read /tmp, and .env holds secrets.

    The output has one of two lines: OK or MISMATCH. If neither shows up, the script stopped earlier, at one of the || exit 1 checks, and changed nothing.