# shellcheck shell=sh# lib/backup.sh — backup / restore / reset / destroy for the encrypted DB.# Backups are gzip copies of the already-encrypted file. Depends on config.sh,# ui.sh, db.sh, target.sh.# backup_create -> integrity-check, gzip-copy to a timestamped file, prune old.backup_create(){ db_require
pe_dirs_ensure
pe_info "verifying database integrity..." db_integrity || pe_die "integrity check failed (wrong key or corrupt DB) — backup aborted"_ts=$(date +%Y%m%d-%H%M%S)_out="$PE_BACKUP_DIR/pentest-$_ts.db.gz" gzip -c "$PE_DB" > "$_out"|| pe_die "backup write failed" chmod 600"$_out" pe_ok "backup created: $_out ($(wc -c < "$_out") bytes)" backup__prune
}# keep only the newest $PE_BACKUP_RETENTION pentest-*.db.gz filesbackup__prune(){_n=$(ls -1t "$PE_BACKUP_DIR"/pentest-*.db.gz 2>/dev/null | wc -l | tr -d ' ')["$_n" -gt "$PE_BACKUP_RETENTION"]||return0 ls -1t "$PE_BACKUP_DIR"/pentest-*.db.gz 2>/dev/null \ | tail -n +"$((PE_BACKUP_RETENTION+1))"\ | whileIFS=read -r _f; do rm -f "$_f"&& pe_info "pruned old backup: $(basename "$_f")"; done}# backup_restore [--file PATH] -> pick a backup (fzf or numbered), snapshot the# current DB, verify a staged candidate, then atomically swap it in.backup_restore(){_pick=""if["${1:-}"= --file ]; then pe_require_value "$@"_pick=$2shift2fi[ -d "$PE_BACKUP_DIR"]|| pe_die "no backups directory: $PE_BACKUP_DIR"if[ -z "$_pick"]; then_list=$(ls -1t "$PE_BACKUP_DIR"/pentest-*.db.gz 2>/dev/null)[ -n "$_list"]|| pe_die "no backups found in $PE_BACKUP_DIR"ifcommand -v fzf >/dev/null 2>&1&&[ -t 0]; then_pick=$(printf'%s\n'"$_list" | fzf --prompt='restore> ' --height=40% --reverse)else pe_info "available backups (newest first):"printf'%s\n'"$_list" | nl -w2 -s' ' >&2printf'number to restore: ' >&2; read -r _n </dev/tty
_pick=$(printf'%s\n'"$_list" | sed -n "${_n}p")fifi[ -n "$_pick"]&&[ -f "$_pick"]|| pe_die "no backup selected" pe_confirm_destructive "RESTORE"\"Overwrite the live database with $(basename "$_pick"). Current DB will be snapshotted first."\||{ pe_warn "aborted"; return1; } pe_dirs_ensure || pe_die "could not prepare pentest-env directories"_candidate=$(mktemp "$PE_DATA_DIR/.pentest-restore.XXXXXX")\|| pe_die "could not create a private restore staging file"if ! gunzip -c "$_pick" > "$_candidate"; then rm -f "$_candidate" pe_die "backup archive is unreadable; live DB unchanged"fi chmod 600"$_candidate"# Validate against the configured key before the candidate can replace the# live database. db_integrity reads PE_DB, so point it at the staged file# only for this check and restore the exported path immediately afterwards._live_db=$PE_DBPE_DB=$_candidateexport PE_DB
if db_integrity; then_candidate_ok=1; else_candidate_ok=0; fiPE_DB=$_live_dbexport PE_DB
if["$_candidate_ok" !=1]; then rm -f "$_candidate" pe_die "backup failed integrity check (wrong key or corrupt archive); live DB unchanged"fiif[ -f "$PE_DB"]; then_safe="$PE_BACKUP_DIR/pre-restore-$(date +%Y%m%d-%H%M%S)-$$.db.gz"if ! gzip -c "$PE_DB" > "$_safe"; then rm -f "$_safe""$_candidate" pe_die "could not snapshot the current DB; restore aborted"fi chmod 600"$_safe" pe_info "current DB snapshotted to $(basename "$_safe")"fi mv -f "$_candidate""$PE_DB"||{ rm -f "$_candidate" pe_die "restore swap failed; live DB unchanged"} chmod 600"$PE_DB"# A restored database may assign an old numeric ID to a different target.# Force an explicit `pe target use` so prompt/tmux cache and query scope can# never disagree after the swap. rm -f "$PE_ACTIVE_TARGET_FILE""$PE_ACTIVE_DISPLAY_FILE" pe_ok "restored from $(basename "$_pick")"}# backup_reset -> wipe every row but keep the schema. Double-confirmed.backup_reset(){ db_require
pe_warn "auto-snapshotting before reset" backup_create
pe_confirm_destructive "RESET"\"Delete ALL rows from every table (targets, hosts, services, credentials, flags, notes). Schema kept."\||{ pe_warn "aborted"; return1; } db_exec "DELETE FROM notes; DELETE FROM flags; DELETE FROM credentials; DELETE FROM services; DELETE FROM hosts; DELETE FROM targets; DELETE FROM sqlite_sequence; VACUUM;"||return1 rm -f "$PE_ACTIVE_TARGET_FILE""$PE_ACTIVE_DISPLAY_FILE" pe_ok "database reset to empty schema"}# backup_destroy -> remove the database file entirely. Double-confirmed.backup_destroy(){ db_exists || pe_die "no database to destroy at $PE_DB" pe_confirm_destructive "DESTROY"\"Permanently delete the database file $PE_DB. Backups in $PE_BACKUP_DIR are kept."\||{ pe_warn "aborted"; return1; } rm -f "$PE_DB""$PE_ACTIVE_TARGET_FILE""$PE_ACTIVE_DISPLAY_FILE" pe_ok "database destroyed (backups retained — 'pe restore' to recover)"}