1

I created a script to automatically transfer images from an SD card to my NAS which works like a charm when executing it from the shell:

#!/bin/sh   
{

TARGET=/home/pi/Networkdrive
MOUNT=/home/pi/sdCard
APPLICATION=/home/pi/sdCard-Backuper
FILE_MATCH=".*\.(jpg|nef|mov)$"
CARD_DEV="sdb1"

printf "****** Script sd-autocopy.sh was started on " >> $APPLICATION/logfile.log
date +"%Y-%m-%d - %H:%M:%S" >> $APPLICATION/logfile.log
printf " ******\n" >> $APPLICATION/logfile.log

while true
do

    # Wait for a card reader or a camera
    CARD_READER=$(ls /dev/* | grep $CARD_DEV | cut -d"/" -f3)
    until [ ! -z $CARD_READER ]
        do
        sleep 5
        CARD_READER=$(ls /dev/sd* | grep $CARD_DEV | cut -d"/" -f3)
    done

    # If the card reader is detected, mount it
    if [ ! -z $CARD_READER ]; then
        sudo mount /dev/$CARD_DEV $MOUNT
    fi

    # Find folders with images on SD-Card
    LINES=$(find "$MOUNT" -regextype posix-egrep -iregex "$FILE_MATCH" -printf "%h\n" | sort | uniq)

    # Handle images when folders with images were found
    if [ ! -z $LINES ]; then
        cd $TARGET
        # Move the images
        for LINE in $LINES; do
            printf "Image transfer started " >> $APPLICATION/logfile.log
            date +"%Y-%m-%d - %H:%M:%S" >> $APPLICATION/logfile.log
            exiftool "-Directory<DateTimeOriginal" -d "%Y/%Y-%m/%Y-%m-%d" $LINE >> $APPLICATION/logfile.log
        done

        printf "****** Image transfer finished " >> $APPLICATION/logfile.log
        date +"%Y-%m-%d - %H:%M:%S" >> $APPLICATION/logfile.log
        printf " ******\n" >> $APPLICATION/logfile.log
        sleep 5
    fi

    sudo umount /dev/$CARD_DEV

done # while true
} &
exit 0

However when executing it from using crontab the script also gets called but the images do not appear on the network drive. To be honest, I do not know where the images are copied at the moment. My crontab looks as follows:

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games
@reboot sh /home/pi/sdCard-Backuper/sdAutocopy.sh

I took $PATH when calling in my home directory.

echo $PATH

My assumption what the problem might be is the following line in the script:

cd $TARGET
# Move the images

I must change to the directory of the network drive because exiftool does not accept a target folder where the images should be copied to. It only accepts the source folder. Phew... this took me hours so far and I have no idea what might be wrong.

Your help is highly appreciated!

Thanks, Oliver

H0ttenburg
  • 53
  • 1
  • 3
  • Try including `$TARGET` in you `-d` option. `-d "$TARGET/%Y/%Y-%m/%Y-%m-%d"`. I'm not familiar enough with cron to know if that's exact, but you should be able to include the target path in there. – StarGeek Nov 01 '17 at 18:37
  • Another option would be to place $TARGET with the Tag Copy part of the command. But that gets tricky as DateTimeOriginal now has to be treated as a string, but not interpreted as a bash variable. I think something like this would work, but am unable to test it. `"-Directory<$TARGET"'/$DateTimeOriginal'` (take careful note of the double/single quotes). – StarGeek Nov 01 '17 at 18:50
  • @StarGeek brilliant!!! The change to `-d "$TARGET/%Y/%Y-%m/%Y-%m-%d"` helped! Many, many thanks! – H0ttenburg Nov 01 '17 at 19:13

0 Answers0