Skip to content

Instantly share code, notes, and snippets.

@maxkagamine
Last active June 28, 2023 01:34
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save maxkagamine/0fda138ff67e4ad9fcad692fe852a168 to your computer and use it in GitHub Desktop.
Save maxkagamine/0fda138ff67e4ad9fcad692fe852a168 to your computer and use it in GitHub Desktop.
Unraid user script to create backups of the flash drive and appdata on a schedule. An email is sent with the full command output if the cronjob fails.
#!/bin/bash
#name=Backup flash and appdata
#description=Creates tarballs of the flash drive and appdata share.
#argumentDescription=Backup directory
#argumentDefault=/mnt/user/Backup/Sovngarde
#arrayStarted=true
#noParity=true
#clearLog=true
set -eo pipefail
shopt -s extglob
PATH="/usr/local/emhttp/webGui/scripts:$PATH"
cd "${1:-/mnt/user/Backup/Sovngarde}"
create_backup() {
# Usage: create_backup <name> <src>
local name=$1
local src=$2
local date; date=$(date -I)
local tarball="${name}_${date}.tgz"
echo "$src -> $tarball"
tar cvfz "$tarball" \
--exclude-ignore .tarignore \
--transform "s/^\./${name}/" \
--show-stored-names \
--totals \
-C "$src" .
# Remove old backups
rm -fv "$name"_!("$date").tgz
}
run() {
echo "Backing up to $(pwd)"
create_backup flash /boot
create_backup appdata /mnt/user/appdata
# For testing error notification
if [[ $FAIL_ON_PURPOSE ]]; then
echo 'Task failed successfully.' >&2
return 1
fi
}
# Capture log for error report while still writing to stdout
# https://stackoverflow.com/a/16292136
{ log=$(run 2>&1 | tee /dev/fd/5); } 5>&1 || err=$?
if (( err > 0 )); then
notify \
-s "$(sed -n 's/^#name=//p' "${BASH_SOURCE[0]}") script failed" \
-d "Exit code $err (logs below)" \
-i 'alert' \
-m "$log"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment