Skip to content

Instantly share code, notes, and snippets.

@t3chguy
Forked from ljm42/PhotoImport.sh
Last active August 19, 2023 05:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save t3chguy/e71e31b004b910ae589b6d72c0b5c7d0 to your computer and use it in GitHub Desktop.
Save t3chguy/e71e31b004b910ae589b6d72c0b5c7d0 to your computer and use it in GitHub Desktop.
unRAID - automatically move photos to array
#!/bin/bash
PATH=/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin
## Usage (after configuration):
## 1. Insert camera's memory card into a USB port on your unRAID system
## 2. The system will automatically move (or copy) any images/videos from the memory card to the array
## If jhead was installed, it will automatically rotate images according to the exif data
## 3. Wait for the imperial theme to play, then remove the memory card
## Preparation:
## 1. Install jhead (to automatically rotate photos) using the Nerd Pack plugin
## 2. Install the "Unassigned Devices" plugin
## 3. Use that plugin to set this script to run *in the background* when a memory card is inserted
## 4. Configure variables in this script as described below
## Warning:
## The newperms script has a bug in unRAID 6.1.3 - 6.1.7
## see https://lime-technology.com/forum/index.php?topic=43388.0
## --- BEGIN CONFIGURATION ---
## SET THIS FOR YOUR CAMERAS:
## array of directories under /DCIM/ that contain files you want to move (or copy)
## can contain regex
VALIDDIRS=("/DCIM/[0-9][0-9][0-9]___[0-9][0-9]" "/DCIM/[0-9][0-9][0-9]CANON" "/DCIM/[0-9][0-9][0-9]_FUJI" "/DCIM/[0-9][0-9][0-9]NIKON" \
"/DCIM/[0-9][0-9][0-9]MSDCF" "/DCIM/[0-9][0-9][0-9]OLYMP" "/DCIM/[0-9][0-9][0-9]MEDIA" "/DCIM/[0-9][0-9][0-9]GOPRO" "/DCIM/[0-9][0-9][0-9]_PANA" "/DCIM/Camera[0-9][0-9]")
## SET THIS FOR YOUR SYSTEM:
## location to move files to. use date command to ensure unique dir
DESTINATION="/mnt/user/Vault/Videos/Insta360/$(date +"%Y-%m-%d-%H-%M-%S-%N")/"
## SET THIS FOR YOUR SYSTEM:
## change to "move" when you are confident everything is working
MOVE_OR_COPY="move"
## set this to 1 and check the syslog for additional debugging info
DEBUG=""
## 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
log_all() {
log_local "$1"
logger "$PROG_NAME-$1"
}
log_local() {
# echo "`date` $PROG_NAME-$1"
echo "`date` $PROG_NAME-$1" >> $LOGFILE
}
log_debug() {
if [ ${DEBUG} ]
then
log_local "$1"
fi
}
case $ACTION in
'ADD' )
#
# Beep that the device is plugged in.
#
beep -l 200 -f 600 -n -l 200 -f 800
sleep 2
if [ -d ${MOUNTPOINT} ]
then
# only process an automount. manual mount is messy, users may or may not expect it to unmount afterwards
if [ "$OWNER" = "udev" ]; then
log_all "Started"
log_debug "Logging to $LOGFILE"
RSYNCFLAG=""
MOVEMSG="copying"
if [ ${MOVE_OR_COPY} == "move" ]; then
RSYNCFLAG=" --remove-source-files "
MOVEMSG="moving"
fi
# only operate on USB disks that contain a /DCIM directory, everything else will simply be mounted
if [ -d "${MOUNTPOINT}/DCIM" ]; then
log_debug "DCIM exists ${MOUNTPOINT}/DCIM"
# loop through all the subdirs in /DCIM looking for dirs defined in VALIDDIRS
for DIR in ${MOUNTPOINT}/DCIM/*; do
if [ -d "${DIR}" ]; then
log_debug "checking ${DIR}"
for element in "${VALIDDIRS[@]}"; do
if [[ ${DIR} =~ ${element} ]]; then
# process this dir
mkdir -p "${DESTINATION}"
log_local "${MOVEMSG} ${DIR}/ to ${DESTINATION}"
rsync -a ${RSYNCFLAG} --progress "${DIR}/" "${DESTINATION}"
fi
done
fi
done
# files were moved (or copied). rotate images, fix permissions
if [ -d "${DESTINATION}" ]; then
if [ -e "/usr/bin/jhead" -a -e "/usr/bin/jpegtran" ]; then
log_debug "running jhead on ${DESTINATION}"
jhead -autorot -ft "${DESTINATION}"*.[jJ][pP][gG]
fi
log_debug "fixing permissions on ${DESTINATION}"
newperms "${DESTINATION}"
sync -f ${DESTINATION}
fi
# sync and unmount USB drive
sync -f ${MOUNTPOINT}
sleep 1
/usr/local/sbin/rc.unassigned umount $DEVICE
# send notification
beep -l 200 -f 600 -n -l 200 -f 800
/usr/local/emhttp/webGui/scripts/notify -e "unRAID Server Notice" -s "Photo Import" -d "Photo Import completed" -i "normal"
fi # end check for DCIM directory
else
log_all "Photo Import drive not processed, owner is $OWNER"
fi # end check for valid owner
else
log_all "Mountpoint doesn't exist ${MOUNTPOINT}"
fi # end check for valid mountpoint
;;
'REMOVE' )
#
# Beep that the device is unmounted.
#
beep -l 200 -f 800 -n -l 200 -f 600
log_all "Photo Import drive unmounted, can safely be removed"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment