#!/usr/bin/env bash # Save slot and VM snapshot management for Sysadmin Chronicles. # # Usage: # manage-saves.sh Interactive menu # manage-saves.sh --list List save slots (non-interactive) # manage-saves.sh --reset Reset current save # manage-saves.sh --reset slot-1 Reset a specific slot set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)" source "$PROJECT_ROOT/tools/lib/ui.sh" source "$PROJECT_ROOT/tools/lib/config.sh" source "$PROJECT_ROOT/tools/lib/save.sh" source "$PROJECT_ROOT/tools/lib/libvirt.sh" source "$PROJECT_ROOT/tools/lib/vm.sh" config_read || true export LIBVIRT_DEFAULT_URI="${SC_LIBVIRT_URI:-${LIBVIRT_DEFAULT_URI:-qemu:///system}}" # Non-interactive flags for arg in "$@"; do case "$arg" in --list) save_list exit 0 ;; esac done if [[ "${1:-}" == "--reset" ]]; then slot="${2:-}" if [ -n "$slot" ]; then save_reset "$slot" else save_reset fi exit 0 fi # --------------------------------------------------------------------------- # Interactive menu # --------------------------------------------------------------------------- declare -A VM_LABEL=( [sc-workstation]="workstation (ares)" [sc-web-server]="web server (hermes)" [sc-build-machine]="build server (vulcan)" ) ALL_VMS=(sc-workstation sc-web-server sc-build-machine) sc_header "SYSADMIN CHRONICLES — SAVE MANAGEMENT" _main_menu() { while true; do echo "" save_list echo "" echo " ── Save Actions ─────────────────────────────" echo " s) Switch active save slot" echo " n) New save slot" echo " r) Reset a save slot" echo " e) Export save to file" echo " i) Import save from file" echo "" echo " ── VM Snapshots ─────────────────────────────" echo " v) View and manage VM snapshots" echo "" echo " q) Quit" echo "" printf " > " >/dev/tty read -r choice " >/dev/tty read -r slot /dev/null | head -1 | cut -d: -f2 || echo '')" if [ -n "$active_day" ] && [ "$active_day" -gt 1 ] 2>/dev/null; then echo "" sc_warn "This save is on Day $active_day. Your VMs may not match this slot's expected state." sc_info "If things look wrong, use 'tools/vm/rebuild-vms.sh' to revert VMs." fi } _new_slot() { echo " Create in which slot? (slot-1 / slot-2 / slot-3)" printf " > " >/dev/tty read -r slot " >/dev/tty read -r slot " >/dev/tty read -r slot " >/dev/tty read -r dest " >/dev/tty read -r src " >/dev/tty read -r slot /dev/null; then snaps="$(virsh snapshot-list "$vm" --name 2>/dev/null | grep -v '^$' || true)" if [ -n "$snaps" ]; then while IFS= read -r snap; do ts="$(virsh snapshot-info "$vm" "$snap" 2>/dev/null | grep 'Creation Time' | awk '{print $3}' || true)" if [[ "$snap" == baseline.* ]] || [[ "$snap" == checkpoint.* ]]; then prot=" [protected]" else prot="" fi printf " %-36s %-12s%s\n" "$snap" "$ts" "$prot" done <<< "$snaps" else echo " (no snapshots)" fi else echo " (VM not found)" fi echo "" done echo " Actions: [t]ake snapshot [r]evert [d]elete [q]uit" echo "" printf " > " >/dev/tty local snap_action read -r snap_action " >/dev/tty read -r snap_name " >/dev/tty read -r snap_name " >/dev/tty read -r snap_name " >/dev/tty read -r _result_ref