]> arthur.barton.de Git - ax-linux.git/blob - btrfs/mksnapshot/btrfs-mksnapshot
btrfs-mksnapshot: sort output
[ax-linux.git] / btrfs / mksnapshot / btrfs-mksnapshot
1 #!/bin/bash -e
2 #
3 # btrfs-mksnapshot -- Make snapshots of btrfs filesystems
4 # Copyright (c)2013-2014 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 function 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=`grep " btrfs " /etc/fstab \
53         | grep "^LABEL=" \
54         | grep " $VOLUME_PATH " \
55         | cut -d'"' -f2 \
56         | cut -d'=' -f2- \
57         | cut -d' ' -f1`
58 if [ -z "$FS_NAME" ]; then
59         echo "$NAME: Failed to detect btrfs filesystem label for \"$VOLUME_PATH\"!"
60         exit 1
61 fi
62
63 [ -z "$DRY_RUN" ] || echo "FS_NAME=$FS_NAME"
64
65 # Detect btrfs subvolume name
66 VOLUME_NAME=`grep " btrfs " /etc/fstab \
67         | grep "subvol=" \
68         | grep " $VOLUME_PATH " \
69         | sed -r 's|.*subvol=([[:alnum:]]*).*|\1|'`
70 if [ -z "$VOLUME_NAME" -a "$VOLUME_PATH" = "/" ]; then
71         VOLUME_NAME=`btrfs subvolume get-default / | cut -d' ' -f9`
72 fi
73 if [ -z "$VOLUME_NAME" ]; then
74         echo "$NAME: Failed to detect btrfs subvolume name for \"$VOLUME_PATH\"!"
75         exit 1
76 fi
77 BASE_VOLUME_NAME=`echo "$VOLUME_NAME" | cut -d'@' -f1`
78
79 if [ -n "$DRY_RUN" ]; then
80         echo "VOLUME_NAME=$VOLUME_NAME"
81         echo "BASE_VOLUME_NAME=$BASE_VOLUME_NAME"
82 fi
83
84 # Detect mount point of the whole btrfs filesystem
85 FS_MOUNT_PATH="/media/$FS_NAME"
86 if [ ! -d "$FS_MOUNT_PATH" ]; then
87         echo "$NAME: Directory \"$FS_MOUNT_PATH\" does not exist!"
88         exit 1
89 fi
90 grep " btrfs " /proc/mounts | grep " $FS_MOUNT_PATH " >/dev/null 2>&1
91 if [ $? -ne 0 ]; then
92         echo "$NAME: btrfs \"$FS_NAME\" seems not to be mounted on \"$FS_MOUNT_PATH\"!"
93         exit 1
94 fi
95
96 [ -z "$DRY_RUN" ] || echo "FS_MOUNT_PATH=$FS_MOUNT_PATH"
97
98 # Generate snapshot name
99 i=1
100 date=`date +%Y%m%d`
101 while true; do
102         id="$date-$i"
103         [ -e "$FS_MOUNT_PATH/$BASE_VOLUME_NAME@$id" ] || break
104         if [ -n "$ONLY_ONE_PER_DAY" ]; then
105                 echo "Snapshot \"$BASE_VOLUME_NAME@$id\" of \"$VOLUME_PATH\" (\"$VOLUME_NAME\") in btrfs \"$FS_NAME\" already exists."
106                 exit 0
107         fi
108         i=`expr $i + 1`
109 done
110 NEW_VOLUME_NAME="$BASE_VOLUME_NAME@$id"
111
112 [ -z "$DRY_RUN" ] || echo "NEW_VOLUME_NAME=$NEW_VOLUME_NAME"
113
114 [ -z "$DRY_RUN" ] || exit 0
115
116 echo "Creating snapshot of \"$VOLUME_PATH\" (\"$VOLUME_NAME\") in btrfs \"$FS_NAME\" ..."
117 set -x
118
119 cd "$FS_MOUNT_PATH"
120 btrfs subvolume snapshot "$VOLUME_NAME" "$NEW_VOLUME_NAME"
121 find . -maxdepth 1 \( -name "$BASE_VOLUME_NAME" -o \
122  -name "$BASE_VOLUME_NAME"@'*' \) -exec ls -1d {} \; | sort | column