Skip to content

Instantly share code, notes, and snippets.

@HikariKnight
Last active January 21, 2022 06:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HikariKnight/fe7621359e415ca7fa8ab233eb9d3e6d to your computer and use it in GitHub Desktop.
Save HikariKnight/fe7621359e415ca7fa8ab233eb9d3e6d to your computer and use it in GitHub Desktop.
Custom btrfs snapshots for when you need different naming schemes than timeshift or snapper and keep them for 1 month
#!/bin/bash
TIMESTAMP=`date +"%Y.%m.%d-%H.%M.%S"`
SNAP="btrfs subvolume snapshot -r"
SOURCE_DIR="/Tank/data"
SNAP_DIR="/Tank/data/.snapshots"
SNAP_NAME="@GMT_$TIMESTAMP"
$SNAP $SOURCE_DIR $SNAP_DIR/$SNAP_NAME
rm -rf $SNAP_DIR/.snapshots/@SNAP_NAME/.snapshots/*
# Cleanup old snapshots
for D in `find "$SNAP_DIR/" -maxdepth 1 -type d`
do
# If the folder is not the parent folder
if [[ "$D" != "$SNAP_DIR/" ]]
then
CREATED=$(btrfs subvolume show $D | grep -oP 'Creation time:\s*\K\d.*$' | perl -pe "s/(\d{4}-\d{2}-\d{2}).+/\1/")
TIMELINE=$((($(date +%s)-$(date +%s --date "$CREATED"))/(3600*24)))
# If the snapshot is older than 1 month
if [[ $TIMELINE > 31 ]]
then
echo snapshot $D is $TIMELINE days old, deleting
btrfs sub delete "$D"
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment