Skip to content

Commit 1ff7b34

Browse files
author
buildplan
committed
handle restore ownership
1 parent cf1d367 commit 1ff7b34

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

restic-backup.sh

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,30 +1330,37 @@ run_restore() {
13301330
echo -e "${C_GREEN}✅ Restore completed${C_RESET}"
13311331

13321332
# Set file ownership logic
1333-
if [[ "$restore_dest" == /home/* ]]; then
1334-
local dest_user
1335-
dest_user=$(stat -c %U "$(dirname "$restore_dest")" 2>/dev/null || echo "${restore_dest#/home/}" | cut -d/ -f1)
1336-
if [[ -n "$dest_user" ]] && id -u "$dest_user" &>/dev/null; then
1337-
echo -e "${C_CYAN}ℹ️ Home directory detected. Setting ownership of restored files to '$dest_user'...${C_RESET}"
1338-
if chown -R "${dest_user}:${dest_user}" "$restore_dest"; then
1339-
log_message "Successfully changed ownership of $restore_dest to $dest_user"
1340-
echo -e "${C_GREEN}✅ Ownership set to '$dest_user'${C_RESET}"
1341-
else
1342-
log_message "WARNING: Failed to change ownership of $restore_dest to $dest_user"
1343-
echo -e "${C_YELLOW}⚠️ Could not set file ownership. Please check permissions manually.${C_RESET}"
1344-
fi
1345-
fi
1346-
fi
1333+
_handle_restore_ownership "$restore_dest"
1334+
13471335
send_notification "Restore SUCCESS: $HOSTNAME" "white_check_mark" \
13481336
"${NTFY_PRIORITY_SUCCESS}" "success" "Restored $snapshot_id to $restore_dest"
13491337
fi
13501338
rm -f "$restore_log"
13511339
}
13521340

1341+
_handle_restore_ownership() {
1342+
local restore_dest="$1"
1343+
1344+
if [[ "$restore_dest" == /home/* ]]; then
1345+
local dest_user
1346+
dest_user=$(stat -c %U "$(dirname "$restore_dest")" 2>/dev/null || echo "${restore_dest#/home/}" | cut -d/ -f1)
1347+
1348+
if [[ -n "$dest_user" ]] && id -u "$dest_user" &>/dev/null; then
1349+
log_message "Home directory detected. Setting ownership of restored files to '$dest_user'."
1350+
if chown -R "${dest_user}:${dest_user}" "$restore_dest"; then
1351+
log_message "Successfully changed ownership of $restore_dest to $dest_user"
1352+
else
1353+
log_message "WARNING: Failed to change ownership of $restore_dest to $dest_user. Please check permissions manually."
1354+
fi
1355+
fi
1356+
fi
1357+
}
1358+
13531359
_run_restore_command() {
13541360
local snapshot_id="$1"
13551361
local restore_dest="$2"
13561362
shift 2
1363+
mkdir -p "$restore_dest"
13571364

13581365
# Build the command
13591366
local restic_cmd=(restic)
@@ -1398,6 +1405,7 @@ run_background_restore() {
13981405
if _run_restore_command "$@"; then
13991406
local end_time=$(date +%s)
14001407
local duration=$((end_time - start_time))
1408+
_handle_restore_ownership "$restore_dest"
14011409
log_message "Background restore SUCCESS: ${snapshot_id} to ${restore_dest} in ${duration}s."
14021410
local notification_message
14031411
printf -v notification_message "Successfully restored snapshot %s to %s in %dm %ds." \
@@ -1418,6 +1426,7 @@ run_sync_restore() {
14181426
log_message "Starting synchronous restore."
14191427

14201428
if _run_restore_command "$@"; then
1429+
_handle_restore_ownership "$@"
14211430
log_message "Sync-restore SUCCESS."
14221431
send_notification "Sync Restore SUCCESS: $HOSTNAME" "white_check_mark" \
14231432
"${NTFY_PRIORITY_SUCCESS}" "success" "Successfully completed synchronous restore."

0 commit comments

Comments
 (0)