2020-01-26 19:45:26 -04:00
|
|
|
#!/bin/sh
|
2020-04-14 11:52:29 +02:00
|
|
|
#
|
2021-01-15 20:00:26 -07:00
|
|
|
# Copyright (c) 2018-2021, Christer Edwards <christer.edwards@gmail.com>
|
2020-01-26 19:45:26 -04:00
|
|
|
# All rights reserved.
|
2020-04-14 11:52:29 +02:00
|
|
|
#
|
2020-01-26 19:45:26 -04:00
|
|
|
# Redistribution and use in source and binary forms, with or without
|
|
|
|
|
# modification, are permitted provided that the following conditions are met:
|
2020-04-14 11:52:29 +02:00
|
|
|
#
|
2020-01-26 19:45:26 -04:00
|
|
|
# * Redistributions of source code must retain the above copyright notice, this
|
|
|
|
|
# list of conditions and the following disclaimer.
|
2020-04-14 11:52:29 +02:00
|
|
|
#
|
2020-01-26 19:45:26 -04:00
|
|
|
# * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
|
# this list of conditions and the following disclaimer in the documentation
|
|
|
|
|
# and/or other materials provided with the distribution.
|
2020-04-14 11:52:29 +02:00
|
|
|
#
|
2020-01-26 19:45:26 -04:00
|
|
|
# * Neither the name of the copyright holder nor the names of its
|
|
|
|
|
# contributors may be used to endorse or promote products derived from
|
|
|
|
|
# this software without specific prior written permission.
|
2020-04-14 11:52:29 +02:00
|
|
|
#
|
2020-01-26 19:45:26 -04:00
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
|
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
|
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
|
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
|
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
|
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
|
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
|
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
|
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
2020-08-30 10:57:14 -04:00
|
|
|
. /usr/local/share/bastille/common.sh
|
2020-01-26 19:45:26 -04:00
|
|
|
. /usr/local/etc/bastille/bastille.conf
|
|
|
|
|
|
|
|
|
|
usage() {
|
2021-07-07 05:05:38 -04:00
|
|
|
# Build an independent usage for the export command
|
|
|
|
|
# Valid compress/options for ZFS systems are raw, .gz, .tgz, .txz and .xz(default)
|
|
|
|
|
# Valid compress/options for non ZFS configured systems are .tgz and .txz(default)
|
|
|
|
|
echo -e "${COLOR_RED}Usage: bastille export TARGET | option(s) | PATH${COLOR_RESET}"
|
|
|
|
|
|
|
|
|
|
cat << EOF
|
|
|
|
|
Options:
|
|
|
|
|
|
|
|
|
|
gz | --gz -- Export a ZFS jail using GZIP(.gz) compressed image.
|
|
|
|
|
-r | raw | --raw -- Export a ZFS jail to an uncompressed RAW image.
|
|
|
|
|
-s | safe | --safe -- Safely stop and start a ZFS jail before the exporting process.
|
|
|
|
|
tgz | --tgz -- Export a jail using simple .tgz compressed archive instead.
|
|
|
|
|
txz | --txz -- Export a jail using simple .txz compressed archive instead.
|
|
|
|
|
-v | verbose | --verbose -- Be more verbose during the ZFS send operation.
|
|
|
|
|
|
|
|
|
|
EOF
|
|
|
|
|
exit 1
|
2020-01-26 19:45:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Handle special-case commands first
|
|
|
|
|
case "$1" in
|
|
|
|
|
help|-h|--help)
|
|
|
|
|
usage
|
|
|
|
|
;;
|
|
|
|
|
esac
|
|
|
|
|
|
2020-10-16 14:48:36 -04:00
|
|
|
# Check for unsupported actions
|
|
|
|
|
if [ "${TARGET}" = "ALL" ]; then
|
|
|
|
|
error_exit "Batch export is unsupported."
|
|
|
|
|
fi
|
|
|
|
|
|
2021-07-07 05:05:38 -04:00
|
|
|
if [ $# -gt 4 ] || [ $# -lt 0 ]; then
|
2020-01-26 19:45:26 -04:00
|
|
|
usage
|
|
|
|
|
fi
|
|
|
|
|
|
2021-07-07 05:05:38 -04:00
|
|
|
zfs_enable_check() {
|
|
|
|
|
# Temporarily disable ZFS so we can create a standard backup archive
|
|
|
|
|
if [ "${bastille_zfs_enable}" = "YES" ]; then
|
|
|
|
|
bastille_zfs_enable="NO"
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GZIP_EXPORT=
|
2021-01-08 20:30:03 -04:00
|
|
|
SAFE_EXPORT=
|
2021-07-07 05:05:38 -04:00
|
|
|
RAW_EXPORT=
|
|
|
|
|
DIR_EXPORT=
|
|
|
|
|
TXZ_EXPORT=
|
|
|
|
|
TGZ_EXPORT=
|
|
|
|
|
OPT_ZSEND="-R"
|
2021-05-15 08:13:14 -04:00
|
|
|
|
2021-07-07 05:05:38 -04:00
|
|
|
# Handle and parse option args
|
|
|
|
|
while [ $# -gt 0 ]; do
|
|
|
|
|
case "${1}" in
|
|
|
|
|
gz|--gz)
|
|
|
|
|
GZIP_EXPORT="1"
|
|
|
|
|
shift
|
|
|
|
|
;;
|
|
|
|
|
tgz|--tgz)
|
|
|
|
|
TGZ_EXPORT="1"
|
|
|
|
|
zfs_enable_check
|
|
|
|
|
shift
|
|
|
|
|
;;
|
|
|
|
|
txz|--txz)
|
|
|
|
|
TXZ_EXPORT="1"
|
|
|
|
|
zfs_enable_check
|
|
|
|
|
shift
|
|
|
|
|
;;
|
|
|
|
|
-s|safe|--safe)
|
|
|
|
|
SAFE_EXPORT="1"
|
|
|
|
|
shift
|
|
|
|
|
;;
|
|
|
|
|
-r|raw|--raw)
|
|
|
|
|
RAW_EXPORT="1"
|
|
|
|
|
shift
|
|
|
|
|
;;
|
|
|
|
|
-v|verbose|--verbose)
|
|
|
|
|
OPT_ZSEND="-Rv"
|
|
|
|
|
shift
|
|
|
|
|
;;
|
|
|
|
|
*)
|
|
|
|
|
if echo "${1}" | grep -q "\/"; then
|
|
|
|
|
DIR_EXPORT="${1}"
|
|
|
|
|
else
|
|
|
|
|
usage
|
|
|
|
|
fi
|
|
|
|
|
shift
|
|
|
|
|
;;
|
|
|
|
|
esac
|
|
|
|
|
done
|
|
|
|
|
|
|
|
|
|
# Validate for combined options
|
|
|
|
|
if [ -n "${TXZ_EXPORT}" -o -n "${TGZ_EXPORT}" ] && [ -n "${SAFE_EXPORT}" ]; then
|
|
|
|
|
error_exit "Error: Simple archive modes with safe ZFS export can't be used together."
|
|
|
|
|
fi
|
|
|
|
|
if [ -z "${bastille_zfs_enable}" ]; then
|
|
|
|
|
if [ -n "${GZIP_EXPORT}" -o -n "${RAW_EXPORT}" -o "${SAFE_EXPORT}" ]; then
|
|
|
|
|
error_exit "Options --gz, --raw, --safe are valid for ZFS configured systems only."
|
|
|
|
|
fi
|
|
|
|
|
fi
|
|
|
|
|
if [ -n "${SAFE_EXPORT}" ]; then
|
|
|
|
|
# Check if container is running, otherwise just ignore
|
|
|
|
|
if [ -z "$(jls name | awk "/^${TARGET}$/")" ]; then
|
|
|
|
|
SAFE_EXPORT=
|
2021-02-18 06:48:43 -04:00
|
|
|
fi
|
|
|
|
|
fi
|
|
|
|
|
|
2020-10-16 14:48:36 -04:00
|
|
|
# Export directory check
|
2021-07-07 05:05:38 -04:00
|
|
|
if [ -n "${DIR_EXPORT}" ]; then
|
|
|
|
|
if [ -d "${DIR_EXPORT}" ]; then
|
2020-10-16 14:48:36 -04:00
|
|
|
# Set the user defined export directory
|
2021-07-07 05:05:38 -04:00
|
|
|
bastille_backupsdir="${DIR_EXPORT}"
|
2020-10-16 14:48:36 -04:00
|
|
|
else
|
|
|
|
|
error_exit "Error: Path not found."
|
|
|
|
|
fi
|
|
|
|
|
fi
|
|
|
|
|
|
2021-07-07 05:05:38 -04:00
|
|
|
# Fallback to default if missing config parameters
|
|
|
|
|
if [ -z "${bastille_compress_xz_options}" ]; then
|
|
|
|
|
bastille_compress_xz_options="-0 -v"
|
|
|
|
|
fi
|
|
|
|
|
if [ -z "${bastille_compress_gz_options}" ]; then
|
|
|
|
|
bastille_compress_gz_options="-1 -v"
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
create_zfs_snap() {
|
2021-01-08 20:30:03 -04:00
|
|
|
# Take a recursive temporary snapshot
|
|
|
|
|
info "Creating temporary ZFS snapshot for export..."
|
|
|
|
|
zfs snapshot -r "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${TARGET}@bastille_export_${DATE}"
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-07 05:05:38 -04:00
|
|
|
export_check() {
|
|
|
|
|
# Inform the user about the exporting method
|
|
|
|
|
if [ -n "$(jls name | awk "/^${TARGET}$/")" ]; then
|
|
|
|
|
if [ -n "${SAFE_EXPORT}" ]; then
|
|
|
|
|
EXPORT_AS="Safely exporting"
|
|
|
|
|
else
|
|
|
|
|
EXPORT_AS="Hot exporting"
|
|
|
|
|
fi
|
|
|
|
|
else
|
|
|
|
|
EXPORT_AS="Exporting"
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if [ "${FILE_EXT}" = ".xz" -o "${FILE_EXT}" = ".gz" -o "${FILE_EXT}" = "" ]; then
|
|
|
|
|
EXPORT_TYPE="image"
|
|
|
|
|
else
|
|
|
|
|
EXPORT_TYPE="archive"
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if [ -n "${RAW_EXPORT}" ]; then
|
|
|
|
|
EXPORT_INFO="to a raw ${EXPORT_TYPE}"
|
|
|
|
|
else
|
|
|
|
|
EXPORT_INFO="to a compressed ${FILE_EXT} ${EXPORT_TYPE}"
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
info "${EXPORT_AS} '${TARGET}' ${EXPORT_INFO}..."
|
|
|
|
|
|
|
|
|
|
# Safely stop and snapshot the jail
|
|
|
|
|
if [ -n "${SAFE_EXPORT}" ]; then
|
|
|
|
|
bastille stop ${TARGET}
|
|
|
|
|
create_zfs_snap
|
|
|
|
|
bastille start ${TARGET}
|
|
|
|
|
else
|
|
|
|
|
create_zfs_snap
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
if [ "${bastille_zfs_enable}" = "YES" ]; then
|
|
|
|
|
info "Sending ZFS data stream..."
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
jail_export() {
|
2020-01-26 19:45:26 -04:00
|
|
|
# Attempt to export the container
|
2020-02-21 13:58:45 +01:00
|
|
|
DATE=$(date +%F-%H%M%S)
|
2020-08-30 15:37:54 -04:00
|
|
|
if [ "${bastille_zfs_enable}" = "YES" ]; then
|
|
|
|
|
if [ -n "${bastille_zfs_zpool}" ]; then
|
2021-07-07 05:05:38 -04:00
|
|
|
if [ -n "${RAW_EXPORT}" ]; then
|
|
|
|
|
FILE_EXT=""
|
|
|
|
|
export_check
|
|
|
|
|
|
|
|
|
|
# Export the raw container recursively and cleanup temporary snapshots
|
|
|
|
|
zfs send ${OPT_ZSEND} "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${TARGET}@bastille_export_${DATE}" \
|
|
|
|
|
> "${bastille_backupsdir}/${TARGET}_${DATE}"
|
|
|
|
|
zfs destroy "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${TARGET}/root@bastille_export_${DATE}"
|
|
|
|
|
zfs destroy "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${TARGET}@bastille_export_${DATE}"
|
|
|
|
|
elif [ -n "${GZIP_EXPORT}" ]; then
|
|
|
|
|
FILE_EXT=".gz"
|
|
|
|
|
export_check
|
2021-05-15 08:13:14 -04:00
|
|
|
|
2021-07-07 05:05:38 -04:00
|
|
|
# Export the raw container recursively and cleanup temporary snapshots
|
|
|
|
|
zfs send ${OPT_ZSEND} "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${TARGET}@bastille_export_${DATE}" | \
|
|
|
|
|
gzip ${bastille_compress_gz_options} > "${bastille_backupsdir}/${TARGET}_${DATE}${FILE_EXT}"
|
|
|
|
|
zfs destroy "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${TARGET}/root@bastille_export_${DATE}"
|
|
|
|
|
zfs destroy "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${TARGET}@bastille_export_${DATE}"
|
2021-01-08 20:30:03 -04:00
|
|
|
else
|
2021-07-07 05:05:38 -04:00
|
|
|
FILE_EXT=".xz"
|
|
|
|
|
export_check
|
2021-05-15 08:13:14 -04:00
|
|
|
|
2021-07-07 05:05:38 -04:00
|
|
|
# Export the container recursively and cleanup temporary snapshots(default)
|
|
|
|
|
zfs send ${OPT_ZSEND} "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${TARGET}@bastille_export_${DATE}" | \
|
|
|
|
|
xz ${bastille_compress_xz_options} > "${bastille_backupsdir}/${TARGET}_${DATE}${FILE_EXT}"
|
|
|
|
|
zfs destroy "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${TARGET}/root@bastille_export_${DATE}"
|
|
|
|
|
zfs destroy "${bastille_zfs_zpool}/${bastille_zfs_prefix}/jails/${TARGET}@bastille_export_${DATE}"
|
|
|
|
|
fi
|
2020-01-26 19:45:26 -04:00
|
|
|
fi
|
2020-08-30 15:37:54 -04:00
|
|
|
else
|
2021-07-07 05:05:38 -04:00
|
|
|
if [ -n "${TGZ_EXPORT}" ]; then
|
|
|
|
|
FILE_EXT=".tgz"
|
|
|
|
|
|
|
|
|
|
# Create standard tgz backup archive
|
|
|
|
|
info "Exporting '${TARGET}' to a compressed ${FILE_EXT} archive..."
|
|
|
|
|
cd "${bastille_jailsdir}" && tar -cf - "${TARGET}" | gzip ${bastille_compress_gz_options} > "${bastille_backupsdir}/${TARGET}_${DATE}${FILE_EXT}"
|
|
|
|
|
else
|
|
|
|
|
FILE_EXT=".txz"
|
|
|
|
|
|
|
|
|
|
# Create standard txz backup archive(default)
|
|
|
|
|
info "Exporting '${TARGET}' to a compressed ${FILE_EXT} archive..."
|
|
|
|
|
cd "${bastille_jailsdir}" && tar -cf - "${TARGET}" | xz ${bastille_compress_xz_options} > "${bastille_backupsdir}/${TARGET}_${DATE}${FILE_EXT}"
|
|
|
|
|
fi
|
2020-08-30 15:37:54 -04:00
|
|
|
fi
|
2020-01-26 19:45:26 -04:00
|
|
|
|
2020-08-30 15:37:54 -04:00
|
|
|
if [ "$?" -ne 0 ]; then
|
|
|
|
|
error_exit "Failed to export '${TARGET}' container."
|
2020-01-26 19:45:26 -04:00
|
|
|
else
|
2020-08-30 15:37:54 -04:00
|
|
|
# Generate container checksum file
|
|
|
|
|
cd "${bastille_backupsdir}"
|
2021-07-07 05:05:38 -04:00
|
|
|
sha256 -q "${TARGET}_${DATE}${FILE_EXT}" > "${TARGET}_${DATE}.sha256"
|
|
|
|
|
info "Exported '${bastille_backupsdir}/${TARGET}_${DATE}${FILE_EXT}' successfully."
|
2020-08-30 15:37:54 -04:00
|
|
|
exit 0
|
2020-01-26 19:45:26 -04:00
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-04 10:23:43 -04:00
|
|
|
# Check if backups directory/dataset exist
|
|
|
|
|
if [ ! -d "${bastille_backupsdir}" ]; then
|
2020-08-30 10:57:14 -04:00
|
|
|
error_exit "Backups directory/dataset does not exist. See 'bastille bootstrap'."
|
2020-01-26 19:45:26 -04:00
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# Check if is a ZFS system
|
|
|
|
|
if [ "${bastille_zfs_enable}" != "YES" ]; then
|
|
|
|
|
# Check if container is running and ask for stop in UFS systems
|
|
|
|
|
if [ -n "$(jls name | awk "/^${TARGET}$/")" ]; then
|
2020-08-30 10:57:14 -04:00
|
|
|
error_exit "${TARGET} is running. See 'bastille stop'."
|
2020-01-26 19:45:26 -04:00
|
|
|
fi
|
|
|
|
|
fi
|
|
|
|
|
|
2021-07-07 05:05:38 -04:00
|
|
|
if [ -n "${TARGET}" ]; then
|
|
|
|
|
jail_export
|
|
|
|
|
fi
|