Skip to content

Instantly share code, notes, and snippets.

@killerbees19
Created May 26, 2022 18:12
Show Gist options
  • Save killerbees19/e8622039d8f0e84752b76b84a5d77ac1 to your computer and use it in GitHub Desktop.
Save killerbees19/e8622039d8f0e84752b76b84a5d77ac1 to your computer and use it in GitHub Desktop.
Backup TrueNAS config via FTP

Tested at TrueNAS CORE 12.0-U8.1 😊

Installation

  • Save the shell script anywhere and modify destination path to fit your needs.
  • Create a ~/.netrc file as root and paste your FTP credentials:
machine backup-destination.example.net
login yourstupidremoteusername
password yoursecretpassword

Optional

  • Setup a cronjob for the script via WebUI.
  • Test restore of uploaded backup file.

Credits

save_config.sh by Spearfoot

#!/bin/bash
# [email protected] (2022-05-26)
set -euf -o pipefail
destination="backup-destination.example.net"
upload="ftp://$destination/truenas-backup/$(date -u +'%Y')/"
tmpfile="$(hostname -f)-$(date -u +'%Y%m%d%H%M%S').tar.xz"
tcpport=
wol=()
## Optional: Wake On LAN with FTP check
#wol=(wake vtnet0 "aa:bb:cc:dd:ee:ff")
#tcpport=21
## Optional: Alternative pseudo-WOL via HTTP command with FTP check
#wol=(curl --silent --show-error --output /dev/null "https://example.org/cgi-bin/luci/command/cfg012354/args")
#tcpport=21
if [[ -n "${wol[*]}" && -n "$tcpport" ]]
then
for i in {1..30}
do
nc -z -w 1 "$destination" "$tcpport" 2>/dev/null && break
"${wol[@]}"
sleep 10
done
if [[ "$i" -eq 30 ]]
then
echo "ERROR: Could not wake up target device!" >&2
exit 1
fi
fi
function cleanup
{
[[ -n "$tmp" ]] && rm -rf "$tmp"
}
trap cleanup EXIT
tmp=$(mktemp -d -t "truenas-backup")
[[ -n "$tmp" ]] && cd "$tmp" || exit 1
cp -afn /etc/version /data/pwenc_secret "$tmp/"
sqlite3 /data/freenas-v1.db ".backup main '$tmp/freenas-v1.db'"
tar -caf "$tmpfile" freenas-v1.db pwenc_secret version
echo "$(sha512 -q "$tmpfile") $(basename "$tmpfile")" > "$tmpfile.sha512"
curl --silent --show-error --netrc --ssl-reqd --ftp-create-dirs --upload-file "{$tmpfile,$tmpfile.sha512}" "$upload"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment