diff --git a/.config/systemd/user/garmin-dump.service b/.config/systemd/user/garmin-dump.service index ab8db7f..652ae9f 100644 --- a/.config/systemd/user/garmin-dump.service +++ b/.config/systemd/user/garmin-dump.service @@ -5,7 +5,7 @@ StartLimitBurst=4 [Service] Type=oneshot -ExecStart=%h/.local/bin/garmin-dump +ExecStart=%h/.local/bin/db-dump negsplit Garmin week # 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/library-dump.service b/.config/systemd/user/library-dump.service index 261513e..9372565 100644 --- a/.config/systemd/user/library-dump.service +++ b/.config/systemd/user/library-dump.service @@ -5,7 +5,7 @@ StartLimitBurst=4 [Service] Type=oneshot -ExecStart=%h/.local/bin/library-dump +ExecStart=%h/.local/bin/db-dump library_manager Library day # 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/weather-dump.service b/.config/systemd/user/weather-dump.service index 2793953..65e9759 100644 --- a/.config/systemd/user/weather-dump.service +++ b/.config/systemd/user/weather-dump.service @@ -5,7 +5,7 @@ StartLimitBurst=4 [Service] Type=oneshot -ExecStart=%h/.local/bin/weather-dump +ExecStart=%h/.local/bin/db-dump ens-forecast Weather week # MariaDB not up yet (e.g. caught up right after boot)? retry a few times Restart=on-failure RestartSec=5min diff --git a/.local/bin/db-dump b/.local/bin/db-dump new file mode 100755 index 0000000..de8b7fa --- /dev/null +++ b/.local/bin/db-dump @@ -0,0 +1,38 @@ +#!/bin/bash +# Usage: db-dump PROJECT DATABASE week|day +# Dump a MariaDB database to ~/.local/share/PROJECT/dumps with login ~/.config/PROJECT/dump.cnf. +# week: per-ISO-week files, keeping the two newest; day: per-weekday files (seven self-rotating copies). +set -euo pipefail + +if (($# != 3)) || [[ $3 != week && $3 != day ]]; then + echo "usage: ${0##*/} PROJECT DATABASE week|day" >&2 + exit 2 +fi +project=$1 db=$2 mode=$3 + +cnf=${XDG_CONFIG_HOME:-$HOME/.config}/$project/dump.cnf +dir=${XDG_DATA_HOME:-$HOME/.local/share}/$project/dumps +if [[ $mode == week ]]; then + out=$dir/$db-$(date +%G-W%V).sql.gz +else + out=$dir/$db-$(LC_ALL=C date +%a).sql.gz +fi +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 "$db" | gzip > "$tmp" +mv "$tmp" "$out" + +# Weekly names sort by week, so all but the last $keep are the old ones +if [[ $mode == week ]]; then + dumps=("$dir/$db"-[0-9][0-9][0-9][0-9]-W[0-9][0-9].sql.gz) + if ((${#dumps[@]} > keep)); then + rm -f -- "${dumps[@]:0:${#dumps[@]}-keep}" + fi +fi diff --git a/.local/bin/garmin-dump b/.local/bin/garmin-dump deleted file mode 100755 index a8bdee0..0000000 --- a/.local/bin/garmin-dump +++ /dev/null @@ -1,24 +0,0 @@ -#!/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 diff --git a/.local/bin/library-dump b/.local/bin/library-dump deleted file mode 100755 index 7abffcd..0000000 --- a/.local/bin/library-dump +++ /dev/null @@ -1,17 +0,0 @@ -#!/bin/bash -# Dump the Library database to a per-weekday file (seven self-rotating copies). -set -euo pipefail - -cnf=${XDG_CONFIG_HOME:-$HOME/.config}/library_manager/dump.cnf -dir=${XDG_DATA_HOME:-$HOME/.local/share}/library_manager/dumps -out=$dir/Library-$(LC_ALL=C date +%a).sql.gz - -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 Library | gzip > "$tmp" -mv "$tmp" "$out" diff --git a/.local/bin/weather-dump b/.local/bin/weather-dump deleted file mode 100755 index 9e092de..0000000 --- a/.local/bin/weather-dump +++ /dev/null @@ -1,24 +0,0 @@ -#!/bin/bash -# Dump the Weather database (ens-forecast) to a per-ISO-week file, keeping the two newest weeks. -set -euo pipefail - -cnf=${XDG_CONFIG_HOME:-$HOME/.config}/ens-forecast/dump.cnf -dir=${XDG_DATA_HOME:-$HOME/.local/share}/ens-forecast/dumps -out=$dir/Weather-$(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 Weather | gzip > "$tmp" -mv "$tmp" "$out" - -# Names sort by week, so all but the last $keep are the old ones -dumps=("$dir"/Weather-*.sql.gz) -if (( ${#dumps[@]} > keep )); then - rm -f -- "${dumps[@]:0:${#dumps[@]}-keep}" -fi