-
-
Save Mearman/6461b6a200d25976d84ae83d988418d8 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. | |
enableMove=1 # 1 | 0 : Whether to remove files from device after move | |
useFlagFile=0 # 1 | 0 : Whether to only operate only if the flag file exists | |
srcPath="${MOUNTPOINT}" | |
srcLabel="${LABEL}" | |
backupPath="/mnt/user/main/autocopy" | |
backupFolder="$(date +'%Y')/$(date +'%Y_%m')/$(date +'%Y_%m_%d')/$(date +'%Y_%m_%d')_${srcLabel}" | |
destPath="${backupPath}/${backupFolder}" | |
globalIncludeFile="global_include.txt" | |
globalIncludePath="${backupPath}/${globalIncludeFile}" | |
includeFile="include.txt" | |
includePath="${srcPath}/${includeFile}" | |
excludeFile="exclude.txt" | |
excludePath="${srcPath}/${excludeFile}" | |
flagFile="autoCopy.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 | |
# Whether to look for and only operate when flagFile exists | |
if [[ "${useFlagFile}" -eq "1" ]]; then | |
flagPath="${srcPath}/${flagFile}" | |
if test -f "$flagPath"; then | |
echo "$flagPath found" | |
else | |
echo "Cannot find flag file" | |
exit 1 | |
fi | |
fi | |
writeLog "backup destination: ${destPath}" | |
# Backup the contents of drive. | |
if [[ "${enableAdd}" -eq "1" ]]; then | |
writeLog "creating output dir" | |
mkdir -p $destPath | |
# See: https://linux.die.net/man/1/rsync | |
rsyncArgs=("-Cavb" "--max-delete=0" "--suffix=_$(date +'%H%M%S')") | |
# Add dry run argument. | |
if [[ "${enableDryRun}" -eq "1" ]]; then | |
writeLog "dry-run enabled" | |
rsyncArgs+=("--dry-run") | |
fi | |
# exclude flagFile from being moved | |
if [[ "${useFlagFile}" -eq "1" ]]; then | |
rsyncArgs+=("--exclude=${flagFile}") | |
fi | |
# Add remove source files argument. | |
if [[ "${enableMove}" -eq "1" ]]; then | |
writeLog "remove-source-files enabled" | |
rsyncArgs+=("--remove-source-files") | |
fi | |
# Add exclude file argument. | |
if [[ -n "${excludePath}" ]] && [[ -f "${excludePath}" ]]; then | |
writeLog "exclude list file: ${excludePath}" | |
rsyncArgs+=("--exclude-from=${excludePath}") | |
rsyncArgs+=("--exclude=${excludeFile}") | |
fi | |
if [[ -n "${globalIncludePath}" ]] && [[ -f "${globalIncludePath}" ]]; then | |
writeLog "global include list file: ${globalIncludePath}" | |
rsyncArgs+=("--include-from=${globalIncludePath}") | |
fi | |
# Add include file argument. | |
if [[ -n "${includePath}" ]] && [[ -f "${includePath}" ]]; then | |
writeLog "include list file: ${includePath}" | |
rsyncArgs+=("--include-from=${includePath}") | |
rsyncArgs+=("--exclude=${includeFile}") | |
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 | |
find "${backupPath}" -type d -empty -delete | |
find "${srcPath}" -type d -empty -delete | |
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