]> arthur.barton.de Git - ax-linux.git/blob - btrfs/mksnapshot/btrfs-mksnapshot
btrfs-mksnapshot[-rootfs]: Clean up, use findmnt(8)
[ax-linux.git] / btrfs / mksnapshot / btrfs-mksnapshot
1 #!/bin/bash
2 #
3 # btrfs-mksnapshot -- Make snapshots of btrfs filesystems
4 # Copyright (c)2013-2020 Alexander Barton (alex@barton.de)
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11
12 NAME=$(basename "$0")
13
14 Usage()
15 {
16         echo "Usage: $NAME [-1] [<path>]"; echo
17         echo " -1      Create only one shapshot per day."
18         echo " -n      Dry run, perform a trial run with no changes made."
19         echo " <path>  Volume root path (default: \"/\")."
20         echo; exit 1
21 }
22
23 ONLY_ONE_PER_DAY=
24 DRY_RUN=
25
26 while [ $# -ge 1 ]; do
27         case "$1" in
28           [^-]*)
29                 break
30                 ;;
31           -1)
32                 ONLY_ONE_PER_DAY=1
33                 ;;
34           -n)
35                 DRY_RUN=1
36                 ;;
37           *)
38                 Usage
39         esac
40         shift
41 done
42 [ $# -gt 1 ] && Usage
43
44 [ -z "$DRY_RUN" ] || echo "ONLY_ONE_PER_DAY=$ONLY_ONE_PER_DAY"
45
46 # The subvolume to snapshot, "/" by default
47 [ $# -ge 1 ] && VOLUME_PATH="$1" || VOLUME_PATH="/"
48
49 [ -z "$DRY_RUN" ] || echo "VOLUME_PATH=$VOLUME_PATH"
50
51 # Detect btrfs filesystem
52 FS_NAME=$(btrfs filesystem show -m "$VOLUME_PATH" 2>/dev/null | head -1 | cut -d"'" -f2)
53 if [ -z "$FS_NAME" ]; then
54         echo "$NAME: Failed to detect btrfs filesystem label for \"$VOLUME_PATH\"!"
55         exit 1
56 fi
57
58 [ -z "$DRY_RUN" ] || echo "FS_NAME=$FS_NAME"
59
60 # Detect btrfs subvolume name
61 VOLUME_NAME=$(findmnt -nf -o source "$VOLUME_PATH" | grep -F '[' | sed -r 's|^.*\[/(.*)\]|\1|')
62 if [ -z "$VOLUME_NAME" ] && [ "$VOLUME_PATH" = "/" ]; then
63         VOLUME_NAME=$(btrfs subvolume get-default / | cut -d' ' -f9)
64 fi
65 if [ -z "$VOLUME_NAME" ]; then
66         echo "$NAME: Failed to detect btrfs subvolume name for \"$VOLUME_PATH\"!"
67         exit 1
68 fi
69 BASE_VOLUME_NAME=$(echo "$VOLUME_NAME" | cut -d'@' -f1)
70
71 if [ -n "$DRY_RUN" ]; then
72         echo "VOLUME_NAME=$VOLUME_NAME"
73         echo "BASE_VOLUME_NAME=$BASE_VOLUME_NAME"
74 fi
75
76 # Detect mount point of the whole btrfs filesystem
77 FS_MOUNT_PATH="/media/$FS_NAME"
78 if [ ! -d "$FS_MOUNT_PATH" ]; then
79         echo "$NAME: Directory \"$FS_MOUNT_PATH\" does not exist!"
80         exit 1
81 fi
82 if ! grep -q " $FS_MOUNT_PATH btrfs " /proc/mounts; then
83         echo "$NAME: btrfs \"$FS_NAME\" seems not to be mounted on \"$FS_MOUNT_PATH\"!"
84         exit 1
85 fi
86
87 [ -z "$DRY_RUN" ] || echo "FS_MOUNT_PATH=$FS_MOUNT_PATH"
88
89 # Generate snapshot name
90 i=1
91 date=$(date +%Y%m%d)
92 while true; do
93         id="$date-$i"
94         [ -e "$FS_MOUNT_PATH/$BASE_VOLUME_NAME@$id" ] || break
95         if [ -n "$ONLY_ONE_PER_DAY" ]; then
96                 echo "Snapshot \"$BASE_VOLUME_NAME@$id\" of \"$VOLUME_PATH\" (\"$VOLUME_NAME\") in btrfs \"$FS_NAME\" already exists."
97                 exit 0
98         fi
99         i=$((i + 1))
100 done
101 NEW_VOLUME_NAME="$BASE_VOLUME_NAME@$id"
102
103 [ -z "$DRY_RUN" ] || echo "NEW_VOLUME_NAME=$NEW_VOLUME_NAME"
104
105 [ -z "$DRY_RUN" ] || exit 0
106
107 echo "Creating snapshot of \"$VOLUME_PATH\" (\"$VOLUME_NAME\") in btrfs \"$FS_NAME\" ..."
108 set -x
109
110 cd "$FS_MOUNT_PATH" || exit 1
111 btrfs subvolume snapshot -r "$VOLUME_NAME" "$NEW_VOLUME_NAME"
112 find . -maxdepth 1 \( -name "$BASE_VOLUME_NAME" -o \
113  -name "$BASE_VOLUME_NAME"@'*' \) -exec ls -1d {} \; | sort | column