3 # btrfs-mksnapshot -- Make snapshots of btrfs filesystems
4 # Copyright (c)2013 Alexander Barton (alex@barton.de)
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.
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: \"/\")."
26 while [ $# -ge 1 ]; do
44 [ -z "$DRY_RUN" ] || echo "ONLY_ONE_PER_DAY=$ONLY_ONE_PER_DAY"
46 # The subvolume to snapshot, "/" by default
47 [ $# -ge 1 ] && VOLUME_PATH="$1" || VOLUME_PATH="/"
49 [ -z "$DRY_RUN" ] || echo "VOLUME_PATH=$VOLUME_PATH"
51 # Detect btrfs filesystem
52 FS_NAME=`grep " btrfs " /etc/fstab \
54 | grep " $VOLUME_PATH " \
58 if [ -z "$FS_NAME" ]; then
59 echo "$NAME: Failed to detect btrfs filesystem label for \"$VOLUME_PATH\"!"
63 [ -z "$DRY_RUN" ] || echo "FS_NAME=$FS_NAME"
65 # Detect btrfs subvolume name
66 VOLUME_NAME=`grep " btrfs " /etc/fstab \
68 | grep " $VOLUME_PATH " \
69 | sed -r 's|.*subvol=([[:alnum:]]*).*|\1|'`
70 if [ -z "$VOLUME_NAME" ]; then
71 echo "$NAME: Failed to detect btrfs subvolume name for \"$VOLUME_PATH\"!"
75 [ -z "$DRY_RUN" ] || echo "VOLUME_NAME=$VOLUME_NAME"
77 # Detect mount point of the whole btrfs filesystem
78 FS_MOUNT_PATH="/media/$FS_NAME"
79 if [ ! -d "$FS_MOUNT_PATH" ]; then
80 echo "$NAME: Directory \"$FS_MOUNT_PATH\" does not exist!"
83 grep " btrfs " /proc/mounts | grep " $FS_MOUNT_PATH " >/dev/null 2>&1
85 echo "$NAME: btrfs \"$FS_NAME\" seems not to be mounted on \"$FS_MOUNT_PATH\"!"
89 [ -z "$DRY_RUN" ] || echo "FS_MOUNT_PATH=$FS_MOUNT_PATH"
91 [ -z "$DRY_RUN" ] || exit 0
93 # Generate snapshot name
98 [ -e "$FS_MOUNT_PATH/$VOLUME_NAME-$id" ] || break
99 if [ -n "$ONLY_ONE_PER_DAY" ]; then
100 echo "Daily snapshot of \"$VOLUME_PATH\" (\"$VOLUME_NAME\") in btrfs \"$FS_NAME\" already exists."
106 echo "Creating snapshot of \"$VOLUME_PATH\" (\"$VOLUME_NAME\") in btrfs \"$FS_NAME\" ..."
110 btrfs subvolume snapshot "$VOLUME_NAME" "$VOLUME_NAME-$id"
111 find . -maxdepth 1 -name "$VOLUME_NAME"'*' -exec ls -1d {} \; | column