Nano automatic rollback configuration after a failed upgrade
0 reputation · 13 Jun 2021, 07:19 UTC
0 reputation · 13 Jun 2021, 07:19 UTC
Goal: Configure nano to automatically restore the most recent backup file when a newly upgraded nano binary fails to start or crashes, eliminating the need for manual file copy.
Current behavior: nano creates a single backup with a tilde (~) suffix on each save, but provides no option to trigger a rollback automatically after a failed upgrade; recovery relies on the user having write permission in the file’s directory and manually copying the backup over the active file.
Uncertainty: It is unclear whether a configuration setting could be added to nano to enable this automatic rollback, or if retaining multiple backups would be required to safeguard against intervening saves that overwrite the single tilde file.
Specific questions:
1. Is there a configuration flag for automatic rollback?
No. Nano’s configuration system (via nanorc or command‑line options) only controls backup creation on save (the ~ file) and other editor behaviour. It has no option to restore that backup automatically when the binary fails to start or crashes.
2. Can Nano keep more than one backup?
Not natively. Each time you save a file Nano overwrites the previous ~ backup. There is no built‑in rotation or versioning system.
3. Is an external wrapper the only practical method?
Yes. Without source‑level changes, you must wrap the nano invocation in a script that can detect a failure and copy the ~ file back over the active file. The wrapper can also implement a simple rotation scheme if you need multiple pre‑upgrade states.
The following Bash script demonstrates a minimal wrapper that:
--backup flag (ensures a ~ file is created on every save).~ backup over the original file.# nano-rollback-wrapper.sh
#!/usr/bin/env bash
set -e
# Target file to edit
TARGET="$1"
if [[ -z "$TARGET" ]]; then
echo "Usage: $0 <file>"
exit 1
fi
# Ensure the file exists
if [[ ! -f "$TARGET" ]]; then
echo "File not found: $TARGET"
exit 1
fi
# Create a timestamped backup before launching Nano
TIMESTAMP=$(date +%s)
BASE=$(basename "$TARGET")
DIR=$(dirname "$TARGET")
BACKUP="$DIR/${BASE}.~${TIMESTAMP}"
cp "$TARGET" "$BACKUP"
# Run Nano with backup enabled
nano --backup "$TARGET"
EXIT_CODE=$?
# If Nano failed (non‑zero exit), restore the backup
if [[ $EXIT_CODE -ne 0 ]]; then
echo "Nano exited with code $EXIT_CODE – restoring backup"
cp "$BACKUP" "$TARGET"
# Optional: keep only the most recent backup
rm -f "$DIR/${BASE}.~{1..100}" # adjust as needed
fi
exit $EXIT_CODE
To use it, make the script executable and invoke it instead of nano:
chmod +x nano-rollback-wrapper.sh
./nano-rollback-wrapper.sh /etc/myapp/config.conf
~ backup remains unchanged.kill -9 $PID or send SIGTERM to force a non‑zero exit.nano binary in place, the wrapper will still work because it runs the new binary and can detect failures.To tailor the wrapper to your environment, could you specify which operating system distribution and Nano version you are using? Certain package managers may install Nano with different default options or in different locations, which can affect the wrapper’s path resolution.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.