Help with Bash Backup Script before shutdown

In the stage creating a bash script to run before i shutdown my desktop.

Does my script need any more anything else?

I want to have proper check with –read-data alongside forget + prune maintance script created.

#!/bin/bash

source /home/$USER/.bin/backup/.env

LOG_FILE="/var/log/scripts/pc-backup.log"

log_message() {
    echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE"
}

restic unlock \
    --repo "$RESTIC_PC_REPO" \
    --password-file <(echo "$RESTIC_PC_PW")

restic check \
    --repo "$RESTIC_PC_REPO" \
    --password-file <(echo "$RESTIC_PC_PW")

restic backup \
    --repo "$RESTIC_PC_REPO" \
    --password-file <(echo "$RESTIC_PC_PW") \
    --verbose \
    --tag auto \
    --compression auto \
    --files-from "/home/$USER/.bin/backup/backup.files" \
    --exclude-file "/home/$USER/.bin/backup/exclude.files" 2>&1 | tee -a "$LOG_FILE"

BACKUP_EXIT_CODE=${PIPESTATUS[0]}

if [ $BACKUP_EXIT_CODE -eq 0 ]; then
    log_message "Backup completed successfully"
else
    log_message "ERROR: Backup failed with exit code $BACKUP_EXIT_CODE"
    exit $BACKUP_EXIT_CODE
fi

Hi, I would not personally lead with a unconditional unlock of your repository. What if it is locked for a valid reason?

i understand your point but why would i lock the repo if im gonna backup it up.

Please read again.

Your script starts with an unconditional UNlock. This is potentially dangerous because there might be some other restic instance running already whose lock you just removed forcefully.

sorry, im trying to understand this restic backup script from others. so should i remove it or put a conditional unlock if so what condition?

@adamol @leftytennis @martinleben the script from adamol executes restic unlock, this removes stale locks only and is safe to use.
But if you add --remove-all then it becomes unconditional.

I could only find some good reference in the command line help:

restic unlock --help

The "unlock" command removes stale locks that have been created by other restic processes.

EXIT STATUS
===========

Exit status is 0 if the command was successful.
Exit status is 1 if there was any error.

Usage:
  restic unlock [flags]

Flags:
  -h, --help         help for unlock
      --remove-all   remove all locks, even non-stale ones

When I work on Windows and OneDrive or shared network drives then sometimes restic is interrupted due to network disruptions. This leaves lock files in place. The unlock instruction at the start makes that the backup script can remove the stale lock at the next round; and if the repo is genuinly locked it will fail.

2 Likes

@adamol then I have some generic remarks on the script.

  1. you source variables from the .env file. why then echo that to a --password-file option? i’d define RESTIC_PASSWORD and RESTIC_REPOSITORY in the .env and then you can remove all these --password-file and --repo lines. Refer:
    Backing up — restic 0.18.1 documentation
  2. you execute three commands in the script (unlock, check, backup). In the end you only pass on one exit code from backup. However it could be that CHECK fails and BACKUP passes, in this case the exit could would be 0 (success). I’d want to be alerted for all errors. The easiest is probably to also pipe the other commands to your logfile and process the exit code in similar way as BACKUP?
  3. you mention you want to run this before the computer shuts down. How do you do that?
1 Like

thanks, the comments & opinion

  1. thanks will update the source variables, per docs
  2. absolutely, trying to get the base script sorted out, then i will get the status check done
  3. so i have keyboard shortcut, eg SUPER + S, which runs the backup script then systemctl poweroff