Merge the database dump scripts into db-dump
garmin-dump, weather-dump and library-dump differed only in project, database and rotation; one db-dump PROJECT DATABASE week|day replaces them. The units keep their names and only change ExecStart.
This commit is contained in:
7 files changed
+41
-68
No files matched your search
Executable
+38
@@ -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
|
||||
Reference in new issue
Block a user