Auto import from SD card to Immich
Here's a quick write up on how to import pictures and videos from SD card reader to Immich using systemd on Linux
I recently bought a mirrorless camera, and want to make it as smooth as possible to upload photos to Immich. After I have imported, I delete the images I do not want.
Automount SD card using systemd
First we need to indentify the UUID of the SD card:
blkid
Make sure to take note of the UUID of your SD card. Usually SD cards get UUID in the Format "XXXX-XXXX". My SD card was 4A21-0000
Create the file:
/etc/udev/rules.d/99-sdcard.rules
With the content:
ACTION=="add", SUBSYSTEM=="block", ENV{ID_FS_UUID}=="4A21-0000", ENV{SYSTEMD_WANTS}+="mnt-fuji.mount"
ACTION=="remove",SUBSYSTEM=="block", ENV{ID_FS_UUID}=="4A21-0000", RUN+="/usr/bin/systemctl stop mnt-fuji.mount"
Make sure to change 4A21-0000
to your UUID.
Now we can create the systemd mount. As you can see from the sdcard.rules I called the mount mnt-fuji.mnt. This is actually a systemd service file.
Create the file:
/etc/systemd/system/mnt-fuji.mount
With the content:
[Unit]
Description=Mount Fuji SD Card
Wants=dev-disk-by\x2duuid-4A21\x2d0000.device
After=dev-disk-by\x2duuid-4A21\x2d0000.device
[Mount]
What=/dev/disk/by-uuid/4A21-0000
Where=/mnt/fuji
Type=exfat
Options=ro,noexec,nodev,nosuid
[Install]
WantedBy=multi-user.target
Remember to change the UUID, Type (type is systemd for filesystem), and mount point to your liking.
Create the systemd service file for running the script:
/etc/systemd/system/on-fuji-inserted.service
With the content:
[Unit]
Description=Run custom program when Fuji is mounted
Requires=mnt-fuji.mount
After=mnt-fuji.mount
[Install]
WantedBy=mnt-fuji.mount
[Service]
Type=oneshot
Environment="IMMICH_API_KEY=<insert_your_api_key_here>"
Environment="IMMICH_INSTANCE_URL=https://<immich.youdomain>"
Environment="MOUNT_DIR=/mnt/fuji/DCIM/100_FUJI/"
ExecStart=/usr/local/bin/auto-upload-immich.sh --when=inserted --mount-point=/mnt/fuji
Please make sure to edit the environment variables for your environment. Take note that I have set the variable MOUNT_DIR to DCIM/100_FUJI, so Immich doesn't need to scan the whole SD card for the media files.
So finally it it is the script. Create and edit the file:
/usr/local/bin/auto-upload-immich.sh
With the content:
#!/bin/bash
# Check required env vars is set
if [ -z "${IMMICH_INSTANCE_URL:-}" ] || [ -z "${IMMICH_API_KEY:-}" ] || [ -z "${MOUNT_DIR:-}" ]; then
echo >&2 "Error: both IMMICH_INSTANCE_URL and IMMICH_API_KEY must be set"
echo >&2 " e.g. export IMMICH_INSTANCE_URL=https://your.immich.instance"
echo >&2 " export IMMICH_API_KEY=your_api_key_here"
echo >&2 "You also need to set the MOUNT_DIR env var"
exit 1
fi
# set immich_cli variable
IMMICH_CLI="docker run --rm --pull=always \
-v $MOUNT_DIR:/import:ro \
-e IMMICH_INSTANCE_URL=$IMMICH_INSTANCE_URL \
-e IMMICH_API_KEY=$IMMICH_API_KEY \
ghcr.io/immich-app/immich-cli:latest upload -r /import"
echo "Starting Immich import"
$IMMICH_CLI
- You need docker (or podman) to run this.
- We also pull=always, so we do not end up with a stale immich cli image.
- We do not delete the files from the SD card, and mount it as read only inside the container.
Finally we need to run some udev reloads and start the services.
systemctl list-units --all *udevd.service
systemctl daemon-reload
systemctl enable --now mnt-fuji.mount
systemctl enable on-fuji-inserted.service
You can check your services with:
journalctl -u on-fuji-inserted.service -f
and
journalctl -u mnt-fuji.mount -f