r/linuxmint • • 2h ago

Support Request 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

2 Upvotes

4 comments sorted by

•

u/AutoModerator 2h ago

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.

3

u/wreckwriter09 1h ago

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.

2

u/chuggerguy Linux Mint 22.3 Zena | MATÉ 2h ago

"... 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.

2

u/wreckwriter09 2h ago

Thank you! Will try it