~craftyguy/blog.craftyguy.net

e05c568b4133191e3c95c41f234b95c6f7472a09 — Clayton Craft 8 months ago 17042f5
Add post about backing up Garmin Edge on Linux
1 files changed, 51 insertions(+), 0 deletions(-)

A content/garmin_udiskie.md
A content/garmin_udiskie.md => content/garmin_udiskie.md +51 -0
@@ 0,0 1,51 @@
+++
title = "Automatic backup of Garmin Edge on Linux"
date = 2021-03-12
[taxonomies]
tags = ["cycling", "shell", "automation"]
+++
I purchased a Garmin Edge 130 last year to use for recording cycling trips, one of the main reasons for choosing this device was that it didn't require any special/proprietary software for accessing recorded activities or adding tracks. When the device is plugged into a computer via USB, it shows up as a mass storage device! Perfect!

Obviously keeping my precious ride recordings solely on the device is risky business, and (obviously) manually copying them off each and every time I ride is annoying and not something I want to do, considering I've been known to ride several times a week. So I came up with the following solution to have recorded rides automatically backed up off the device when I plug it in, something I do at the end of almost every ride to ensure the Edge always has a full charge.

This solution uses `udiskie`, which is an automount helper frontend thing for `udisks2`. A custom script is run by `udiskie` when it automounts the mass storage device, the script then runs a command for backing up recordings.

### ~/.config/udiskie/config.yml:
```yaml
---
program_options:
  notify_command: "/home/<ME>/bin/backup_garmin {event} {device_presentation}"
  notify: true
  tray: false
  automount: true
```
`udiskie` needs to be started, which can be accomplished by running it manually from the command line, via a systemd user service, etc.

### Script to perform the backup:
```bash
#!/bin/sh
set -e

BACKUP_DIR=/home/$(whoami)/Sync/rides/garmin
mkdir -p "$BACKUP_DIR"
event="$1"
disk="$2"
[ -z "$event" ] && exit 0
[ -z "$disk" ] && exit 0
# only pay attention to device_mounted event
[ "$event" != "device_mounted" ] && exit 0

label="$(lsblk -n -o label /dev/sdb)"
[ "$label" != "GARMIN" ] && exit 0

ride_dir="/run/media/$(whoami)/GARMIN/GARMIN/ACTIVITY"

notify-send "Backing up $disk/$label to $BACKUP_DIR"
rsync -r "$ride_dir/" "$BACKUP_DIR/"
```

`BACKUP_DIR` can be modified to send backups to some other location.

Since reading from the Garmin is slow, this uses `rsync` so that any existing files are not re-transferred if they haven't changed.

This backup script could be extended to run any number of things on other events from `udiskie`!