1
0
mirror of https://github.com/Spearfoot/FreeNAS-scripts synced 2026-01-10 11:43:13 +00:00

Update improved version

This version has improved support for TrueNAS
This commit is contained in:
Keith Nash
2021-08-06 12:07:17 -05:00
committed by GitHub
parent 9601c07cfe
commit 2fd314cdb8

View File

@@ -1,109 +1,161 @@
#!/bin/sh #!/bin/sh
rundate=$(date) #####
# Backup the TrueNAS/FreeNAS configuration database and password secret encryption files
################################################# #####
# Backup the FreeNAS configuration file
################################################# # REQUIRED: Specify the dataset on your system where you want the configuration files copied.
# Don't include the trailing slash.
# Optional: specify your email address here if you want to receive notification
email="" # Example: configdir="/mnt/tank/sysadmin/config"
# Optional: specify the short name of your ESXi host if you are running FreeNAS configdir=""
# as a VM and you want to back up the ESXi host's configuration
esxihost="" # Remove this code once you've defined configdir above... :-)
# Specify the dataset on your system where you want the configuration files copied. if [ -z "${configdir}" ]; then
# Don't include the trailing slash. echo "Edit script and specify the target directory ('configdir') before using $0"
exit 2
# Example: configdir=/mnt/tank/sysadmin/config fi
configdir="" # Optional: Set non-zero 'd0_tar' flag to have both files stored in a tarball as typically
# needed when restoring a configuration.
# Remove this code once you've defined configdir above... :-) do_tar=1
if [ -z ${configdir} ]; then # Optional: specify your email address here if you want to receive a notification message.
echo "Edit script and specify the target directory ('configdir') before using $0" notifyemail=""
exit 2
fi # Optional: specify the short name of your ESXi host if you are running FreeNAS
# as a VM and you want to back up the ESXi host's configuration
freenashost=$(hostname -s) esxihost=""
fnconfigdest_version=$(< /etc/version sed -e 's/)//;s/(//;s/ /-/' | tr -d '\n') # Get the date and version of TrueNAS/FreeNAS:
fnconfigdest_date=$(date +%Y%m%d%H%M%S)
fnconfigdest="${configdir}"/"${freenashost}"-"${fnconfigdest_version}"-"${fnconfigdest_date}".db rundate=$(date)
echo "Backup FreeNAS configuration database file: ${fnconfigdest}" osvers=$(grep -i truenas /etc/version)
if [ -z "${osvers}" ]; then
iscorral=$(< /etc/version grep "Corral" | awk {'print $1'}) osvers=$(grep -i freenas /etc/version)
if [ -z "${osvers}" ]; then
if [ ! -z "${iscorral}" ]; then osvers="UNKNOWN"
# FreeNAS Corral: make a CLI call: else
cli -e "system config download path=${fnconfigdest}" osvers="FreeNAS"
else fi
# FreeNAS 9.x: Copy the source to the destination: else
cp /data/freenas-v1.db "${fnconfigdest}" osvers="TrueNAS"
fi fi
l_status=$? # Form a unique, timestamped filename for the backup configuration database and tarball
################################################# P1=$(hostname -s)
# Backup the VMware ESXi host configuration: P2=$(< /etc/version sed -e 's/)//;s/(//;s/ /-/' | tr -d '\n')
################################################# P3=$(date +%Y%m%d%H%M%S)
fnconfigdest_base="$P1"-"$P2"-"$P3"
if [ ! -z "${esxihost}" ]; then fnconfigdestdb="${configdir}"/"${fnconfigdest_base}".db
esxihostname=$(ssh root@"${esxihost}" hostname) fnconfigtarball="${configdir}"/"${fnconfigdest_base}".tar
esxiversion=$(ssh root@"${esxihost}" uname -a | sed -e "s|VMkernel ||;s|$esxihostname ||")
esxiconfig_url=$(ssh root@"${esxihost}" vim-cmd hostsvc/firmware/backup_config | awk '{print $7}' | sed -e "s|*|$esxihostname|") # Copy the source database and password encryption secret key to the destination:
esxiconfig_date=$(date +%Y%m%d%H%M%S)
esxiconfig_file="${configdir}"/"${esxihost}"-configBundle-"${esxiconfig_date}".tgz echo "Backup ${osvers} configuration database file: ${fnconfigdestdb}"
echo "Downloading $esxiconfig_url to $esxiconfig_file" cp -f /data/pwenc_secret "$configdir"
wget --no-check-certificate --output-document="${esxiconfig_file}" "${esxiconfig_url}" /usr/local/bin/sqlite3 /data/freenas-v1.db ".backup main '${fnconfigdestdb}'"
fi l_status=$?
################################################# # Validate the configuration file and create tarball:
# Send email notification if indicated:
################################################# if [ $l_status -eq 0 ]; then
dbstatus=$(sqlite3 "$fnconfigdestdb" "pragma integrity_check;")
if [ ! -z "${email}" ]; then printf 'sqlite3 status: [%s]\n' "${dbstatus}"
freenashostuc=$(hostname -s | tr '[:lower:]' '[:upper:]') if [ "${dbstatus}" = "ok" ]; then
freenashostname=$(hostname) l_status=0
freenasversion=$(cat /etc/version) if [ $do_tar -ne 0 ]; then
logfile="/tmp/save_config.tmp" # Save the config DB w/ its original name in the tarball -- makes restoring them easier:
if [ $l_status -eq 0 ]; then cp -f "${fnconfigdestdb}" "${configdir}"/freenas-v1.db
subject="FreeNAS configuration saved on server ${freenashostuc}" tar -cvf "${fnconfigtarball}" -C "${configdir}" freenas-v1.db pwenc_secret
else l_status=$?
subject="FreeNAS configuration backup failed on server ${freenashostuc}" printf 'tar status: [%s]\n' "${l_status}"
fi fi
( else
echo "To: ${email}" l_status=1
echo "Subject: ${subject}" fi
echo "Content-Type: text/html" fi
echo "MIME-Version: 1.0"
printf "\r\n" if [ $l_status -eq 0 ]; then
echo "<pre style=\"font-size:14px\">" echo "Success backing up configuration files to directory ${configdir}"
if [ $l_status -eq 0 ]; then else
echo "Configuration file saved successfully on ${rundate}" echo "Error backing up configuration files to directory ${configdir}"
else fi
echo "Configuration backup failed with status=${l_status} on ${rundate}" l_status=$?
fi
echo "" # Backup the VMware ESXi host configuration:
echo "FreeNAS:"
echo "Server: ${freenashostname}" if [ -n "${esxihost}" ]; then
echo "Version: ${freenasversion}" esxihostname=$(ssh root@"${esxihost}" hostname)
echo "File: ${fnconfigdest}" esxiversion=$(ssh root@"${esxihost}" uname -a | sed -e "s|VMkernel ||;s|$esxihostname ||")
if [ ! -z "${esxihost}" ]; then esxiconfig_url=$(ssh root@"${esxihost}" vim-cmd hostsvc/firmware/backup_config | awk '{print $7}' | sed -e "s|*|$esxihostname|")
echo "" esxiconfig_date=$(date +%Y%m%d%H%M%S)
echo "ESXi:" esxiconfig_file="${configdir}"/"${esxihost}"-configBundle-"${esxiconfig_date}".tgz
echo "Server: ${esxihostname}"
echo "Version: ${esxiversion}" echo "Downloading $esxiconfig_url to $esxiconfig_file"
echo "File: ${esxiconfig_file}" wget --no-check-certificate --output-document="${esxiconfig_file}" "${esxiconfig_url}"
fi fi
echo "</pre>"
) > ${logfile} # Send email notification if indicated:
sendmail ${email} < ${logfile}
rm ${logfile} if [ -n "${notifyemail}" ]; then
fi freenashostuc=$(hostname -s | tr '[:lower:]' '[:upper:]')
freenashostname=$(hostname)
freenasversion=$(< /etc/version sed -e 's/)//;s/(//;s/ /-/' | tr -d '\n')
boundary="===== MIME boundary; ${osvers} server ${freenashostname} ====="
logfile="/tmp/save_config.tmp"
if [ $l_status -eq 0 ]; then
subject="${osvers} configuration saved on server ${freenashostuc}"
else
subject="${osvers} configuration backup failed on server ${freenashostuc}"
fi
printf "%s\n" "To: ${notifyemail}
Subject: ${subject}
Mime-Version: 1.0
Content-Type: multipart/mixed; boundary=\"$boundary\"
--${boundary}
Content-Type: text/html; charset=\"US-ASCII\"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
<html><head></head><body><pre style=\"font-size:14px; white-space:pre\">" > ${logfile}
(
if [ $l_status -eq 0 ]; then
echo "Configuration file saved successfully on ${rundate}"
else
echo "Configuration backup failed with status=${l_status} on ${rundate}"
fi
echo ""
echo "--- ${osvers} ---"
echo "Server: ${freenashostname}"
echo "Version: ${freenasversion}"
echo "Files:"
echo " ${fnconfigdestdb}"
echo " ${configdir}/pwenc_secret"
if [ "$do_tar" -ne 0 ]; then
echo " ${fnconfigtarball}"
fi
if [ -n "${esxihost}" ]; then
echo ""
echo "--- ESXi ---"
echo "Server: ${esxihostname}"
echo "Version: ${esxiversion}"
echo "File: ${esxiconfig_file}"
fi
) >> ${logfile}
printf "%s\n" "</pre></body></html>
--${boundary}--" >> ${logfile}
sendmail -t -oi < ${logfile}
rm ${logfile}
fi