Override case options by the user defined option(s)

This commit is contained in:
JRGTH
2022-01-04 17:03:07 -04:00
parent 788e4c283b
commit 26e8f382e4

View File

@@ -49,7 +49,7 @@ usage() {
-v | --verbose -- Be more verbose during the ZFS send operation. -v | --verbose -- Be more verbose during the ZFS send operation.
--xz -- Export a ZFS jail using XZ(.xz) compressed image. --xz -- Export a ZFS jail using XZ(.xz) compressed image.
Tip: If no option specified, container should be exported to standard output. Note: If no export option specified, the container should be redirected to standard output.
EOF EOF
exit 1 exit 1
@@ -80,6 +80,7 @@ zfs_enable_check() {
TARGET="${1}" TARGET="${1}"
GZIP_EXPORT= GZIP_EXPORT=
XZ_EXPORT=
SAFE_EXPORT= SAFE_EXPORT=
USER_EXPORT= USER_EXPORT=
RAW_EXPORT= RAW_EXPORT=
@@ -93,67 +94,113 @@ opt_count() {
COMP_OPTION=$(expr ${COMP_OPTION} + 1) COMP_OPTION=$(expr ${COMP_OPTION} + 1)
} }
# Handle and parse option args if [ -n "${bastille_export_options}" ]; then
while [ $# -gt 0 ]; do # Overrides the case options by the user defined option(s) automatically.
case "${1}" in # Add bastille_export_options="--optionA --optionB" to bastille.conf, or simply `export bastille_export_options="--optionA --optionB"` environment variable.
--gz) # To restore the standard case options, empty bastille_export_options="" in bastille.conf, or `unset bastille_export_options` environment variable.
GZIP_EXPORT="1" # Reference "/bastille/issues/443"
TARGET="${2}"
opt_count DEFAULT_EXPORT_OPTS="${bastille_export_options}"
shift info "Default export option(s): '${DEFAULT_EXPORT_OPTS}'"
;;
--xz) for opt in ${DEFAULT_EXPORT_OPTS}; do
XZ_EXPORT="1" case "${opt}" in
TARGET="${2}" --gz)
opt_count GZIP_EXPORT="1"
shift opt_count
;; shift;;
--tgz) --xz)
TGZ_EXPORT="1" XZ_EXPORT="1"
TARGET="${2}" opt_count
opt_count shift;;
zfs_enable_check --tgz)
shift TGZ_EXPORT="1"
;; opt_count
--txz) zfs_enable_check
TXZ_EXPORT="1" shift;;
TARGET="${2}" --txz)
opt_count TXZ_EXPORT="1"
zfs_enable_check opt_count
shift zfs_enable_check
;; shift;;
-s|--safe) --safe)
SAFE_EXPORT="1" SAFE_EXPORT="1"
TARGET="${2}" opt_count
shift shift;;
;; --raw)
-r|--raw) RAW_EXPORT="1"
RAW_EXPORT="1" opt_count
TARGET="${2}" shift ;;
opt_count --verbose)
shift OPT_ZSEND="-Rv"
;; shift;;
-v|--verbose) -*|--*) error_notify "Unknown Option."
OPT_ZSEND="-Rv" usage;;
TARGET="${2}" esac
shift done
;; else
-*|--*) # Handle and parse option args
error_notify "Unknown Option." while [ $# -gt 0 ]; do
usage case "${1}" in
;; --gz)
*) GZIP_EXPORT="1"
if echo "${1}" | grep -q "\/"; then TARGET="${2}"
DIR_EXPORT="${1}" opt_count
else shift
if [ $# -gt 2 ] || [ $# -lt 1 ]; then ;;
usage --xz)
XZ_EXPORT="1"
TARGET="${2}"
opt_count
shift
;;
--tgz)
TGZ_EXPORT="1"
TARGET="${2}"
opt_count
zfs_enable_check
shift
;;
--txz)
TXZ_EXPORT="1"
TARGET="${2}"
opt_count
zfs_enable_check
shift
;;
-s|--safe)
SAFE_EXPORT="1"
TARGET="${2}"
shift
;;
-r|--raw)
RAW_EXPORT="1"
TARGET="${2}"
opt_count
shift
;;
-v|--verbose)
OPT_ZSEND="-Rv"
TARGET="${2}"
shift
;;
-*|--*)
error_notify "Unknown Option."
usage
;;
*)
if echo "${1}" | grep -q "\/"; then
DIR_EXPORT="${1}"
else
if [ $# -gt 2 ] || [ $# -lt 1 ]; then
usage
fi
fi fi
fi shift
shift ;;
;; esac
esac done
done fi
# Validate for combined options # Validate for combined options
if [ "${COMP_OPTION}" -gt "1" ]; then if [ "${COMP_OPTION}" -gt "1" ]; then
@@ -200,13 +247,13 @@ create_zfs_snap() {
if [ -z "${USER_EXPORT}" ]; then if [ -z "${USER_EXPORT}" ]; then
info "Creating temporary ZFS snapshot for export..." info "Creating temporary ZFS snapshot for export..."
fi fi
zfs snapshot -r "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${TARGET}@bastille_export_${DATE}" zfs snapshot -r "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${TARGET}@bastille_${TARGET}_${DATE}"
} }
clean_zfs_snap() { clean_zfs_snap() {
# Cleanup the recursive temporary snapshot # Cleanup the recursive temporary snapshot
zfs destroy "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${TARGET}/root@bastille_export_${DATE}" zfs destroy "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${TARGET}/root@bastille_${TARGET}_${DATE}"
zfs destroy "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${TARGET}@bastille_export_${DATE}" zfs destroy "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${TARGET}@bastille_${TARGET}_${DATE}"
} }
export_check() { export_check() {
@@ -263,7 +310,7 @@ jail_export() {
export_check export_check
# Export the raw container recursively and cleanup temporary snapshots # Export the raw container recursively and cleanup temporary snapshots
zfs send ${OPT_ZSEND} "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${TARGET}@bastille_export_${DATE}" \ zfs send ${OPT_ZSEND} "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${TARGET}@bastille_${TARGET}_${DATE}" \
> "${bastille_backupsdir}/${TARGET}_${DATE}" > "${bastille_backupsdir}/${TARGET}_${DATE}"
clean_zfs_snap clean_zfs_snap
elif [ -n "${GZIP_EXPORT}" ]; then elif [ -n "${GZIP_EXPORT}" ]; then
@@ -271,7 +318,7 @@ jail_export() {
export_check export_check
# Export the raw container recursively and cleanup temporary snapshots # Export the raw container recursively and cleanup temporary snapshots
zfs send ${OPT_ZSEND} "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${TARGET}@bastille_export_${DATE}" | \ zfs send ${OPT_ZSEND} "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${TARGET}@bastille_${TARGET}_${DATE}" | \
gzip ${bastille_compress_gz_options} > "${bastille_backupsdir}/${TARGET}_${DATE}${FILE_EXT}" gzip ${bastille_compress_gz_options} > "${bastille_backupsdir}/${TARGET}_${DATE}${FILE_EXT}"
clean_zfs_snap clean_zfs_snap
elif [ -n "${XZ_EXPORT}" ]; then elif [ -n "${XZ_EXPORT}" ]; then
@@ -279,7 +326,7 @@ jail_export() {
export_check export_check
# Export the container recursively and cleanup temporary snapshots # Export the container recursively and cleanup temporary snapshots
zfs send ${OPT_ZSEND} "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${TARGET}@bastille_export_${DATE}" | \ zfs send ${OPT_ZSEND} "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${TARGET}@bastille_${TARGET}_${DATE}" | \
xz ${bastille_compress_xz_options} > "${bastille_backupsdir}/${TARGET}_${DATE}${FILE_EXT}" xz ${bastille_compress_xz_options} > "${bastille_backupsdir}/${TARGET}_${DATE}${FILE_EXT}"
clean_zfs_snap clean_zfs_snap
else else
@@ -288,8 +335,10 @@ jail_export() {
export_check export_check
# Quietly export the container recursively, user must redirect standard output # Quietly export the container recursively, user must redirect standard output
zfs send ${OPT_ZSEND} "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${TARGET}@bastille_export_${DATE}" if ! zfs send ${OPT_ZSEND} "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${TARGET}@bastille_${TARGET}_${DATE}"; then
clean_zfs_snap clean_zfs_snap
error_notify "\nError: An export option is required, see 'bastille export, otherwise the user must redirect to standard output."
fi
fi fi
fi fi
else else