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
fi2025-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.shis 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.
Note to self: Be careful when running the ' Docker Safe New Perms' tool
I stored this script in my
/mnt/user/.directory and did not exclude it from the path before running the tool without thinking (although the appdata share is excluded by default).This utility starts a background process that goes to each of your data disks and cache disk and changes file and directory ownership to nobody/users (i.e., uid/gid to 99/100), and sets permissions as follows:
For directories: drwxrwxrwx For read/write files: -rw-rw-rw- For readonly files: -r--r--r--
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)
Warning
Scripts must be stored anywhere outside of
/bootbecause Unraid boots from a FAT32 formatted USB that does not support script executions. I always had this script stored in the/mnt/userpath, but/mnt/cachewould work just as well and wouldn’t have been affected by the Docker Safe New Perms tool)
From here, I was able to fix it by running commands to:
- Move the file to the
/mnt/cache/scriptsdirectory instead (so I don’t accidentally do this again if I forget!) - Reset the ownership of the script to root.
- Make the script executable again
- 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.shFinally, 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