evobackup/lib/bkctld-remove

67 lines
1.8 KiB
Bash
Executable File

#!/bin/sh
#
# Description: Remove jail and all dated copies (incs)
# Usage: remove <jailname>|all
# Return codes:
# * 101 : jail removal aborted
#
# shellcheck source=./includes
LIBDIR="$(dirname $0)" && . "${LIBDIR}/includes"
jail_name="${1:?}"
if [ -z "${jail_name}" ]; then
show_help && exit 1
fi
jail_path=$(jail_path "${jail_name}")
incs_path=$(incs_path "${jail_name}")
test -d "${jail_path}" || error "${jail_name}: jail not found" 2
if [ "${FORCE}" != "1" ]; then
answer=""
while :; do
printf "> Are you sure you want to delete jail \`%s'? [Y,n,?] " "${jail_name}"
read -r answer
case $answer in
[Yy]|"" )
break
;;
[Nn] )
tty -s && echo "Abort." >&2
exit 101
;;
* )
printf "y - yes, execute actions and exit\n"
printf "n - no, don't execute actions and exit\n"
printf "? - print this help\n"
;;
esac
done
fi
"${LIBDIR}/bkctld-is-on" "${jail_name}" && "${LIBDIR}/bkctld-stop" "${jail_name}"
rm -f "${CONFDIR}/${jail_name}"
jail_inode=$(stat --format=%i "${jail_path}")
if [ "${jail_inode}" -eq 256 ]; then
/bin/btrfs subvolume delete "${jail_path}" | debug
else
rm -rf "${jail_path:?}" | debug
fi
# TODO: use functions here
if [ -d "${incs_path}" ]; then
incs=$(ls "${incs_path}")
for inc in ${incs}; do
inc_inode=$(stat --format=%i "${incs_path}/${inc}")
if [ "${inc_inode}" -eq 256 ]; then
/bin/btrfs subvolume delete "${incs_path}/${inc}" | debug
else
warning "You need to purge \`${incs_path}/${inc}' manually"
fi
done
rmdir --ignore-fail-on-non-empty "${incs_path}" | debug
fi
"${LIBDIR}/bkctld-firewall" "${jail_name}"
notice "Delete jail \`${jail_name}' : OK"