Created
August 27, 2020 03:53
-
-
Save dale3h/a7b7e70770b0eed9b23466641aa6969b to your computer and use it in GitHub Desktop.
[unRAID] Auto-Copy for Unassigned Devices Plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
################################################################ | |
# Auto-Copy Script for unRAID Unassigned Devices Plugin | |
# Original script: https://gitlab.com/snippets/1737763 | |
################################################################ | |
PATH=/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin | |
# Available variables: | |
# | |
# AVAIL : Available space. | |
# USED : Used space. | |
# SIZE : Partition size. | |
# SERIAL : Disk serial number. | |
# ACTION : If mounting, ADD; if unmounting, REMOVE. | |
# MOUNTPOINT : Where the partition is mounted. | |
# FSTYPE : Partition filesystem. | |
# LABEL : Partition label. | |
# DEVICE : Partition device, e.g /dev/sda1. | |
# OWNER : "udev" if executed by UDEV, otherwise "user". | |
# PROG_NAME : Program name of this script. | |
# LOGFILE : Log file for this script. | |
# Configuration variables. | |
enableAdd=1 # 1 | 0 : Easily turn on or off ADD action. | |
enableBackup=1 # 1 | 0 : Easily turn on or off automatic backup. | |
enableDryRun=0 # 1 | 0 : Easily turn on or off dry run (test) mode. | |
enableUmount=1 # 1 | 0 : Disable umount in ADD action for testing. | |
enableRemove=1 # 1 | 0 : Easily turn on or off REMOVE action. | |
showVars=1 # 1 | 0 : Easily show Vars in ADD action. | |
srcPath="${MOUNTPOINT}" | |
srcLabel="${LABEL}" | |
backupPath="/mnt/user/backup/autocopy" | |
backupFolder="${srcLabel}_$(date +%Y%m%d)" | |
destPath="${backupPath}/${backupFolder}" | |
excludeFile="${srcPath}/exclude.txt" | |
# Function to set custom settings based on drive serial. | |
customSettings() { | |
case "${SERIAL}" in | |
'easystore_04BA_B8E1CE999933A6944C1B40E595EF') | |
# This overrides the backup path for a specific drive. | |
backupPath="/mnt/disk5/Backup" | |
destPath="${backupPath}/${backupFolder}" | |
writeLog "backupPath override: ${backupPath}" | |
writeLog "destPath override: ${destPath}" | |
;; | |
esac | |
} | |
# Function to email. | |
sendNotifyNormal() { | |
# Call with sendNotify "Subject" "Description". | |
/usr/local/emhttp/webGui/scripts/notify -i normal -s "${1}" -d "${2}" | |
} | |
sendNotifyError() { | |
# Call with sendNotify "Subject" "Description". | |
/usr/local/emhttp/webGui/scripts/notify -i error -s "${1}" -d "${2}" | |
} | |
clearLog() { | |
echo -n > "${LOGFILE}" | |
} | |
writeLog() { | |
args=("[$(date)]") | |
args+=("$@") | |
echo "${args[@]}" >> "${LOGFILE}" | |
} | |
echoVars() { | |
cat <<EOT >> "${LOGFILE}" | |
################################################################################ | |
showVars enabled: | |
AVAIL : ${AVAIL} | |
USED : ${USED} | |
SIZE : ${SIZE} | |
SERIAL : ${SERIAL} | |
ACTION : ${ACTION} | |
MOUNTPOINT : ${MOUNTPOINT} | |
FSTYPE : ${FSTYPE} | |
LABEL : ${LABEL} | |
DEVICE : ${DEVICE} | |
OWNER : ${OWNER} | |
PROG_NAME : ${PROG_NAME} | |
LOGFILE : ${LOGFILE} | |
srcPath : ${srcPath} | |
srcLabel : ${srcLabel} | |
backupPath : ${backupPath} | |
backupFolder : ${backupFolder} | |
destPath : ${destPath} | |
excludeFile : ${excludeFile} | |
################################################################################ | |
EOT | |
} | |
case "${ACTION}" in | |
'ADD') | |
writeLog "device mounted: ${srcLabel} (${srcPath})" | |
# Check for custom setting overrides. | |
customSettings | |
# Show all available variables. | |
if [[ "${showVars}" -eq "1" ]]; then | |
echoVars | |
fi | |
writeLog "backup destination: ${destPath}" | |
# Backup the contents of drive. | |
if [[ "${enableAdd}" -eq "1" ]]; then | |
# See: https://linux.die.net/man/1/rsync | |
rsyncArgs=("-Cav") | |
# Add dry run argument. | |
if [[ "${enableDryRun}" -eq "1" ]]; then | |
writeLog "dry-run enabled" | |
rsyncArgs+=("--dry-run") | |
fi | |
# Add exclude file argument. | |
if [[ -n "${excludeFile}" ]] && [[ -f "${excludeFile}" ]]; then | |
writeLog "exclude list file: ${excludeFile}" | |
rsyncArgs+=("--exclude-from" "${excludeFile}") | |
fi | |
# Add source and destination arguments. | |
rsyncArgs+=("${srcPath}/" "${destPath}") | |
writeLog "backup command: rsync ${rsyncArgs[*]}" | |
if [[ "${enableBackup}" -eq "1" ]]; then | |
if rsync "${rsyncArgs[@]}" >> "${LOGFILE}" 2>&1; then | |
writeLog "backup finished: ${srcPath} -> ${destPath}" | |
writeLog "ready to unmount: ${srcPath}" | |
# Call function to email. | |
sendNotifyNormal "" "Backup finished: ${srcPath} -> ${destPath}" | |
if [[ "${enableUmount}" -eq "1" ]]; then | |
/usr/local/sbin/rc.unassigned umount "${DEVICE}" | |
writeLog "device unmounted: ${srcPath} (${DEVICE})" | |
else | |
writeLog "unmount disabled" | |
fi | |
else | |
writeLog "backup failed: ${srcPath} -> ${destPath}" | |
sendNotifyerror "" "Backup failed: ${srcPath} -> ${destPath}" | |
fi | |
else | |
writeLog "backup disabled" | |
fi | |
fi | |
;; | |
'REMOVE') | |
if [[ "${enableRemove}" -eq "1" ]]; then | |
writeLog "device removed: ${DEVICE}" | |
fi | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment