diff options
| author | Max Bossing <info@maxbossing.de> | 2025-10-05 02:30:05 +0200 |
|---|---|---|
| committer | Max Bossing <info@maxbossing.de> | 2025-10-05 02:30:05 +0200 |
| commit | a8c5788d615a572cae5802d63cfec28bbae7a1af (patch) | |
| tree | 933194f5d91d3710d3588d6afba3ac96f7462998 | |
| parent | 96b5b9dbd60e1a6bb4a1b10d7cd7954d5a49ad68 (diff) | |
| -rw-r--r-- | scripts/rollover/LICENSE | 8 | ||||
| -rw-r--r-- | scripts/rollover/README | 10 | ||||
| -rw-r--r-- | scripts/rollover/calibre-rollover.sh | 2 | ||||
| -rw-r--r-- | scripts/rollover/forge-rollover.sh | 2 | ||||
| -rw-r--r-- | scripts/rollover/rollover.sh | 54 |
5 files changed, 76 insertions, 0 deletions
diff --git a/scripts/rollover/LICENSE b/scripts/rollover/LICENSE new file mode 100644 index 0000000..18a7e6b --- /dev/null +++ b/scripts/rollover/LICENSE @@ -0,0 +1,8 @@ +The MIT License (MIT) +Copyright © 2025 c41ro + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/scripts/rollover/README b/scripts/rollover/README new file mode 100644 index 0000000..fd2dd08 --- /dev/null +++ b/scripts/rollover/README @@ -0,0 +1,10 @@ +# backup.sh + +This rolls over a weeks worth of backups made by the backup script and refiles them under the Week number + +1. Drop this script in your home and make it executable. +3. For every thing you want to backup, create a bash script that sets the REMOTE_LOCATION and BACKUP_DIR variable. +3. Add a new cron job like this `@weekly BASH_ENV=$HOME/environment.sh $HOME/rollover.sh >> $HOME/rollover.log`. + + +License: It's like 10 lines bro. MIT if you must. diff --git a/scripts/rollover/calibre-rollover.sh b/scripts/rollover/calibre-rollover.sh new file mode 100644 index 0000000..bce194b --- /dev/null +++ b/scripts/rollover/calibre-rollover.sh @@ -0,0 +1,2 @@ +#!/usr/bin/bash +BACKUP_DIR="/home/c41ro/backups/books" diff --git a/scripts/rollover/forge-rollover.sh b/scripts/rollover/forge-rollover.sh new file mode 100644 index 0000000..bd906c8 --- /dev/null +++ b/scripts/rollover/forge-rollover.sh @@ -0,0 +1,2 @@ +#!/usr/bin/bash +BACKUP_DIR="/home/c41ro/backups/forge" diff --git a/scripts/rollover/rollover.sh b/scripts/rollover/rollover.sh new file mode 100644 index 0000000..d6e3fab --- /dev/null +++ b/scripts/rollover/rollover.sh @@ -0,0 +1,54 @@ +#!/usr/bin/env bash +set -euo pipefail + +[ -d "$BACKUP_DIR" ] || { + echo "Not a directory: $BACKUP_DIR" >&2 + exit 1 +} + +ts_regex='^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}$' + +shopt -s nullglob +candidates=() +for p in "$BACKUP_DIR"/*; do + [[ -d "$p" ]] || continue + name="${p##*/}" + [[ "$name" =~ $ts_regex ]] && candidates+=("$name") +done +shopt -u nullglob + +if ((${#candidates[@]} == 0)); then + echo "No matching timestamp directories found in $BACKUP_DIR" + exit 0 +fi + +mapfile -t sorted < <(printf '%s\n' "${candidates[@]}" | LC_ALL=C sort -r) +newest="${sorted[0]}" +newest_path="${BACKUP_DIR%/}/$newest" + +for name in "${sorted[@]:1}"; do + rm -rf -- "${BACKUP_DIR%/}/$name" +done + +date_part="${newest%%T*}" + +if date --version >/dev/null 2>&1; then + weekname="$(date -d "$date_part" +%G-W%V)" +else + weekname="$(date -jf "%Y-%m-%d" "$date_part" +%G-W%V)" +fi + +target="${BACKUP_DIR%/}/${weekname}" + +if [[ "$newest_path" == "$target" ]]; then + echo "Newest directory already named '$target'. Done." + exit 0 +fi + +if [[ -e "$target" && "$target" != "$newest_path" ]]; then + echo "Target '$target' already exists. Refusing to overwrite." >&2 + exit 1 +fi + +mv -- "$newest_path" "$target" +echo "rolled $newest over to $target" |
