Skip to content

Commit f2443eb

Browse files
author
buildplan
committed
improve restic key import for updates and fix shellcheck
1 parent 77a380a commit f2443eb

File tree

1 file changed

+47
-24
lines changed

1 file changed

+47
-24
lines changed

restic-backup.sh

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -47,31 +47,46 @@ fi
4747
# =================================================================
4848

4949
import_restic_key() {
50-
local fpr="CF8F18F2844575973F79D4E191A6868BD3F7A907"
51-
# Check local user keyring
50+
local fpr servers debian_keyring
51+
52+
# Official Fingerprint
53+
fpr="CF8F18F2844575973F79D4E191A6868BD3F7A907"
54+
55+
# 1. Check local user keyring first
5256
if gpg --list-keys "$fpr" >/dev/null 2>&1; then
5357
return 0
5458
fi
55-
# Check Debian/Ubuntu system keyring
56-
local debian_keyring="/usr/share/keyrings/restic-archive-keyring.gpg"
57-
if [[ -f "$debian_keyring" ]]; then
58-
echo "Found debian keyring, checking for key..."
59-
if gpg --no-default-keyring --keyring "$debian_keyring" --list-keys "$fpr" >/dev/null 2>&1; then
60-
echo "Importing trusted key from system keyring..."
61-
gpg --no-default-keyring --keyring "$debian_keyring" --export "$fpr" | gpg --import >/dev/null 2>&1
62-
return $?
63-
fi
59+
60+
echo "Restic PGP key not found. Attempting import..."
61+
62+
# 2. Attempt Direct Download from Restic.net
63+
echo "Attempting direct download from restic.net..."
64+
if curl -sL "https://restic.net/gpg-key-alex.asc" | gpg --import >/dev/null 2>&1; then
65+
echo "Key imported successfully via direct download."
66+
return 0
6467
fi
65-
# Try public keyservers fallback
66-
local servers=( "hkps://keys.openpgp.org" "hkps://keyserver.ubuntu.com" )
68+
69+
# 3. Try Keyservers
70+
servers=( "hkps://keyserver.ubuntu.com" "hkps://keys.openpgp.org" "hkps://pgp.mit.edu" )
6771
for server in "${servers[@]}"; do
6872
echo "Attempting to fetch from $server..."
69-
if gpg --keyserver "$server" --recv-keys "$fpr"; then
70-
echo "Key imported successfully."
73+
if gpg --keyserver "$server" --recv-keys "$fpr" >/dev/null 2>&1; then
74+
echo "Key imported successfully from $server."
7175
return 0
7276
fi
7377
done
74-
echo "Failed to import restic PGP key." >&2
78+
79+
# 4. Check Debian/Ubuntu system keyring (Fallback for apt-installed systems)
80+
debian_keyring="/usr/share/keyrings/restic-archive-keyring.gpg"
81+
if [[ -f "$debian_keyring" ]]; then
82+
echo "Checking system keyring..."
83+
if gpg --no-default-keyring --keyring "$debian_keyring" --export "$fpr" | gpg --import >/dev/null 2>&1; then
84+
echo "Imported from system keyring."
85+
return 0
86+
fi
87+
fi
88+
89+
echo -e "${C_RED}Failed to import restic PGP key from all sources.${C_RESET}" >&2
7590
return 1
7691
}
7792

@@ -258,7 +273,7 @@ if [ ! -f "$CONFIG_FILE" ]; then
258273
echo -e "${C_RED}ERROR: Configuration file not found: $CONFIG_FILE${C_RESET}" >&2
259274
exit 1
260275
fi
261-
# shellcheck source=restic-backup.conf
276+
# shellcheck source=/dev/null
262277
source "$CONFIG_FILE"
263278
REQUIRED_VARS=(
264279
"RESTIC_REPOSITORY"
@@ -326,7 +341,7 @@ display_help() {
326341
echo -e "${C_BOLD}${C_YELLOW}DEPENDENCIES:${C_RESET}"
327342
echo -e " This script requires: ${C_GREEN}restic, curl, gpg, bzip2, less, jq, flock${C_RESET}"
328343
echo
329-
echo -e "Config: ${C_DIM}${CONFIG_FILE}${C_RESET} Log: ${C_DIM}${LOG_FILE}${C_RESET}"
344+
echo -e "Config: ${C_DIM}${CONFIG_FILE}${C_RESET} Log: ${C_DIM}${LOG_FILE:-"(not set)"}${C_RESET}"
330345
echo
331346
echo -e "For full details, see the online documentation: \e]8;;${readme_url}\a${C_CYAN}README.md${C_RESET}\e]8;;\a"
332347
echo -e "${C_YELLOW}Note:${C_RESET} For restic official documentation See: https://restic.readthedocs.io/"
@@ -354,7 +369,9 @@ handle_crash() {
354369

355370
build_backup_command() {
356371
local cmd=(restic)
357-
cmd+=($(get_verbosity_flags))
372+
local -a v_flags
373+
read -ra v_flags <<< "$(get_verbosity_flags)"
374+
cmd+=("${v_flags[@]}")
358375
if [ -n "${SFTP_CONNECTIONS:-}" ]; then
359376
cmd+=(-o "sftp.connections=${SFTP_CONNECTIONS}")
360377
fi
@@ -462,7 +479,7 @@ run_unlock() {
462479
echo -e "${C_YELLOW}Found stale locks in the repository:${C_RESET}"
463480
echo "$lock_info"
464481
local other_processes
465-
other_processes=$(ps aux | grep 'restic ' | grep -v 'grep' || true)
482+
other_processes=$(pgrep -ax restic || true)
466483
if [ -n "$other_processes" ]; then
467484
echo -e "${C_YELLOW}WARNING: Another restic process appears to be running:${C_RESET}"
468485
echo "$other_processes"
@@ -617,6 +634,7 @@ send_teams() {
617634
local escaped_message
618635
escaped_message=$(echo "$message" | sed 's/\\/\\\\/g' | sed 's/"/\\"/g' | sed ':a;N;$!ba;s/\n/\\n/g')
619636
local json_payload
637+
# shellcheck disable=SC2016
620638
printf -v json_payload '{
621639
"type": "message",
622640
"attachments": [{
@@ -1262,10 +1280,11 @@ run_backup() {
12621280
log_message "Backup completed successfully"
12631281
echo -e "${C_GREEN}✅ Backup completed${C_RESET}"
12641282
local stats_msg
1265-
printf -v stats_msg "Files: %s new, %s changed, %s unmodified\nData added: %s\nDuration: %dm %ds" \
1283+
printf -v stats_msg "Files: %s new, %s changed, %s unmodified\nProcessed: %s\nData added: %s\nDuration: %dm %ds" \
12661284
"${files_new:-0}" \
12671285
"${files_changed:-0}" \
12681286
"${files_unmodified:-0}" \
1287+
"${data_processed:-0}" \
12691288
"${data_added:-Not applicable}" \
12701289
"$((duration / 60))" \
12711290
"$((duration % 60))"
@@ -1284,7 +1303,9 @@ run_forget() {
12841303
echo -e "${C_BOLD}--- Cleaning Old Snapshots ---${C_RESET}"
12851304
log_message "Running retention policy"
12861305
local forget_cmd=(restic)
1287-
forget_cmd+=($(get_verbosity_flags))
1306+
local -a v_flags
1307+
read -ra v_flags <<< "$(get_verbosity_flags)"
1308+
forget_cmd+=("${v_flags[@]}")
12881309
forget_cmd+=(forget)
12891310
[ -n "${KEEP_LAST:-}" ] && forget_cmd+=(--keep-last "$KEEP_LAST")
12901311
[ -n "${KEEP_DAILY:-}" ] && forget_cmd+=(--keep-daily "$KEEP_DAILY")
@@ -1460,7 +1481,9 @@ _run_restore_command() {
14601481
shift 2
14611482
mkdir -p "$restore_dest"
14621483
local restic_cmd=(restic)
1463-
restic_cmd+=($(get_verbosity_flags))
1484+
local -a v_flags
1485+
read -ra v_flags <<< "$(get_verbosity_flags)"
1486+
restic_cmd+=("${v_flags[@]}")
14641487
restic_cmd+=(restore "$snapshot_id" --target "$restore_dest")
14651488
if [ $# -gt 0 ]; then
14661489
for path in "$@"; do
@@ -1643,7 +1666,7 @@ echo "To restore a specific directory from the latest snapshot:"
16431666
# restic restore latest --target /mnt/restore --include "/home/user_files"
16441667
16451668
EOF
1646-
chmod 400 "$tmpfile"
1669+
chmod 400 "$tmpfile"
16471670
mv -f "$tmpfile" "$recovery_file"
16481671
echo -e "\n${C_GREEN}✅ Recovery Kit generated: ${C_BOLD}${recovery_file}${C_RESET}"
16491672
echo -e "${C_BOLD}${C_RED}WARNING: This file contains your repository password.${C_RESET}"

0 commit comments

Comments
 (0)