Skip to content

Instantly share code, notes, and snippets.

@killerbees19
Last active April 15, 2022 10:29
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 killerbees19/d179f693d16d7992f1238c97501464c0 to your computer and use it in GitHub Desktop.
Save killerbees19/d179f693d16d7992f1238c97501464c0 to your computer and use it in GitHub Desktop.
USBcopy for PTP/MTP devices
ACTION=="bind", SUBSYSTEMS=="usb", ATTRS{idVendor}=="04a9", ATTRS{idProduct}=="32d2", TAG+="systemd", ENV{SYSTEMD_WANTS}="usbcopy-canon-m50.service"
ACTION=="bind", SUBSYSTEMS=="usb", ATTRS{idVendor}=="04b0", ATTRS{idProduct}=="019a", TAG+="systemd", ENV{SYSTEMD_WANTS}="usbcopy-nikon-s9900.service"
#ACTION=="bind", SUBSYSTEMS=="usb", ATTRS{idVendor}=="0123", ATTRS{idProduct}=="4567", ENV{ID_SERIAL_SHORT}=="89abcdef", TAG+="systemd", ENV{SYSTEMD_WANTS}="usbcopy-example.service"
editor \
	/usr/local/sbin/usbcopy.sh \
	/etc/udev/rules.d/75-usbcopy.rules \
	/etc/systemd/system/usbcopy-canon-m50.service \
	/etc/systemd/system/usbcopy-nikon-s9900.service

chmod +x /usr/local/sbin/usbcopy.sh
apt install rsync gphotofs
udevadm control --reload
systemctl daemon-reload
[Service]
Type=oneshot
ExecStart=/usr/local/sbin/usbcopy.sh canon-m50
[Service]
Type=oneshot
ExecStart=/usr/local/sbin/usbcopy.sh nikon-s9900
#!/bin/bash
# cs@fnx.li (2022-04-14)
# [PUBLIC] W/O WOL-SUPPORT!
set -euf -o pipefail
MOUNTPOINT="/mnt/gphotofs"
SOURCE="$MOUNTPOINT/store_"
DESTINATION="user@example.net:/path/to/destination/"
SUBJECT="[$(hostname -s)] USBcopy task ${1:-}:"
ARGS=()
case "${1:-}" in
canon-m50)
SOURCE+="00020001"
DESTINATION+="canon/m50"
;;
nikon-s9900)
SOURCE+="00010001"
DESTINATION+="nikon/s9900"
ARGS=( --exclude "NCFL" )
;;
*)
echo "Usage: $0 <TASK>" >&2
exit 1
;;
esac
function error
{
tee -a "$LOGFILE" >&2 <<< "ERROR: $1"
exit 1
}
function cleanup
{
if [[ -z "${1:-}" && -n "${LOGFILE:-}" ]]
then
[ "$finished" -eq 1 ] && subject="Finished..." || subject="FAILED!"
mail -s "$SUBJECT $subject" "$(id -un)" < "$LOGFILE" || :
fi
fusermount -u "$MOUNTPOINT" 2>/dev/null || :
rmdir "$MOUNTPOINT" 2>/dev/null || :
rm -f "$LOGFILE"
}
finished=0
trap cleanup EXIT
LOGFILE=$(mktemp)
cleanup prepare
mkdir "$MOUNTPOINT"
gphotofs "$MOUNTPOINT"
if [ ! -d "$SOURCE" ]
then
error "Source $SOURCE unavailable!"
fi
rsync \
--rsh "ssh -o BatchMode=yes" \
--one-file-system --recursive --times --size-only \
--backup --suffix "~bkp-$(date --utc +%y%m%d-%H%M%S)" \
--stats --verbose --human-readable "${ARGS[@]}" \
"$SOURCE/" "$DESTINATION/" \
2>&1 | tee "$LOGFILE"
finished=1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment