]> arthur.barton.de Git - ax-linux.git/commitdiff
btrfs-mksnapshot[-rootfs]: Fix shellcheck(1) warnings
authorAlexander Barton <alex@barton.de>
Sun, 26 Jan 2020 19:42:42 +0000 (20:42 +0100)
committerAlexander Barton <alex@barton.de>
Sun, 26 Jan 2020 19:42:42 +0000 (20:42 +0100)
btrfs/mksnapshot/btrfs-mksnapshot
btrfs/mksnapshot/btrfs-mksnapshot-rootfs

index f173cbfa5e3055787a10e94a4691f510913402cc..15edff9a0cd2fdd98bf3f116527b857cb3a1d33b 100755 (executable)
@@ -1,7 +1,7 @@
 #!/bin/bash
 #
 # btrfs-mksnapshot -- Make snapshots of btrfs filesystems
-# Copyright (c)2013-2014 Alexander Barton (alex@barton.de)
+# Copyright (c)2013-2020 Alexander Barton (alex@barton.de)
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -9,9 +9,9 @@
 # (at your option) any later version.
 #
 
-NAME=`basename "$0"`
+NAME=$(basename "$0")
 
-function Usage()
+Usage()
 {
        echo "Usage: $NAME [-1] [<path>]"; echo
        echo " -1      Create only one shapshot per day."
@@ -25,7 +25,7 @@ DRY_RUN=
 
 while [ $# -ge 1 ]; do
        case "$1" in
-         [^\-]*)
+         [^-]*)
                break
                ;;
          -1)
@@ -49,14 +49,14 @@ done
 [ -z "$DRY_RUN" ] || echo "VOLUME_PATH=$VOLUME_PATH"
 
 # Detect btrfs filesystem
-FS_NAME=`btrfs fi sh -m / 2>/dev/null | head -1 | cut -d"'" -f2`
+FS_NAME=$(btrfs filesystem show -m / 2>/dev/null | head -1 | cut -d"'" -f2)
 if [ -z "$FS_NAME" ]; then
-       FS_NAME=`grep " btrfs " /etc/fstab \
+       FS_NAME=$(grep " btrfs " /etc/fstab \
                | grep "^LABEL=" \
                | grep " $VOLUME_PATH " \
                | cut -d'"' -f2 \
                | cut -d'=' -f2- \
-               | cut -d' ' -f1`
+               | cut -d' ' -f1)
 fi
 if [ -z "$FS_NAME" ]; then
        echo "$NAME: Failed to detect btrfs filesystem label for \"$VOLUME_PATH\"!"
@@ -66,18 +66,18 @@ fi
 [ -z "$DRY_RUN" ] || echo "FS_NAME=$FS_NAME"
 
 # Detect btrfs subvolume name
-VOLUME_NAME=`grep " btrfs " /etc/fstab \
+VOLUME_NAME=$(grep " btrfs " /etc/fstab \
        | grep "subvol=" \
        | grep " $VOLUME_PATH " \
-       | sed -r 's|.*subvol=([^, \t]*).*|\1|'`
-if [ -z "$VOLUME_NAME" -a "$VOLUME_PATH" = "/" ]; then
-       VOLUME_NAME=`btrfs subvolume get-default / | cut -d' ' -f9`
+       | sed -r 's|.*subvol=([^, \t]*).*|\1|')
+if [ -z "$VOLUME_NAME" ] && [ "$VOLUME_PATH" = "/" ]; then
+       VOLUME_NAME=$(btrfs subvolume get-default / | cut -d' ' -f9)
 fi
 if [ -z "$VOLUME_NAME" ]; then
        echo "$NAME: Failed to detect btrfs subvolume name for \"$VOLUME_PATH\"!"
        exit 1
 fi
-BASE_VOLUME_NAME=`echo "$VOLUME_NAME" | cut -d'@' -f1`
+BASE_VOLUME_NAME=$(echo "$VOLUME_NAME" | cut -d'@' -f1)
 
 if [ -n "$DRY_RUN" ]; then
        echo "VOLUME_NAME=$VOLUME_NAME"
@@ -90,8 +90,7 @@ if [ ! -d "$FS_MOUNT_PATH" ]; then
        echo "$NAME: Directory \"$FS_MOUNT_PATH\" does not exist!"
        exit 1
 fi
-grep " btrfs " /proc/mounts | grep " $FS_MOUNT_PATH " >/dev/null 2>&1
-if [ $? -ne 0 ]; then
+if grep " btrfs " /proc/mounts | grep " $FS_MOUNT_PATH " >/dev/null 2>&1; then
        echo "$NAME: btrfs \"$FS_NAME\" seems not to be mounted on \"$FS_MOUNT_PATH\"!"
        exit 1
 fi
@@ -100,7 +99,7 @@ fi
 
 # Generate snapshot name
 i=1
-date=`date +%Y%m%d`
+date=$(date +%Y%m%d)
 while true; do
        id="$date-$i"
        [ -e "$FS_MOUNT_PATH/$BASE_VOLUME_NAME@$id" ] || break
@@ -108,7 +107,7 @@ while true; do
                echo "Snapshot \"$BASE_VOLUME_NAME@$id\" of \"$VOLUME_PATH\" (\"$VOLUME_NAME\") in btrfs \"$FS_NAME\" already exists."
                exit 0
        fi
-       i=`expr $i + 1`
+       i=$((i + 1))
 done
 NEW_VOLUME_NAME="$BASE_VOLUME_NAME@$id"
 
@@ -119,7 +118,7 @@ NEW_VOLUME_NAME="$BASE_VOLUME_NAME@$id"
 echo "Creating snapshot of \"$VOLUME_PATH\" (\"$VOLUME_NAME\") in btrfs \"$FS_NAME\" ..."
 set -x
 
-cd "$FS_MOUNT_PATH"
+cd "$FS_MOUNT_PATH" || exit 1
 btrfs subvolume snapshot -r "$VOLUME_NAME" "$NEW_VOLUME_NAME"
 find . -maxdepth 1 \( -name "$BASE_VOLUME_NAME" -o \
  -name "$BASE_VOLUME_NAME"@'*' \) -exec ls -1d {} \; | sort | column
index ebc2f3f42910a0a979e854aeab25de402ed91476..2744d03727e2bffe3e757a796f05348dd36d2265 100755 (executable)
@@ -1,7 +1,7 @@
-#!/bin/bash -e
+#!/bin/sh -e
 #
 # btrfs-mksnapshot-rootfs -- Make snapshots of root filesystems ("/")
-# Copyright (c)2014 Alexander Barton (alex@barton.de)
+# Copyright (c)2014-2020 Alexander Barton (alex@barton.de)
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -9,9 +9,9 @@
 # (at your option) any later version.
 #
 
-NAME=`basename "$0"`
+NAME=$(basename "$0")
 
-function Usage()
+Usage()
 {
        echo "Usage: $NAME"; echo
        exit 1
@@ -19,7 +19,7 @@ function Usage()
 
 [ $# -eq 0 ] || Usage
 
-if [ -n "`fgrep ' / btrfs ' /etc/fstab 2>/dev/null`" ]; then
+if grep -Fq ' / btrfs ' /etc/fstab; then
        if [ -x /usr/local/sbin/btrfs-mksnapshot ]; then
                /usr/local/sbin/btrfs-mksnapshot -1 / || exit 1
        else