add stats to backups

This commit is contained in:
2025-10-13 17:27:13 -05:00
parent 2f627902f3
commit 30302af17b
3 changed files with 65 additions and 69 deletions

View File

@@ -228,19 +228,6 @@ in
units = "imperial";
hour-format = "24h";
}
{
type = "custom-api";
title = "uptime";
title-url = "\${uptimekuma_url}";
url = "\${uptimekuma_url}/api/status-page/\${uptimekuma_page}";
subrequests = {
heartbeats = {
url = "\${uptimekuma_url}/api/status-page/heartbeat/\${uptimekuma_page}";
};
};
cache = "10m";
template = "{{ $hb := .Subrequest \"heartbeats\" }}\n\n{{ if not (.JSON.Exists \"publicGroupList\") }}\n<p class=\"color-negative\">Error reading response</p>\n{{ else if eq (len (.JSON.Array \"publicGroupList\")) 0 }}\n<p>No monitors found</p>\n{{ else }}\n\n<ul class=\"dynamic-columns list-gap-8\">\n {{ range .JSON.Array \"publicGroupList\" }}\n {{ range .Array \"monitorList\" }}\n {{ $id := .String \"id\" }}\n {{ $hbArray := $hb.JSON.Array (print \"heartbeatList.\" $id) }}\n <div class=\"flex items-center gap-12\">\n <a class=\"size-title-dynamic color-highlight text-truncate block grow\" href=\"\${uptimekuma_url}/dashboard/{{ $id }}\"\n target=\"_blank\" rel=\"noreferrer\">\n {{ .String \"name\" }} </a>\n\n {{ if gt (len $hbArray) 0 }}\n {{ $latest := index $hbArray (sub (len $hbArray) 1) }}\n {{ if eq ($latest.Int \"status\") 1 }}\n <div>{{ $latest.Int \"ping\" }}ms</div>\n <div class=\"monitor-site-status-icon-compact\" title=\"OK\">\n <svg fill=\"var(--color-positive)\" xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 20 20\">\n <path fill-rule=\"evenodd\"\n d=\"M10 18a8 8 0 1 0 0-16 8 8 0 0 0 0 16Zm3.857-9.809a.75.75 0 0 0-1.214-.882l-3.483 4.79-1.88-1.88a.75.75 0 1 0-1.06 1.061l2.5 2.5a.75.75 0 0 0 1.137-.089l4-5.5Z\"\n clip-rule=\"evenodd\"></path>\n </svg>\n </div>\n {{ else }}\n <div><span class=\"color-negative\">DOWN</span></div>\n <div class=\"monitor-site-status-icon-compact\" title=\"{{ if $latest.Exists \"msg\" }}{{ $latest.String \"msg\" }}{{ else\n }}Error{{ end }}\">\n <svg fill=\"var(--color-negative)\" xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 20 20\">\n <path fill-rule=\"evenodd\"\n d=\"M8.485 2.495c.673-1.167 2.357-1.167 3.03 0l6.28 10.875c.673 1.167-.17 2.625-1.516 2.625H3.72c-1.347 0-2.189-1.458-1.515-2.625L8.485 2.495ZM10 5a.75.75 0 0 1 .75.75v3.5a.75.75 0 0 1-1.5 0v-3.5A.75.75 0 0 1 10 5Zm0 9a1 1 0 1 0 0-2 1 1 0 0 0 0 2Z\"\n clip-rule=\"evenodd\"></path>\n </svg>\n </div>\n {{ end }}\n {{ else }}\n <div><span class=\"color-negative\">No data</span></div>\n <div class=\"monitor-site-status-icon-compact\" title=\"No data available\">\n <svg fill=\"var(--color-negative)\" xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 20 20\">\n <path d=\"M10 18a8 8 0 1 1 0-16 8 8 0 0 1 0 16zm0-2a6 6 0 1 0 0-12 6 6 0 0 0 0 12zm-.75-8a.75.75 0 0 1 1.5 0v3a.75.75 0 0 1-1.5 0V8zm.75 5a1 1 0 1 1 0 2 1 1 0 0 1 0-2z\"/>\n </svg>\n </div>\n {{ end }}\n </div>\n {{ end }}\n {{ end }}\n</ul>\n{{ end }}\n";
}
{
type = "markets";
markets = [

View File

@@ -54,29 +54,56 @@ in
# EnvironmentFile = config.modules.system.backups.passphraseFile;
# the actual script borg is using
ExecStart = pkgs.writeShellScript "borg-backup" ''
set -euo pipefail
export BORG_PASSPHRASE="$(cat ${cfg.passwd_file})"
export BORG_REPO="${cfg.repo}"
timestamp="$(date +'%Y-%m-%d_%H:%M:%S')"
mode=${cfg.mode}
backup() {
set -euo pipefail
export BORG_PASSPHRASE="$(cat ${cfg.passwd_file})"
export BORG_REPO="${cfg.repo}"
timestamp="$(date +'%Y-%m-%d_%H:%M:%S')"
mode=${cfg.mode}
# init repo in needed
if ! borg info "$BORG_REPO" >/dev/null 2>&1; then
echo "Initializing Borg repo at $BORG_REPO"
borg init --encryption=repokey "$BORG_REPO"
fi
# init repo in needed
if ! borg info "$BORG_REPO" >/dev/null 2>&1; then
echo "Initializing Borg repo at $BORG_REPO"
borg init --encryption=repokey "$BORG_REPO"
fi
borg break-lock "$BORG_REPO" || true
borg break-lock "$BORG_REPO" || true
echo "starting backup at $timestamp"
echo "starting backup at $timestamp"
if [ "$mode" = "split" ]; then
# loop for each backup
${lib.concatStringsSep "\n\n" (lib.mapAttrsToList (bak_name: bak_paths:
''
echo "------------ Backing up ${bak_name} ------------"
archive="$timestamp-${bak_name}"
echo "backing up: ${lib.concatStringsSep " " bak_paths.paths} $archive"
if [ "$mode" = "split" ]; then
# loop for each backup
${lib.concatStringsSep "\n\n" (lib.mapAttrsToList (bak_name: bak_paths:
''
echo "------------ Backing up ${bak_name} ------------"
archive="$timestamp-${bak_name}"
echo "backing up: ${lib.concatStringsSep " " bak_paths.paths} $archive"
borg create \
--verbose \
--filter AME \
--list \
--stats \
--show-rc \
--compression lzma,9 \
"$BORG_REPO::$archive" \
${lib.concatStringsSep " " bak_paths.paths}
echo "pruning old backups for ${bak_name}..."
borg prune -v --list "$BORG_REPO" \
--glob-archives "*-${bak_name}" \
--keep-daily=7 \
--keep-weekly=52 \
--keep-monthly=-1
echo "backup run complete at \"$BORG_REPO::$archive\""
''
) cfg.baks)}
exit 0
else
# flatten all paths from cfg.baks into one big list
all_paths="${
lib.concatStringsSep " "
(lib.flatten
(lib.mapAttrsToList (_: bak: bak.paths) cfg.baks))
}"
borg create \
--verbose \
--filter AME \
@@ -84,44 +111,24 @@ in
--stats \
--show-rc \
--compression lzma,9 \
"$BORG_REPO::$archive" \
${lib.concatStringsSep " " bak_paths.paths}
echo "pruning old backups for ${bak_name}..."
borg prune -v --list "$BORG_REPO" \
--glob-archives "*-${bak_name}" \
--keep-daily=7 \
--keep-weekly=52 \
--keep-monthly=-1
echo "backup run complete at \"$BORG_REPO::$archive\""
''
) cfg.baks)}
exit 0
else
# flatten all paths from cfg.baks into one big list
all_paths="${
lib.concatStringsSep " "
(lib.flatten
(lib.mapAttrsToList (_: bak: bak.paths) cfg.baks))
}"
borg create \
--verbose \
--filter AME \
--list \
--stats \
--show-rc \
--compression lzma,9 \
"$BORG_REPO::$timestamp-${toString config.networking.hostName}" \
$all_paths
"$BORG_REPO::$timestamp-${toString config.networking.hostName}" \
$all_paths
echo "pruning old backups for ${toString config.networking.hostName}..."
borg prune -v --list "$BORG_REPO" \
--glob-archives "*-${toString config.networking.hostName}" \
--keep-daily=7 \
--keep-weekly=52 \
--keep-monthly=-1
echo "backup run complete at \"$BORG_REPO::${toString config.networking.hostName}\""
exit 0
fi
echo "pruning old backups for ${toString config.networking.hostName}..."
borg prune -v --list "$BORG_REPO" \
--glob-archives "*-${toString config.networking.hostName}" \
--keep-daily=7 \
--keep-weekly=52 \
--keep-monthly=-1
echo "backup run complete at \"$BORG_REPO::${toString config.networking.hostName}\""
exit 0
fi
}
time -f "\
Real time: %E\n\
User CPU: %U\n\
Sys CPU: %S\n\
Max memory: %M KB" backup
'';
};
};

View File

@@ -95,6 +95,8 @@ alias stat='sudo systemctl status'
alias restart='sudo systemctl restart'
alias start='sudo systemctl start'
alias stop='sudo systemctl stop'
alias jou='sudo journalctl -xeu'
alias live='sudo journalctl -o short-iso -n 50 -fu'
# shortcuts
alias chil='nvim ~/documents/holocron/tech/devices/yveltal/chilton'