summaryrefslogtreecommitdiff
path: root/import/photobox_import2.sh
blob: 050154ac9168a41538255bb5c7e2306f3bef332b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/bin/bash

readonly photoregex='.*\(jpg\|raf\|cr2\)'

readonly backup_root=/btrfs/staging/import_tmp
readonly pending_root=/btrfs/storage/pending-photos
readonly input="$1"

make_unique() {
    if [[ ! -d "$1" ]]; then
        echo "$1"
    else
        extension=1
        while [[ -d "${1}-${extension}" ]]; do
            extension=$(($extension + 1))
        done
        echo "${1}-${extension}"
    fi
}

readonly backup_path="$(make_unique $backup_root/$(date +%Y-%m-%d)-$(basename $input))"
readonly pending_path="$(make_unique $pending_root/$(date +%Y-%m-%d))"

(
    flock -x -w 120 200 || exit 1
    if [[ $input != *Kingston* ]]; then
      echo "Not on the SD card reader: $input"
      exit 1
    fi
    if [[ -z $(find "$input" -type f -print -quit | head) ]]; then
      echo "No files found. Exiting."
      umount $input
      exit 0
    fi

    mkdir -p "$backup_path"
    rsync -Hvax "$input/." "$backup_path/."
    echo "Contents backed up to: $backup_path"

    if [[ -d "$backup_path/DCIM" && \
          -n $(find "$backup_path/DCIM" -iregex "$photoregex" -print -quit | head) ]]; then
        echo "Found photos"
        mkdir -p "${pending_path}.importing"
        rsync -avx "$backup_path"/DCIM/*/* "${pending_path}.importing/"
        mv "${pending_path}.importing" "${pending_path}"
        echo "Photos written to ${pending_path}"
    fi

    echo "Deleting original files"
    # Prevents 'rm -rf /'
    rm -r "/media/$(basename $input)"
    umount "${input}"
) 200>/tmp/photobox_import2.lock | systemd-cat -t photobox_import