Snapshot removal implemented
Snapshot removing implemented (-r, --remove) Updated helptext Variable expansion fixes
This commit is contained in:
parent
55cbb0df32
commit
9080ccce05
58
snapsh
58
snapsh
|
@ -25,6 +25,7 @@ BTRFS_EXECUTABLE=$(which btrfs)
|
||||||
TIMESTAMP=$(date +%Y.%m.%d-%H:%M:%S)
|
TIMESTAMP=$(date +%Y.%m.%d-%H:%M:%S)
|
||||||
SUBVOLUME="root"
|
SUBVOLUME="root"
|
||||||
DESCRIPTION=""
|
DESCRIPTION=""
|
||||||
|
REMOVE_TARGET=""
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -39,10 +40,8 @@ Options:
|
||||||
snapsh -d \"A snapshot\" -s root
|
snapsh -d \"A snapshot\" -s root
|
||||||
-s SUBVOL, --snapshot SUBVOL Take a snapshot of subvolume named SUBVOL.
|
-s SUBVOL, --snapshot SUBVOL Take a snapshot of subvolume named SUBVOL.
|
||||||
-l, --list List snapshots
|
-l, --list List snapshots
|
||||||
|
-r NUMBER, --remove NUMBER Remove snapshot NUMBER. See snapshot numbers with
|
||||||
Exit codes:
|
snapsh -l\n"
|
||||||
2 - Invalid options
|
|
||||||
3 - Target subvolume does not exist\n"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -61,7 +60,7 @@ snapshot() {
|
||||||
${BTRFS_EXECUTABLE} subvolume create ${SNAPSHOTS_LOCATION}
|
${BTRFS_EXECUTABLE} subvolume create ${SNAPSHOTS_LOCATION}
|
||||||
unset ${REPLY}
|
unset ${REPLY}
|
||||||
else
|
else
|
||||||
EXIT_CODE=3
|
EXIT_CODE=1
|
||||||
fi
|
fi
|
||||||
else
|
else
|
||||||
printf "Creating snapshot of subvolume ${SUBVOLUME} as ${SUBVOLUME}_snapshot_${TIMESTAMP}\n"
|
printf "Creating snapshot of subvolume ${SUBVOLUME} as ${SUBVOLUME}_snapshot_${TIMESTAMP}\n"
|
||||||
|
@ -95,7 +94,7 @@ snapshot() {
|
||||||
|
|
||||||
list() {
|
list() {
|
||||||
root_check
|
root_check
|
||||||
NUM=0
|
NUM=1
|
||||||
printf "%6s %s %s %26s %s %s %6s %s %s\n" "Number" "|" "Time:" "|" "Source" "|" "Type" "|" "Description"
|
printf "%6s %s %s %26s %s %s %6s %s %s\n" "Number" "|" "Time:" "|" "Source" "|" "Type" "|" "Description"
|
||||||
for snapshot in ${SNAPSHOTS_LOCATION}/*/; do
|
for snapshot in ${SNAPSHOTS_LOCATION}/*/; do
|
||||||
. ${snapshot}/.snapsh
|
. ${snapshot}/.snapsh
|
||||||
|
@ -107,11 +106,43 @@ list() {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
remove() {
|
||||||
|
root_check
|
||||||
|
SNAPSHOTS=(${SNAPSHOTS_LOCATION}/*/)
|
||||||
|
let INDEX=${REMOVE_TARGET}-1
|
||||||
|
TARGET=${SNAPSHOTS[${INDEX}]}
|
||||||
|
. ${TARGET}/.snapsh
|
||||||
|
|
||||||
|
if [[ "${INDEX}" -gt "${#SNAPSHOTS[@]}" ]]; then
|
||||||
|
printf "Snapshot number ${REMOVE_TARGET} does not exist.\n"
|
||||||
|
list
|
||||||
|
exit 1
|
||||||
|
elif [[ "${REMOVE_TARGET}" -lt 1 ]]; then
|
||||||
|
printf "Number must be a positive integer.\n"
|
||||||
|
list
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
printf "Delete snapshot ${REMOVE_TARGET}: ${DATE}, subvolume ${SOURCE_SUBVOLUME}, ${DESCRIPTION} (y/n)? "
|
||||||
|
read -n 1
|
||||||
|
if [[ "${REPLY}" == "y" ]]; then
|
||||||
|
printf "\n"
|
||||||
|
${BTRFS_EXECUTABLE} property set ${TARGET} ro false
|
||||||
|
${BTRFS_EXECUTABLE} subvolume delete ${TARGET}
|
||||||
|
exit 0
|
||||||
|
else
|
||||||
|
printf "\nAborted by user.\n"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Check for root permissions
|
# Check for root permissions
|
||||||
root_check() {
|
root_check() {
|
||||||
if [[ "$UID" -ne 0 ]]; then
|
if [[ "$UID" -ne 0 ]]; then
|
||||||
printf "This option needs root permission.\n"
|
printf "This option needs root permission.\n"
|
||||||
exit 3
|
exit 1
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -120,18 +151,18 @@ root_check() {
|
||||||
# If no options are given, display help
|
# If no options are given, display help
|
||||||
if [[ "$#" -eq 0 ]]; then
|
if [[ "$#" -eq 0 ]]; then
|
||||||
help
|
help
|
||||||
exit 2
|
exit 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|
||||||
# Options parsing:
|
# Options parsing:
|
||||||
OPTIONS=$(getopt -a -n snapsh -o hs:d:l --long help,snapshot:,description:,list -- "$@")
|
OPTIONS=$(getopt -a -n snapsh -o hs:d:lr: --long help,snapshot:,description:,list,remove: -- "$@")
|
||||||
|
|
||||||
# Invalid options (getopt returns nonzero)
|
# Invalid options (getopt returns nonzero)
|
||||||
if [[ "$?" -ne 0 ]]; then
|
if [[ "$?" -ne 0 ]]; then
|
||||||
printf "Error Parsing options\n"
|
printf "Error Parsing options\n"
|
||||||
help
|
help
|
||||||
exit 2
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
eval set -- "$OPTIONS"
|
eval set -- "$OPTIONS"
|
||||||
|
@ -160,6 +191,13 @@ while true; do
|
||||||
shift
|
shift
|
||||||
;;
|
;;
|
||||||
|
|
||||||
|
-r | --remove)
|
||||||
|
REMOVE_TARGET="$2"
|
||||||
|
remove
|
||||||
|
shift 2
|
||||||
|
;;
|
||||||
|
|
||||||
|
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue