The Script

This is just a simple backup script I created that uses Rclone to backup my Unraid appdata to a remote Backblaze B2 bucket so I have an offsite backup as well (see 3-2-1-backup-strategy). It’s nothing fancy and I’m sure it can be improved upon if I have some more time to tweak it, but it does what I need it to for now.

#!/bin/bash
 
# Define the source directory and destination for Rclone
SOURCE_DIR="/mnt/user/backups/appdata_backup/"
DEST_BUCKET="backblazeEncrypted:unraid/backups/appdata/"
LOG_FILE="/mnt/user/backups/backup_log.txt"
 
# Find the latest backup directory
LATEST_DIR=$(ls -td ${SOURCE_DIR}ab_* | head -1)
 
# Record the start time
START_TIME=$(date +%s)
 
# Rclone sync command and log only the final output
{
    echo "Starting rclone sync from ${LATEST_DIR} at $(date)"
    rclone sync "${LATEST_DIR}" "${DEST_BUCKET}" --update --transfers 6 --checkers 4 --b2-chunk-size 200M --fast-list
    echo "Rclone sync completed at $(date)"
} >> "${LOG_FILE}" 2>&1
 
# Record the end time
END_TIME=$(date +%s)
 
# Calculate the duration in seconds
DURATION=$(( END_TIME - START_TIME ))
 
# Log the duration
echo "Backup took ${DURATION} seconds." >> "${LOG_FILE}"
 
# Check the exit status of the rclone command
if [ $? -eq 0 ]; then
    echo "Rclone sync completed successfully." >> "${LOG_FILE}"
else
    echo "Rclone sync failed." >> "${LOG_FILE}"
    exit 1
fi

2025-05-21 Update — Fixing a Permission Error

This script had been running fine for a long time now without any issue until a few days ago when I noticed that this script was no longer running at all. Thankfully I configured error emails that I set up through Unraid’s built in notification settings, so I was able to notice it fairly quickly.

The error email I received from the Backup/Restore Appdata plugin said:

/mnt/user/michael/scripts/ab_post-backup.sh is not executable! Skipping!”

What Caused The Error?

I believe this is because I ran the Docker Safe New Perms utility because the timing made sense and when I went to check the ownership and permissions of the script it was no longer owned by root and was not executable.

How did I Fix it?

First, I checked the permissions on the script in the location I originally had it stored in by using this command from the Unraid terminal:

cd /mnt/user/michael/scripts/
ls -l 
 
# Which gave me an output like:
-rw-rw-rw- 1 nobody users 1234 Oct 16 2024 ab_post-backup.sh 

That confirmed that the file is no longer executable and no longer belonged to the root user (which is usually the user that files belong to in Unraid systems)

From here, I was able to fix it by running commands to:

  1. Move the file to the /mnt/cache/scripts directory instead (so I don’t accidentally do this again if I forget!)
  2. Reset the ownership of the script to root.
  3. Make the script executable again
  4. Adjust the permissions to be a bit more restrictive so only root can modify or delete the script.
mv /mnt/user/michael/scripts/ab_post-backup.sh /mnt/cache/scripts/
chown root:root /mnt/cache/scripts/ab_post-backup.sh
chmod +x /mnt/cache/scripts/ab_post-backup.sh
chmod 755 /mnt/cahce/scripts/ab_post-backup.sh

Finally, to verify that the script belongs to root and is now executable again, I ran ls -l and the output confirmed that it worked!

-rwxr-xr-x 1 root root 1234 Oct 16 2024 ab_post-backup.sh*

I also had to change the location of the script in the Backup/Restore Appdata plugin to /mnt/cache/scripts/ab_post-backup.sh so it knows where to find it now.

Maintenance Task For Later:

Now that that is resolved though.. I need to go back through my Unraid shares and make sure I didn’t unintentionally mess anything else up when I ran the utility. So far everything else seemed fine and I didn’t have any other scripts stored there currently, but I still want to check to be sure.

  • Review Unraid share permissions after running Docker Safe New Perms utility