Skip to content

Commit 1cb3cf7

Browse files
authored
Merge pull request #4 from buildplan/improve-restore
Improve restore with less
2 parents 5ac4139 + 9659e9e commit 1cb3cf7

File tree

2 files changed

+40
-22
lines changed

2 files changed

+40
-22
lines changed

restic-backup.sh

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
#!/bin/bash
22

33
# =================================================================
4-
# Restic Backup Script v0.20 - 2025.09.09
4+
# Restic Backup Script v0.21 - 2025.09.09
55
# =================================================================
66

77
set -euo pipefail
88
umask 077
99

1010
# --- Script Constants ---
11-
SCRIPT_VERSION="0.20"
11+
SCRIPT_VERSION="0.21"
1212
SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
1313
CONFIG_FILE="${SCRIPT_DIR}/restic-backup.conf"
1414
LOCK_FILE="/tmp/restic-backup.lock"
@@ -723,12 +723,12 @@ run_restore() {
723723
return 1
724724
fi
725725

726-
# Offer to list snapshot contents to help find paths
726+
# Offer to list snapshot contents
727727
local list_confirm
728728
read -p "Would you like to list the contents of this snapshot to find exact paths? (y/n): " list_confirm
729729
if [[ "${list_confirm,,}" == "y" || "${list_confirm,,}" == "yes" ]]; then
730730
echo -e "${C_DIM}Displaying snapshot contents (use arrow keys to scroll, 'q' to quit)...${C_RESET}"
731-
restic ls -l "$snapshot_id" | less
731+
less -f <(restic ls -l "$snapshot_id")
732732
fi
733733

734734
# Get restore destination
@@ -741,10 +741,7 @@ run_restore() {
741741
# Ask for specific paths to include
742742
local include_paths=()
743743
read -p "Optional: Enter specific file(s) to restore, separated by spaces (leave blank for full restore): " -a include_paths
744-
745744
local restic_cmd=(restic restore "$snapshot_id" --target "$restore_dest" --verbose)
746-
747-
# Add --include flags if paths were provided
748745
if [ ${#include_paths[@]} -gt 0 ]; then
749746
for path in "${include_paths[@]}"; do
750747
restic_cmd+=(--include "$path")
@@ -760,30 +757,55 @@ run_restore() {
760757
fi
761758
echo -e "${C_BOLD}--- Dry Run Complete ---${C_RESET}"
762759

763-
# Ask for final confirmation before proceeding
760+
# Ask for final confirmation
764761
local proceed_confirm
765762
read -p "Proceed with the actual restore? (y/n): " proceed_confirm
766763
if [[ "${proceed_confirm,,}" != "y" && "${proceed_confirm,,}" != "yes" ]]; then
767764
echo "Restore cancelled by user."
768765
return 0
769766
fi
770767

771-
# Create destination if it doesn't exist
768+
# Create destination if it doesn't exist and perform the restore
772769
mkdir -p "$restore_dest"
773-
774-
# Perform the actual restore
775770
echo -e "${C_BOLD}--- Performing Restore ---${C_RESET}"
776771
log_message "Restoring snapshot $snapshot_id to $restore_dest"
777772

778-
if "${restic_cmd[@]}"; then
773+
# Restore Logic
774+
local restore_log
775+
restore_log=$(mktemp)
776+
local restore_success=false
777+
778+
if "${restic_cmd[@]}" 2>&1 | tee "$restore_log"; then
779+
restore_success=true
780+
fi
781+
cat "$restore_log" >> "$LOG_FILE"
782+
783+
# Handle failure of the restic command
784+
if [ "$restore_success" = false ]; then
785+
log_message "ERROR: Restore failed"
786+
echo -e "${C_RED}❌ Restore failed${C_RESET}" >&2
787+
send_notification "Restore FAILED: $HOSTNAME" "x" \
788+
"${NTFY_PRIORITY_FAILURE}" "failure" "Failed to restore $snapshot_id"
789+
rm -f "$restore_log"
790+
return 1
791+
fi
792+
793+
# Check if the restore was successful
794+
if grep -q "Summary: Restored 0 files/dirs" "$restore_log"; then
795+
echo -e "\n${C_YELLOW}⚠️ Restore completed, but no files were restored.${C_RESET}"
796+
echo -e "${C_YELLOW}This usually means the specific path(s) you provided do not exist in this snapshot.${C_RESET}"
797+
echo "Please try the restore again and use the 'list contents' option to verify the exact path."
798+
log_message "Restore completed but restored 0 files (path filter likely found no match)."
799+
send_notification "Restore Notice: $HOSTNAME" "information_source" \
800+
"${NTFY_PRIORITY_SUCCESS}" "warning" "Restore of $snapshot_id completed but 0 files were restored. The specified path filter may not have matched any files in the snapshot."
801+
else
779802
log_message "Restore completed successfully"
780803
echo -e "${C_GREEN}✅ Restore completed${C_RESET}"
781804

782-
# Set file ownership
805+
# Set file ownership logic
783806
if [[ "$restore_dest" == /home/* ]]; then
784807
local dest_user
785808
dest_user=$(echo "$restore_dest" | cut -d/ -f3)
786-
787809
if [[ -n "$dest_user" ]] && id -u "$dest_user" &>/dev/null; then
788810
echo -e "${C_CYAN}ℹ️ Home directory detected. Setting ownership of restored files to '$dest_user'...${C_RESET}"
789811
if chown -R "${dest_user}:${dest_user}" "$restore_dest"; then
@@ -795,16 +817,12 @@ run_restore() {
795817
fi
796818
fi
797819
fi
798-
799820
send_notification "Restore SUCCESS: $HOSTNAME" "white_check_mark" \
800821
"${NTFY_PRIORITY_SUCCESS}" "success" "Restored $snapshot_id to $restore_dest"
801-
else
802-
log_message "ERROR: Restore failed"
803-
echo -e "${C_RED}❌ Restore failed${C_RESET}" >&2
804-
send_notification "Restore FAILED: $HOSTNAME" "x" \
805-
"${NTFY_PRIORITY_FAILURE}" "failure" "Failed to restore $snapshot_id"
806-
return 1
807822
fi
823+
824+
# Clean up the temporary log file
825+
rm -f "$restore_log"
808826
}
809827

810828
# =================================================================

restic-backup.sh.sha256

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
c0a7ffdc8f433e992040947a9cb22285d7ee94be4f210ac5a54b1ee6751d89b1 restic-backup.sh
1+
63a9d3319cd1b3ade34bfc62c8ea832baae438a4ca512f6993f9d1ed2fce2f49 restic-backup.sh

0 commit comments

Comments
 (0)