SMB/samba uptime monitoring

I wanted to be able get a notification when a SMB/samba server went down so I set up a script on a FreeBSD server. This script will connect to a SMB share and check for the existence of a file/folder on that share.
If it does not exist then it will E-mail you. I put this in /home/username/bin
and it needs a tmp directory in /home/username/bin/tmp (or modify the script to use some other directory).
Configure the variables at the beginning for your use.

I named this file check_fileserver_smb.sh

#!/bin/sh

PATH=$PATH:/usr/local/bin:/usr/bin

SMBUSERNAME='myusername'
SMBPW='mypassword'
SERVERTOCHECK='fileserver'
SERVICETOCHECK='archive'
#Note that this needs to be only the first word of the folder name (spaces not supported)
FOLDERTOCHECK='Photos'
NOTIFYEMAIL='youremail@maildomain.comm'

cd ~/bin

FOLDERCHECK=$( smbclient --user=$SMBUSERNAME -c "ls" \\\\$SERVERTOCHECK\\$SERVICETOCHECK $SMBPW 2>/dev/null | grep $FOLDERTOCHECK | cut -f 2 -w )

if [ "$FOLDERCHECK" == "$FOLDERTOCHECK" ]; then
	#echo "Yep, $SERVERTOCHECK samba is up."

	if [ -e tmp/uptime-$SERVERTOCHECK-$SERVICETOCHECK-down ]; then
		rm tmp/uptime-$SERVERTOCHECK-$SERVICETOCHECK-down
		#echo "Back up!"
		date | mail -s "Service Up: $SERVERTOCHECK-$SERVICETOCHECK samba" $NOTIFYEMAIL
	fi
else
	#echo "No, $SERVERTOCHECK samba is down!"
	if [ -e tmp/uptime-$SERVERTOCHECK-$SERVICETOCHECK-down ]; then
		#echo "still down..."
	else
		touch tmp/uptime-$SERVERTOCHECK-$SERVICETOCHECK-down
		date | mail -s "Service Down: $SERVERTOCHECK-$SERVICETOCHECK samba" $NOTIFYEMAIL
	fi
fi

In /etc/crontab I have this to check every 5 minutes:

*/5 * * * * username /home/username/bin/check_fileserver_smb.sh

PS The script would need to be modified to support spaces in any server names, etc.

FreeBSD src via subversion

I always forget the exact command and URL to get a copy of the FreeBSD source using subversion. So here it is, for my convenience and yours!

To get Release 11.0 (includes bugfix and security patches; keeps you at 11.0):

cd /usr
svnlite co svn://svn.freebsd.org/base/releng/11.0 src

To get Stable 11 (includes general updates; updates you to the latest 11.x):

cd /usr
svnlite co svn://svn.freebsd.org/base/stable/11 src

To get ports:

cd /usr
rm -r /usr/ports
svnlite co svn://svn.freebsd.org/ports/head ports

Older versions of FreeBSD might not have svnlite in base. In that case install svn and use that instead.

Firebird Automatic Database Restore on to Backup Server

I maintain a server running the Firebird database that backs-up the database on a daily basis. I also have a server that can be used as a backup in case the main server fails. I wanted to make it so the database is automatically restored on to the backup server every day so I always have the previous days’ data on that server ready to go.

This is specifically for Quantum Control software (which uses Firebird) but I hope it might be helpful to other users of Firebird as well.

This is mainly for users who have some knowledge of database backup and windows batch scripting since the script will require modification to fit your circumstances.

I have a batch file (cp_bak.bat) located in a folder on the desktop names “mainserver QC bak”.

If you use this script you will want to modify several things. First off update all of the locations and server names. Here I use “mainserver” for my database server that clients are using day to day.
For the “net use” command you will want to use appropriate credentials to access the backup file (which I back up with 7z).

@echo off

cd "\Documents and Settings\Administrator\Desktop\mainserver QC bak"

echo Accessing share (net use)...
net use \\mainserver\Quantum mikepassword /USER:mike
echo Copying QC.7z from mainserver...
copy \\mainserver\Quantum\bak\QC.7z "C:\Documents and Settings\Administrator\Desktop\mainserver QC bak\"

echo Removing old extract (Will print error if it does not exist)...
del QUANTUM.FDB.bak
echo Extracting...
C:\Admin\Bin\7za.exe e QC.7z

echo Stopping firebird
sc stop FirebirdGuardianDefaultInstance
timeout 4
echo Overwriting database with backup...
move QUANTUM.FDB.bak C:\Quantum\Database\QUANTUM.FDB
echo Starting firebird
sc start FirebirdGuardianDefaultInstance
timeout 3
echo "Unlocking database..."
"c:\quantum\firebird\bin\gfix.exe" -online "127.0.0.1:c:\quantum\database\quantum.fdb" -user sysdba -pass masterkey 2> "c:\quantum\database\error.txt"

timeout 1

echo "Setting correct security database, turning off maintenance broadcast"
cd "\Documents and Settings\Administrator\Desktop\mainserver QC bak"
"c:\quantum\firebird\bin\isql.exe" "127.0.0.1:c:\quantum\database\quantum.fdb" -i security_update.fbsql -u sysdba -p masterkey 

echo Done!

This script references a SQL file in the same directory named security_update.fbsql

update quantum set locked='F';

update quantum set serverdb='192.168.2.3:c:\quantum\firebird\security.fdb', broadcast='F';

Important things to not in this is that this is the security database on the backup server with the backup IP address. It also disables the database maintenance broadcast message (in Quantum Control).