New features and improvements revised and re-added
This commit is contained in:
@@ -32,7 +32,23 @@
|
||||
. /usr/local/etc/bastille/bastille.conf
|
||||
|
||||
usage() {
|
||||
error_exit "Usage: bastille export TARGET [safe|tarball] | PATH"
|
||||
# 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
|
||||
}
|
||||
|
||||
# Handle special-case commands first
|
||||
@@ -47,81 +63,193 @@ if [ "${TARGET}" = "ALL" ]; then
|
||||
error_exit "Batch export is unsupported."
|
||||
fi
|
||||
|
||||
if [ $# -gt 2 ] || [ $# -lt 0 ]; then
|
||||
if [ $# -gt 4 ] || [ $# -lt 0 ]; then
|
||||
usage
|
||||
fi
|
||||
|
||||
OPTION="${1}"
|
||||
EXPATH="${2}"
|
||||
SAFE_EXPORT=
|
||||
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
|
||||
}
|
||||
|
||||
# Handle some options
|
||||
if [ -n "${OPTION}" ]; then
|
||||
if [ "${OPTION}" = "-t" -o "${OPTION}" = "--txz" -o ${OPTION} = "tarball" ]; then
|
||||
if [ "${bastille_zfs_enable}" = "YES" ]; then
|
||||
# Temporarily disable ZFS so we can create a standard backup archive
|
||||
bastille_zfs_enable="NO"
|
||||
fi
|
||||
elif [ "${OPTION}" = "-s" -o "${OPTION}" = "--safe" -o ${OPTION} = "safe" ]; then
|
||||
SAFE_EXPORT="1"
|
||||
elif echo "${OPTION}" | grep -q "\/"; then
|
||||
if [ -d "${OPTION}" ]; then
|
||||
EXPATH="${OPTION}"
|
||||
else
|
||||
error_exit "Error: Path not found."
|
||||
fi
|
||||
else
|
||||
error_notify "Invalid option!"
|
||||
usage
|
||||
GZIP_EXPORT=
|
||||
SAFE_EXPORT=
|
||||
RAW_EXPORT=
|
||||
DIR_EXPORT=
|
||||
TXZ_EXPORT=
|
||||
TGZ_EXPORT=
|
||||
OPT_ZSEND="-R"
|
||||
|
||||
# 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=
|
||||
fi
|
||||
fi
|
||||
|
||||
# Export directory check
|
||||
if [ -n "${EXPATH}" ]; then
|
||||
if [ -d "${EXPATH}" ]; then
|
||||
if [ -n "${DIR_EXPORT}" ]; then
|
||||
if [ -d "${DIR_EXPORT}" ]; then
|
||||
# Set the user defined export directory
|
||||
bastille_backupsdir="${EXPATH}"
|
||||
bastille_backupsdir="${DIR_EXPORT}"
|
||||
else
|
||||
error_exit "Error: Path not found."
|
||||
fi
|
||||
fi
|
||||
|
||||
create_zfs_snap(){
|
||||
# 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() {
|
||||
# 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}"
|
||||
}
|
||||
|
||||
jail_export()
|
||||
{
|
||||
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() {
|
||||
# Attempt to export the container
|
||||
DATE=$(date +%F-%H%M%S)
|
||||
if [ "${bastille_zfs_enable}" = "YES" ]; then
|
||||
if [ -n "${bastille_zfs_zpool}" ]; then
|
||||
FILE_EXT="xz"
|
||||
if [ -n "${RAW_EXPORT}" ]; then
|
||||
FILE_EXT=""
|
||||
export_check
|
||||
|
||||
if [ -n "${SAFE_EXPORT}" ]; then
|
||||
info "Safely exporting '${TARGET}' to a compressed .${FILE_EXT} archive."
|
||||
bastille stop ${TARGET}
|
||||
create_zfs_snap
|
||||
bastille start ${TARGET}
|
||||
# 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
|
||||
|
||||
# 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}"
|
||||
else
|
||||
info "Hot exporting '${TARGET}' to a compressed .${FILE_EXT} archive."
|
||||
create_zfs_snap
|
||||
fi
|
||||
FILE_EXT=".xz"
|
||||
export_check
|
||||
|
||||
info "Sending ZFS data stream..."
|
||||
# Export the container recursively and cleanup temporary snapshots
|
||||
zfs send -R "${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}"
|
||||
# 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
|
||||
fi
|
||||
else
|
||||
# Create standard backup archive
|
||||
FILE_EXT="txz"
|
||||
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}"
|
||||
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
|
||||
fi
|
||||
|
||||
if [ "$?" -ne 0 ]; then
|
||||
@@ -129,8 +257,8 @@ jail_export()
|
||||
else
|
||||
# Generate container checksum file
|
||||
cd "${bastille_backupsdir}"
|
||||
sha256 -q "${TARGET}_${DATE}.${FILE_EXT}" > "${TARGET}_${DATE}.sha256"
|
||||
info "Exported '${bastille_backupsdir}/${TARGET}_${DATE}.${FILE_EXT}' successfully."
|
||||
sha256 -q "${TARGET}_${DATE}${FILE_EXT}" > "${TARGET}_${DATE}.sha256"
|
||||
info "Exported '${bastille_backupsdir}/${TARGET}_${DATE}${FILE_EXT}' successfully."
|
||||
exit 0
|
||||
fi
|
||||
}
|
||||
@@ -148,4 +276,6 @@ if [ "${bastille_zfs_enable}" != "YES" ]; then
|
||||
fi
|
||||
fi
|
||||
|
||||
jail_export
|
||||
if [ -n "${TARGET}" ]; then
|
||||
jail_export
|
||||
fi
|
||||
|
||||
Reference in New Issue
Block a user