Last active
March 27, 2025 13:51
-
-
Save tbnorth/462d97beda41d076ed482a5cc4b7b140 to your computer and use it in GitHub Desktop.
Backup / log "rotation" keeping more more recent files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
TARGET=trilium-data | |
BKUP_DIR=/home/tbrown02/data/trilium-backup | |
mkdir -p $BKUP_DIR/cull | |
OUTFILE=$(date '+%Y-%m-%d-%H-%M-trilium_bkup.tar.xz') | |
cd ~ | |
nice tar caf $BKUP_DIR/$OUTFILE $TARGET | |
# After https://unix.stackexchange.com/a/669081/319850 | |
# but using sort to handle multiple bkups per day. | |
all=$(ls $BKUP_DIR/*.xz) | |
ten_daily=$(ls $BKUP_DIR/*.xz | sort -r | sort -u -k1,3 -t- | tail -10) | |
twelve_months=$(ls $BKUP_DIR/*.xz | sort -r | sort -u -k1,2 -t- | tail -12) | |
yearly=$(ls $BKUP_DIR/*.xz | sort -r | sort -u -k1,1 -t-) | |
five_days=$(ls $BKUP_DIR/*.xz | sort | tail -$[5*3]) | |
keep=" $ten_daily $twelve_months $yearly $five_days " | |
for item in $all | |
do | |
if ! [[ "$keep" =~ "$item" ]] | |
then | |
mv $item $BKUP_DIR/cull | |
fi | |
done | |
# Demo | |
# Most recent from all years | |
# for BKUP in $(seq 0 1000); do date -d "+$[BKUP*96] hours" '+%Y-%m-%d-%H-%M-trilium_bkup.tar.xz'; done | sort -r | sort -u -k1,1 -t- | |
# Most recent from each of last 12 months | |
# for BKUP in $(seq 0 1000); do date -d "+$[BKUP*12] hours" '+%Y-%m-%d-%H-%M-trilium_bkup.tar.xz'; done | sort -r | sort -u -k1,2 -t- | tail -12 | |
# Most recent from each of last 10 days | |
# for BKUP in $(seq 0 1000); do date -d "+$[BKUP*12] hours" '+%Y-%m-%d-%H-%M-trilium_bkup.tar.xz'; done | sort -r | sort -u -k1,3 -t- | tail -10 | |
# All for last 5 days at three bkups per day | |
# NOTE if you skip days running the backup, e.g. cron job doesn't run Sat/Sun, | |
# this may include files from more than '5' days ago, but they will be culled eventually. | |
# for BKUP in $(seq 0 1000); do date -d "+$[BKUP*8] hours" '+%Y-%m-%d-%H-%M-trilium_bkup.tar.xz'; done | sort | tail -$[5*3] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment