| 1 |
#!/bin/bash |
| 2 |
|
| 3 |
# Copyright (c) 2023 Arnaud Rebillout <[email protected]> |
| 4 |
# Distributed under the same license as mirrorbits. |
| 5 |
|
| 6 |
set -euo pipefail |
| 7 |
|
| 8 |
USAGE="Usage: $(basename $0) [-f] <period>:<retention>... [--] [REDIS_CLI_ARGS...] |
| 9 |
|
| 10 |
Delete old Mirrorbits stats. |
| 11 |
|
| 12 |
This script deletes old Mirrorbits stats, by using the Redis client, and |
| 13 |
directly deleting the keys in the Redis database. It asks for confirmation |
| 14 |
before acting, unless you provide -f in argument. |
| 15 |
|
| 16 |
Mirrorbits maintains daily, monthly and yearly stats. The pairs |
| 17 |
<period>:<retention> define how many stats to keep for each period. |
| 18 |
|
| 19 |
For example, the command: |
| 20 |
|
| 21 |
$(basename $0) daily:30 monthly:12 -- -p 6380 |
| 22 |
|
| 23 |
will keep only the last 30 daily stats and the last 12 monthly stats. Older |
| 24 |
stats will be deleted. Yearly stats are left untouched, as we didn't prodive |
| 25 |
any yearly:<retention> argument. Finally, trailing arguments are handed over |
| 26 |
to the redis-cli command, hence '-p 6380' makes redis-cli use the port 6380. |
| 27 |
" |
| 28 |
|
| 29 |
ARGS= |
| 30 |
FORCE=0 |
| 31 |
|
| 32 |
while [ $# -gt 0 ]; do |
| 33 |
case $1 in |
| 34 |
--) shift; break ;; |
| 35 |
-f|--force) FORCE=1 ;; |
| 36 |
-h|--help) echo "$USAGE"; exit 0 ;; |
| 37 |
-*) break ;; |
| 38 |
*) ARGS="$ARGS $1" ;; |
| 39 |
esac |
| 40 |
shift |
| 41 |
done |
| 42 |
|
| 43 |
# Find which redis cli to use |
| 44 |
REDIS_CLI=redis-cli |
| 45 |
for x in valkey redict keydb; do |
| 46 |
if command -v $x-cli 2>&1 >/dev/null; then |
| 47 |
REDIS_CLI=$x-cli |
| 48 |
break |
| 49 |
fi |
| 50 |
done |
| 51 |
|
| 52 |
REDIS="$REDIS_CLI $@" |
| 53 |
|
| 54 |
# Get all the STATS_* keys |
| 55 |
echo "Scanning the ${REDIS_CLI/-cli/} database for STATS_* keys, this might take a while ..." |
| 56 |
if $REDIS_CLI --help 2>&1 | grep -q -- " --count "; then |
| 57 |
KEYS=$($REDIS --scan --count 1000 --pattern "STATS_*") |
| 58 |
else |
| 59 |
KEYS=$($REDIS --scan --pattern "STATS_*") |
| 60 |
fi |
| 61 |
|
| 62 |
# Keep only the daily/monthly/yearly stats |
| 63 |
FILE_KEYS=$(echo "$KEYS" | grep "^STATS_FILE_[0-9]") |
| 64 |
MIRROR_KEYS=$(echo "$KEYS" | grep "^STATS_MIRROR_[0-9]") |
| 65 |
MIRROR_BYTES_KEYS=$(echo "$KEYS" | grep "^STATS_MIRROR_BYTES_[0-9]") |
| 66 |
|
| 67 |
# Iterate over arguments |
| 68 |
NO_KEY_TO_DELETE=1 |
| 69 |
for arg in $ARGS; do |
| 70 |
period=$(echo $arg | cut -d: -f1) |
| 71 |
retention=$(echo $arg | cut -d: -f2) |
| 72 |
|
| 73 |
case $period in |
| 74 |
daily) pattern="[A-Z]_[0-9]{4}_[01][0-9]_[0-3][0-9]$" ;; |
| 75 |
monthly) pattern="[A-Z]_[0-9]{4}_[01][0-9]$" ;; |
| 76 |
yearly) pattern="[A-Z]_[0-9]{4}$" ;; |
| 77 |
*) |
| 78 |
echo "Invalid period '$period', skipping." >&2 |
| 79 |
continue |
| 80 |
esac |
| 81 |
|
| 82 |
if ! echo "$retention" | grep -qx "[0-9]\+"; then |
| 83 |
echo "Invalid retention '$retention', skipping." >&2 |
| 84 |
continue |
| 85 |
fi |
| 86 |
|
| 87 |
for v in FILE_KEYS MIRROR_KEYS MIRROR_BYTES_KEYS; do |
| 88 |
keys=$(echo "${!v}" | grep -E "$pattern" | LC_ALL=C sort -u) |
| 89 |
toremove=$(echo "$keys" | head -n -$retention) |
| 90 |
if [ -z "$toremove" ]; then |
| 91 |
continue |
| 92 |
fi |
| 93 |
NO_KEY_TO_DELETE=0 |
| 94 |
echo "The following keys will be removed:" |
| 95 |
echo $toremove | fold -s -w 80 |
| 96 |
if [ $FORCE = 0 ]; then |
| 97 |
echo |
| 98 |
read -r -p "Proceed? [Y/n] " |
| 99 |
if [ -z "$REPLY" ]; then REPLY=Y; fi |
| 100 |
if [ "${REPLY,,}" != y ]; then |
| 101 |
echo "Skipped" |
| 102 |
continue |
| 103 |
fi |
| 104 |
fi |
| 105 |
# Remove by blocks of 100 keys |
| 106 |
while [ -n "$toremove" ]; do |
| 107 |
keys=$(echo "$toremove" | head -n 100) |
| 108 |
first=$(echo "$keys" | head -n 1) |
| 109 |
last=$(echo "$keys" | tail -n 1) |
| 110 |
echo "Removing keys from $first to $last ..." |
| 111 |
$REDIS DEL $keys |
| 112 |
toremove=$(echo "$toremove" | tail -n +101) |
| 113 |
done |
| 114 |
done |
| 115 |
done |
| 116 |
|
| 117 |
if [ $NO_KEY_TO_DELETE = 1 ]; then |
| 118 |
echo "No key to delete, nothing was done." |
| 119 |
fi |