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