<commit_before>#!/bin/bash

# Unofficial bash strict mode http://redsymbol.net/articles/unofficial-bash-strict-mode/
set -eu
set -o pipefail
IFS=$'\n\t'

if [[ $EUID -ne 0 ]]; then
   echo "This script must be run as root" 1>&2
   exit 1
fi

COMMAND="$1"
shift

case "$COMMAND" in

  snapshot)

    NAME="backup-$(date +%Y%m%d-%H%M%S)"

    echo "Snapshotting with name=$NAME"

    zfs snapshot "rpool/ROOT/ubuntu-1@$NAME"
    zfs snapshot "rpool/docker@$NAME"
    zfs snapshot "rpool/vms@$NAME"
    zfs snapshot "tank/vms@$NAME"
    zfs snapshot "tank/legacy@$NAME"
    zfs snapshot "tank/pictures@$NAME"
    zfs snapshot "tank/timemachine@$NAME"

    ;;

  backup)

    FROM="$1"
    TO="$2"

    echo "Rolling back backup to: $FROM"

    zfs rollback "backup/ubuntu@$FROM"
    zfs rollback "backup/docker@$FROM"
    zfs rollback "backup/vms_ssd@$FROM"
    zfs rollback "backup/vms_hdd@$FROM"
    zfs rollback "backup/legacy@$FROM"
    zfs rollback "backup/pictures@$FROM"
    zfs rollback "backup/timemachine@$FROM"

    echo "Incremental sync to backup: $FROM -> $TO"

    zfs send -pv -I "rpool/ROOT/ubuntu-1@$FROM" "rpool/ROOT/ubuntu-1@$TO" | zfs receive -v backup/ubuntu
    zfs send -pv -I "rpool/docker@$FROM" "rpool/docker@$TO" | zfs receive -v backup/docker
    zfs send -pv -I "rpool/vms@$FROM" "rpool/vms@$TO" | zfs receive -v backup/vms_ssd
    zfs send -pv -I "tank/vms@$FROM" "tank/vms@$TO" | zfs receive -v backup/vms_hdd
    zfs send -pv -I "tank/legacy@$FROM" "tank/legacy@$TO" | zfs receive -v backup/legacy
    zfs send -pv -I "tank/pictures@$FROM" "tank/pictures@$TO" | zfs receive -v backup/pictures
    zfs send -pv -I "tank/timemachine@$FROM" "tank/timemachine@$TO" | zfs receive -v backup/timemachine

    ;;

  destroy)

    SNAPSHOT="$1"

    echo "Destroying snapshots: $SNAPSHOT"

    zfs destroy "rpool/ROOT/ubuntu-1@$SNAPSHOT"
    zfs destroy "rpool/docker@$SNAPSHOT"
    zfs destroy "rpool/vms@$SNAPSHOT"
    zfs destroy "tank/vms@$SNAPSHOT"<commit_msg>Improve zfs script<commit_after>#!/bin/bash

# Unofficial bash strict mode http://redsymbol.net/articles/unofficial-bash-strict-mode/
set -eu
set -o pipefail
IFS=$'\n\t'

if [[ $EUID -ne 0 ]]; then
   echo "This script must be run as root" 1>&2
   exit 1
fi

COMMAND="$1"
shift

case "$COMMAND" in

  snapshot)

    NAME="backup-$(date +%Y%m%d-%H%M%S)"

    echo "Snapshotting with name=$NAME"

    zfs snapshot "rpool/ROOT/ubuntu-1@$NAME"
    zfs snapshot "rpool/docker@$NAME"
    zfs snapshot "rpool/vms@$NAME"
    zfs snapshot "tank/vms@$NAME"
    zfs snapshot "tank/legacy@$NAME"
    zfs snapshot "tank/pictures@$NAME"
    zfs snapshot "tank/timemachine@$NAME"

    ;;

  backup)

    DISK="$1"
    FROM="$2"

    echo "Destroying snapshots: backup-disk-$DISK"

    zfs destroy -f "rpool/ROOT/ubuntu-1@backup-disk-$DISK"
    zfs destroy -f "rpool/docker@backup-disk-$DISK"
    zfs destroy -f "rpool/vms@backup-disk-$DISK"
    zfs destroy -f "tank/vms@backup-disk-$DISK"
    zfs destroy -f "tank/legacy@backup-disk-$DISK"
    zfs destroy -f "tank/pictures@backup-disk-$DISK"
    zfs destroy -f "tank/timemachine@backup-disk-$DISK"

    zfs destroy -f "backup-$DISK/ubuntu@backup-disk-$DISK"
    zfs destroy -f "backup-$DISK/docker@backup-disk-$DISK"
    zfs destroy -f "backup-$DISK/vms_ssd@backup-disk-$DISK"
    zfs destroy -f "backup-$DISK/vms_hdd@backup-disk-$DISK"
    zfs destroy -f "backup-$DISK/legacy@backup-disk-$DISK"
    zfs destroy -f "backup-$DISK/pictures@backup-disk-$DISK"
    zfs destroy -f "backup-$DISK/timemachine@backup-disk-$DISK"

    echo "Rolling back backup to: $FROM"

    zfs rollback "backup-$DISK/ubuntu@$FROM"
    zfs rollback "backup-$DISK/docker@$FROM"
    zfs rollback "backup-$DISK/vms_ssd@$FROM"
    zfs rollback "backup-$DISK/vms_hdd@$FROM"
    zfs rollback "backup-$DISK/legacy@$FROM"
    zfs rollback "backup-$DISK/pictures@$FROM"
    zfs rollback "backup-$DISK/timemachine@$FROM"

    echo "Snapshotting with name=backup-disk-$DISK"

    zfs snapshot "rpool/ROOT/ubuntu-1@backup-disk-$DISK"
    zfs snapshot "rpool/docker@backup-disk-$DISK"
    zfs snapshot "rpool/vms@backup-disk-$DISK"
    zfs snapshot "tank/vms@backup-disk-$DISK"
    zfs snapshot "tank/legacy@backup-disk-$DISK"
    zfs snapshot "tank/pictures@backup-disk-$DISK"
    zfs snapshot "tank/timemachine@backup-disk-$DISK"

    echo "Incremental sync to backup: $FROM -> backup-disk-$DISK"

    zfs send -pv -I "rpool/ROOT/ubuntu-1@$FROM" "rpool/ROOT/ubuntu-1@backup-disk-$DISK" | zfs receive -v "backup-$DISK/ubuntu"
    zfs send -pv -I "rpool/docker@$FROM" "rpool/docker@backup-disk-$DISK" | zfs receive -v "backup-$DISK/docker"
    zfs send -pv -I "rpool/vms@$FROM" "rpool/vms@backup-disk-$DISK" | zfs receive -v "backup-$DISK/vms_ssd"
    zfs send -pv -I "tank/vms@$FROM" "tank/vms@backup-disk-$DISK" | zfs receive -v "backup-$DISK/vms_hdd"
    zfs send -pv -I "tank/legacy@$FROM" "tank/legacy@backup-disk-$DISK" | zfs receive -v "backup-$DISK/legacy"
    zfs send -pv -I "tank/pictures@$FROM" "tank/pictures@backup-disk-$DISK" | zfs receive -v "backup-$DISK/pictures"
    zfs send -pv -I "tank/timemachine@$FROM" "tank/timemachine@backup-disk-$DISK" | zfs receive -v "backup-$DISK/timemachine"

    ;;

  destroy)

    SNAPSHOT="$1"

    echo "Destroying snapshots: $SNAPSHOT"

    zfs destroy "rpool/ROOT/ubuntu-1@$SNAPSHOT"
    zfs destroy "rpool/docker@$SNAPSHOT"
    zfs destroy "rpool/vms@$SNAPSHOT"
    zfs destroy "tank/vms@$SNAPSHOT"
{'fraction_non_alphanumeric': 0.1560374931078846, 'fraction_numerical': 0.004778533357838633, 'mean_word_length': 3.44971381847915, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5274610', 'n_tokens_mistral': 2298, 'n_tokens_neox': 2196, 'n_words': 460}