r/linuxmint • • Oct 31 '26

SOLVED Backup script fails

So, I created this script to backup my home directory to an external HD which is mounted as BAK2. I can run the command if I use sudo but the script deletes everything on BAK2 and then fails with multiple permission errors. I was hoping to put this on a cron to run a few times a day and basically do an incremental backup but I've hit the end of my knowledge... Here's the script:

#!/bin/bash

# --- CONFIGURATION ---

SOURCE="/home/$USER/"

DESTINATION="/media/wreck/BAK2"

LOGFILE="/home/$USER/rsync_backup.log"

# --- NOTIFICATION: START ---

notify-send "Backup Started" "Syncing your home directory..." --icon=drive-harddisk

# --- RUN RSYNC ---

echo "Backup started at $(date)" >> "$LOGFILE"

rsync -av --delete "$SOURCE" "$DESTINATION" >> "$LOGFILE" 2>&1

# Check if rsync succeeded (exit code 0) or encountered an error

if [ $? -eq 0 ]; then

echo "Backup finished successfully at $(date)" >> "$LOGFILE"

# --- NOTIFICATION: SUCCESS ---

notify-send "Backup Complete" "Your home directory has been successfully mirrored." --icon=emblem-success

else

echo "Backup failed at $(date)" >> "$LOGFILE"

# --- NOTIFICATION: FAILURE ---

notify-send "Backup Failed" "An error occurred during the rsync mirror. Check $LOGFILE for details." --icon=dialog-error --urgency=critical

fi

5 Upvotes

5 comments sorted by

•

u/AutoModerator Oct 31 '26

Please Re-Flair your post if a solution is found. How to Flair a post? This allows other users to search for common issues with the SOLVED flair as a filter, leading to those issues being resolved very fast.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

5

u/wreckwriter09 Oct 31 '26

I think I figured it out. I lost an R somehow.... Script was running sync instead of rsync. Geez... It's running now and creating folders so I think it's going to work. Will report.

3

u/chuggerguy Linux Mint 22.3 Zena | MATÉ Oct 31 '26

"... I can run the command if I use sudo but the script deletes everything on BAK2 ..."

Just a guess but maybe it's because when you run the script as sudo, $USER is not you? (wreck?)

You might be better off replacing occurrences of $USER with your actual username? That or maybe using $SUDO_USER.

I don't know why you'd need to run it as sudo just syncing your home. I assume you have a reason.

Probably running it with cron, you'll need to be pretty explicit with paths. Even with the binaries.

It sometimes takes me a few tries to get stuff like that working. You'll get it.

Good luck.

3

u/wreckwriter09 Oct 31 '26

Thank you! Will try it

3

u/MartinUK_Mendip Nov 01 '26

re. cron: pipe the stderror output of your command to a file, or go look in /var/mail (the error is 'mailed' to you) otherwise you'll never know that an error has occurred.
And ... cron and sudo cron are two different things, and as chuggerguy says neither know anything about environment labels so you have to specify everything as it all works outside the BASH shell.

I did something similar to you, but through ssh to a NAS., which was a great learning experience about rsync, ssh, and how to quote in a BASH script.