Register for the iXsystems Community to get an ad-free experience and exclusive discounts in our eBay Store.

Nightly check of FreeNAS database!

ndcsucks

Newbie
Joined
Aug 30, 2014
Messages
1
Hey gang...

I built the .db file and set up the cron task... I see the backup file in my directory, but when I try to copy to desktop or move it to another location, it tells me I don't have permission. All other files and folders in the same location copy /move fine?
Any idea?
 

diedrichg

Neophyte Sage
Joined
Dec 4, 2012
Messages
1,299
It's this database check still applicable to 9.3 with boot device scrub?
 

cyberjock

Inactive Account
Joined
Mar 25, 2012
Messages
19,526
Yes. The ZFS layers will make sure that whatever you wanted to write to the boot device is there. But what if you unintentionally wrote (such as due to a bug) garbage to the config file? That's what this would identify.

I will say that the likelihood of having corruption of the config file that couldn't be caught by ZFS is pretty slim. It's not likely some bug is going to do something like "dd if=dev/random of=/data/freenas-v1.db bs=1M count=1". But if something equivalent to that were to occur, ZFS would think everything was fine but the config file would not be a valid SQL database.

I still do it because "why not?"

I believe in protection through the layers. Since this is trivial to implement, it's trivial for the system to perform, and there is the possibility that it might help you, I don't see a reason to not do it.
 
Last edited:

diedrichg

Neophyte Sage
Joined
Dec 4, 2012
Messages
1,299
Tyvm! Excellent answer.
 

Dave Grabowski

Neophyte
Joined
Aug 1, 2015
Messages
4
Hi,

GREAT idea with the script. I've modified it to make it more portable... it should run fine on any system without any modifications.

1. The script sends its email to the email address associated with the 'root' user
2. It determines the server name automatically
3. It uses a builtin function to create the temp file
4. If there's an error, it includes the errors in the email (it's debatable as to whether or not to do this; errors spit out by this command could expose security concerns about your NAS; delete this bit if you want)

Code:
#!/bin/sh
# Checks FreeNAS database consistency, and if there's an error, sends an email to the root user

#The command we use to check database consistency
DBCHECK="sqlite3 /data/freenas-v1.db 'pragma integrity_check'"

if [ "$( eval $DBCHECK )" == "ok" ]; then
        echo Database Check OK
        exit 0
    else
        echo Database Check FAILED!
        #Create a temporary file
        TMPFILE=`mktemp`
        #Determine who to send the email to
        EMAIL=`awk '{ if ($1 == "root\:") {print $2 }}' /etc/aliases`
        #Figure out our hostname
        HOSTNAME=`hostname`

        printf "To: $EMAIL\n" >> $TMPFILE
        printf "Subject: ERROR: Database corrupt on server $HOSTNAME\n\n" >> $TMPFILE
        printf "$HOSTNAME has a corrupt FreeNAS config!\n\nResults of database check ($DBCHECK) below:\n---------------\n" >> $TMPFILE
        #Send the results of the database check in the email
        eval $DBCHECK >> $TMPFILE
        #Send the email
        sendmail -t < $TMPFILE
        rm $TMPFILE
        exit 1
    fi
 

George51

Member
Joined
Feb 4, 2014
Messages
126
I recently had an error on one of my pools - and currently going through the process of moving all my scripts to a different pool. When I set up a daily back up of my freenas config, I remember testing the script using

sqlite3 /data/freenas-v1.db 'pragma integrity_check'

pointing to an incorrect file i.e. freehnas-v1.db to trigger the email telling me its corrupt rather than ok.

however this time, regardless of where I point it (files that don't exist etc) it always returns ok, hence never sends the error email. Has this function changed?
 

Mr Snow

Member
Joined
May 22, 2016
Messages
29
I recently had an error on one of my pools - and currently going through the process of moving all my scripts to a different pool. When I set up a daily back up of my freenas config, I remember testing the script using

sqlite3 /data/freenas-v1.db 'pragma integrity_check'

pointing to an incorrect file i.e. freehnas-v1.db to trigger the email telling me its corrupt rather than ok.

however this time, regardless of where I point it (files that don't exist etc) it always returns ok, hence never sends the error email. Has this function changed?
I'm seeing this also. I check the /data directory and I have zero byte files of the names I've been using for testing. So it looks like sqlite3 is actually creating the db file if it doesn't exist before checking it.

I'm currently figuring out how to work around this (I'm not a unix person by trade :D)

Regards,

CJ
 

Mr Snow

Member
Joined
May 22, 2016
Messages
29
ok, I made the following change to allow the fail test to work. Edit the if line to read:

Code:
if [ -f /data/freenas-v1.db ] && [ "$( sqlite3 /data/freenas-v1.db "pragma integrity_check;" )" == "ok" ]; then


Regards,

CJ
 

Ryan Allen

Member
Joined
Oct 11, 2016
Messages
93
I'm running 11.2.
I have tried all versions of this script on this thread.
I get the following error when I run this in Shell just to try it out...
(PS.. the bkpconfig.sh script works just fine.)

[root@freenas11 ~]# sh /mnt/tank/test/scripts/chkconfig.sh
: not foundest/scripts/chkconfig.sh: Code:
/mnt/tank/test/scripts/chkconfig.sh: 45: Syntax error: end of file unexpected (expecting "then")


I'm using Notepad ++

Here is my script:

Code:
#!/bin/sh
# This file runs a database integrity check and emails you in the event that
# your database is corrupt. This file requires you to have *properly* setup
# emailing from the FreeNAS GUI. The following variables are available for
# you to use:
#
# YourEmail: The email address you want to send the email to if your database
# is found to be corrupt. Multiple email addresses are supported if separated
# by a space.
YourEmail="youremail@gmail.com"
# ServerName: How you want your server's name to appear in the email in the
# event that the database is corrupt.
ServerName=freenas11
# TempLocation: Location for the temp file for the email. Default is /tmp
TempLocation=/tmp
# Scroll down to edit the email as you see fit. The default setup is recommended
# since it works and conveys a simple email to let you know what the problem
# is and that you need to take action.
# If you want to test this to ensure it works, simply rename the line with
# freenas-v1.db to pointto a file that doesn't exist. It will error, and you will get
# an email.
if [ -f "$TempLocation"/badconfig.txt ];
then rm "$TempLocation"/badconfig.txt
fi

if [ "$( sqlite3 /data/freenas-v1.db "pragma integrity_check;" )" == "ok" ]; then
#echo "Database is ok."
#bail out with zero (all o.k.) status
exit 0
else
# The following lines are the email that will be sent in the event that errors
# are found. Change it however you wish, just make sure the general format
# is protected.
echo "To: $YourEmail" >> $TempLocation/badconfig.txt
echo "Subject: ERROR: Database corrupt on server $ServerName" >> $TempLocation/badconfig.txt
echo "Your server, $ServerName, has been found to have a corrupt FreeNAS config." >> $TempLocation/badconfig.txt
echo " " >> $TempLocation/badconfig.txt
echo "It is recommended you troubleshoot and correct the problem as soon as possible. Just because the server is operating fine does not mean you can ignore this message." >> $TempLocation/badconfig.txt
echo "$TempLocation"/badconfig.txt
sendmail -t < "$TempLocation"/badconfig.txt
rm "$TempLocation"/badconfig.txt
fi
exit 1


Any suggestions?
 
Top