summaryrefslogtreecommitdiff
path: root/src/samizdat-daily-snapshot-root
blob: c57524c57323fd7cd1cc1cfd913d956a2d305def (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#/bin/sh
[ "$(id -u)" = 0 ] || exec sudo "$0" "$@" || exit

is_subvol()
{
    btrfs subvolume show "$1" >/dev/null 2>&1
}

btrfs_subvolumes()
{
    btrfs subvolume list -a -t "${1:-/}" | awk '{print $4}' | sed -Ene 's?^(<FS_TREE>/)\?root/?/?p'
}

snapit()
{
    local dev mnt target

    mnt=$(mktemp -d /tmp/mnt.XXXXXX) || return
    src=$mnt/$1/
    target=$mnt/${2:-daily/}${1%/}~$(date -I)
    dev=$(btrfs fi sh / | sed -ne 's/.* path //p' | tail -n1) || return

    mount "$dev" -o subvol=/ "$mnt"
    if [ -e "$target" ]
    then
      :
    else
      mkdir -p "$(dirname "$target")"
      btrfs subvolume snapshot -r "$src" "$target"
    fi
    umount "$mnt"
    rmdir "$mnt"
}

do_not_snap()
{
  case "${1#/root/}" in
    /var/cache/apt/archives|/home/*/.cache|/home/*/src/fsmgr/_build|/home/*/.stack|/home/*/.rustup)
      true
    ;;
    *)
      false
    ;;
  esac
}

snapit /root

btrfs_subvolumes | while read f
do
    if is_subvol "$f"
    then
        do_not_snap "$f" || snapit /root/"${f#/}"
    fi
done