Skip to content

Instantly share code, notes, and snippets.

@kiler129
Created October 6, 2022 02:27
Show Gist options
  • Save kiler129/ea904b658048dfd19306b965dd5fd8fd to your computer and use it in GitHub Desktop.
Save kiler129/ea904b658048dfd19306b965dd5fd8fd to your computer and use it in GitHub Desktop.
Backups TrueNAS installation configuration
#!/bin/bash
set -e
if [ "$#" -ne 3 ]; then
# Example to keep 7 copies: backup-truenas.sh tnsHome /mnt/backup/server 7
echo "Usage: ${0} StableID DestinationDir KeepCopies"
exit 1
fi
stableId="${1}"
backupDest="${2}"
keepCopies=$3
# ===============================================================
if ! [[ $keepCopies =~ ^[0-9]+$ ]] || [[ $keepCopies -lt 1 ]]; then
echo "ERROR: Number of kept copies should be a positive integer"
exit 1
fi
tnVersion=$(midclt call system.info | jq -r '.version')
tnHost=$(midclt call system.info | jq -r '.hostname')
timeNow=$(date '+%Y-%m-%d-%H%m%S')
backupFilePath="${backupDest}/${stableId}_${timeNow}_${tnHost}_${tnVersion}.tgz"
echo "Backing up host \"${tnHost}\" (${tnVersion}) to ${backupDest}"
(cd /data && tar -czf "${backupFilePath}" freenas-v1.db pwenc_secret)
echo "Cleaning up old copies leaving ${keepCopies} newest ones..."
(cd "${backupDest}" && ls -tp | grep "${stableId}_*" | grep -v '/$' | tail -n +$((keepCopies+1)) | xargs -d '\n' --no-run-if-empty rm --)
echo "OK"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment