diff --git a/.config/systemd/user/garmin-dump.service b/.config/systemd/user/garmin-dump.service new file mode 100644 index 0000000..ab8db7f --- /dev/null +++ b/.config/systemd/user/garmin-dump.service @@ -0,0 +1,11 @@ +[Unit] +Description=Dump the Garmin database to ~/.local/share/negsplit/dumps +StartLimitIntervalSec=2h +StartLimitBurst=4 + +[Service] +Type=oneshot +ExecStart=%h/.local/bin/garmin-dump +# MariaDB not up yet (e.g. caught up right after boot)? retry a few times +Restart=on-failure +RestartSec=5min diff --git a/.config/systemd/user/garmin-dump.timer b/.config/systemd/user/garmin-dump.timer new file mode 100644 index 0000000..00bdbf1 --- /dev/null +++ b/.config/systemd/user/garmin-dump.timer @@ -0,0 +1,9 @@ +[Unit] +Description=Dump the Garmin database weekly + +[Timer] +OnCalendar=weekly +Persistent=true + +[Install] +WantedBy=timers.target diff --git a/.github/README.md b/.github/README.md index 071eb7c..fc46a7a 100644 --- a/.github/README.md +++ b/.github/README.md @@ -17,7 +17,7 @@ install script. This README lives in `.github/` to keep it out of the home direc | Audio / media | mpd, ncmpcpp, mpDris2, mpv, beets, pipewire | | Packaging | `.config/aurutils-makepkg.conf`, `.config/aurb/`, `.local/bin/aurb` / `aurs` (aurutils wrappers) | | Services | `.config/systemd/user/` (timers and overrides) | -| Scripts | `.local/bin/` (screen layout, phone mount, video helpers, Library database dump) | +| Scripts | `.local/bin/` (screen layout, phone mount, video helpers, Library and Garmin database dumps) | Note: `config.fish` sources `~/.bash_aliases`, so the aliases apply in both shells. diff --git a/.local/bin/garmin-dump b/.local/bin/garmin-dump new file mode 100755 index 0000000..a8bdee0 --- /dev/null +++ b/.local/bin/garmin-dump @@ -0,0 +1,24 @@ +#!/bin/bash +# Dump the Garmin database to a per-ISO-week file, keeping the two newest weeks. +set -euo pipefail + +cnf=${XDG_CONFIG_HOME:-$HOME/.config}/negsplit/dump.cnf +dir=${XDG_DATA_HOME:-$HOME/.local/share}/negsplit/dumps +out=$dir/Garmin-$(date +%G-W%V).sql.gz +keep=2 + +umask 077 +mkdir -p "$dir" + +# Write to a temporary file so a failed dump never replaces a good one +tmp=$(mktemp "$dir/.dump.XXXXXX") +trap 'rm -f "$tmp"' EXIT + +mariadb-dump --defaults-extra-file="$cnf" --single-transaction --databases Garmin | gzip > "$tmp" +mv "$tmp" "$out" + +# Names sort by week, so all but the last $keep are the old ones +dumps=("$dir"/Garmin-*.sql.gz) +if (( ${#dumps[@]} > keep )); then + rm -f -- "${dumps[@]:0:${#dumps[@]}-keep}" +fi