TrueNAS-12.0-U1 Backup config file daily

sorinciupitu

Dabbler
Joined
Jan 17, 2021
Messages
16
Hello,
I'm new in TrueNAS world and I want to ask you how I can backup my config daily, because I run TrueNAS from usb flash drive and I know the risk. After I search on this forum, I find a code line that I run with Cron Jobs. My name pool where I save the backup file is "work" and folder in that pool is "work". This script it save a file, but I'm not sure if that file is good for restore (like I said my version is TrueNAS-12.0-U1). The line is:
Code:
tar -cvf /mnt/work/work/"$(date +%Y_%m_%d)_$HOST_$(cat /etc/version | cut -d' ' -f1).tar.gz" -C /data freenas-v1.db pwenc_secret


Thank you!
 

Hellione

Explorer
Joined
Jan 23, 2021
Messages
55
you dont have to backup your config every day, just do a backup after you changed settings. a backup of the boot config on the (in case of an error not running) pool /mnt is not very helpful. consider saving it somewhere else outside the nas.
 

Hellione

Explorer
Joined
Jan 23, 2021
Messages
55
no code, just do it manually after you changed something.
you could also add a mirror (usb-) drive to secure the boot a bit more.
beware, we are talking about the backup of truenas config, not data on the pool/s?!
 

sorinciupitu

Dabbler
Joined
Jan 17, 2021
Messages
16
Ok, thank you. My intention was to do Cron Job with daily backup, not to worry about the backup file when I make any changes to the settings. After that, I would automatically put the backup file on google drive, to be safe.
 

KevinM

Contributor
Joined
Apr 23, 2013
Messages
106
I back up the freenas configs from our (now) TrueNAS boxes to a centralized system. When we add new boxes I set up SSH key-based logins from this system and add them to the $FREENAS array below. It deletes any backups on the backup server older than $DAYS, and also sends an email summary. Feel free to use/modify as you wish.


Code:
#!/bin/bash

SCRIPT=${0##*/}
HOST=$(hostname --fqdn)
FREENAS=(freenas01 freenas02 freenas03 freenas04 freenas05 freenas06 freenas07 freenas08)
SRCEFILE=/data/freenas-v1.db
DESTDIR=/home/somebody/FreeNAS/backups
DESTFILE=freenas-v1_$(date +%Y%m%d%H%M%S).db
TMPLOG=/tmp/{SCRIPT}-??.log
VARLOG=/var/log/${SCRIPT}.log
RECIPIENT=somebody@somewhere.com
DAYS=30

[ -d ${DESTDIR} ] || mkdir -p ${DESTDIR}

_log() {
    printf "$(date): ${*}\n" >>${TMPLOG} 2>&1;
}

_sendmail() {
    mutt -s "${SUBJECT}" -- ${RECIPIENT} < ${TMPLOG};
}

if pushd ${DESTDIR} &>/dev/null; then
    _log "PASS: pushd ${DESTDIR}."
else
    _log "FAIL: pushd ${DESTDIR}."
    exit 1
fi

for freenas in ${FREENAS[@]}; do
    if scp -q ${freenas}:${SRCEFILE} ${freenas}_${DESTFILE}; then
        _log "PASS: scp -q ${freenas}:/${SRCEFILE} ${freenas}_${DESTFILE}."
    else
        _log "FAIL: scp -q ${freenas}:/${SRCEFILE} ${freenas}_${DESTFILE}."
        exit 1
    fi
done

if [ -d ${DESTDIR} ]; then
    _log "Deleting all files in ${DESTDIR} older than ${DAYS} days..."
    find ${DESTDIR} -maxdepth 1 -type f -mtime +${DAYS} -name '*freenas*' | tee -a ${TMPLOG} | xargs rm -f;
    _log "Completed."
fi

if grep "FAIL" ${TMPLOG} &>/dev/null; then
    RESULT=FAIL
else
    RESULT=PASS
fi

SUBJECT="${RESULT}: ${HOST}: ${SCRIPT}."
_sendmail

printf "\n" >>${TMPLOG}

cat ${TMPLOG} >> ${VARLOG}

sleep 5

rm -rf ${TMPLOG}

popd &>/dev/null
 

sorinciupitu

Dabbler
Joined
Jan 17, 2021
Messages
16
I back up the freenas configs from our (now) TrueNAS boxes to a centralized system. When we add new boxes I set up SSH key-based logins from this system and add them to the $FREENAS array below. It deletes any backups on the backup server older than $DAYS, and also sends an email summary. Feel free to use/modify as you wish.


Code:
#!/bin/bash

SCRIPT=${0##*/}
HOST=$(hostname --fqdn)
FREENAS=(freenas01 freenas02 freenas03 freenas04 freenas05 freenas06 freenas07 freenas08)
SRCEFILE=/data/freenas-v1.db
DESTDIR=/home/somebody/FreeNAS/backups
DESTFILE=freenas-v1_$(date +%Y%m%d%H%M%S).db
TMPLOG=/tmp/{SCRIPT}-??.log
VARLOG=/var/log/${SCRIPT}.log
RECIPIENT=somebody@somewhere.com
DAYS=30

[ -d ${DESTDIR} ] || mkdir -p ${DESTDIR}

_log() {
    printf "$(date): ${*}\n" >>${TMPLOG} 2>&1;
}

_sendmail() {
    mutt -s "${SUBJECT}" -- ${RECIPIENT} < ${TMPLOG};
}

if pushd ${DESTDIR} &>/dev/null; then
    _log "PASS: pushd ${DESTDIR}."
else
    _log "FAIL: pushd ${DESTDIR}."
    exit 1
fi

for freenas in ${FREENAS[@]}; do
    if scp -q ${freenas}:${SRCEFILE} ${freenas}_${DESTFILE}; then
        _log "PASS: scp -q ${freenas}:/${SRCEFILE} ${freenas}_${DESTFILE}."
    else
        _log "FAIL: scp -q ${freenas}:/${SRCEFILE} ${freenas}_${DESTFILE}."
        exit 1
    fi
done

if [ -d ${DESTDIR} ]; then
    _log "Deleting all files in ${DESTDIR} older than ${DAYS} days..."
    find ${DESTDIR} -maxdepth 1 -type f -mtime +${DAYS} -name '*freenas*' | tee -a ${TMPLOG} | xargs rm -f;
    _log "Completed."
fi

if grep "FAIL" ${TMPLOG} &>/dev/null; then
    RESULT=FAIL
else
    RESULT=PASS
fi

SUBJECT="${RESULT}: ${HOST}: ${SCRIPT}."
_sendmail

printf "\n" >>${TMPLOG}

cat ${TMPLOG} >> ${VARLOG}

sleep 5

rm -rf ${TMPLOG}

popd &>/dev/null

Thank you, I will give a try.
 
Top