A sdcard friendly config on Raspberry

Topics (not sure which fora)
when not sure where to post, post here and mods will move it to right forum.

Moderators: leecollings, remb0

Post Reply
PBdA
Posts: 15
Joined: Monday 14 December 2015 16:51
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.5893
Location: Colomiers, France
Contact:

A sdcard friendly config on Raspberry

Post by PBdA »

Domoticz tends to write often on disk. When disk is a sdcard it is better to avoid to write too often. The idea is to mount /tmp in ramdisk and have Domoticz to work in /tmp

Config: Raspberry Pi 3 with Raspbian Jessie.

I start Domoticz with systemd. File is /etc/systemd/system/domoticz.service :

Code: Select all

[Unit]
Description=Domoticz home automation
After=network.target

[Service]
ExecStartPre=/usr/local/bin/set-domoticz start
ExecStart=/opt/domoticz/domoticz -www 81 -sslwww 0 -dbase /tmp/domoticz.db
ExecStopPost=/usr/local/bin/set-domoticz stop
Restart=always

[Install]
WantedBy=multi-user.target
Bash script /usr/local/bin/set-domoticz gets a valid DB at start and save DB to disk at stop :

Code: Select all

#! /bin/bash

# path to adapt
domoticzdir="/opt/domoticz"		# where lives Domoticz
db="/tmp/domoticz.db"			# BD path in a tmpfs filesystem
ozwlog="/tmp/OZW_Log.txt"		# OpenZwave log path in a tmpfs filesystem
heartbeat="/tmp/domoticz.heartbeat"	# heartbeat file for watchdog
########################################################################
bckbase="$domoticzdir/backups/hourly/backup-hour-"

checkdb() {
	if [ "$(sqlite3 -batch -bail -cmd 'pragma integrity_check;' -cmd '.quit' $1 2> /dev/null)" == ok ]; then
		rm -f $1-shm $1-wal
		return 0
	fi
	return 1
}

case "$1" in
	 start)
		touch $heartbeat
		rm -f $domoticzdir/Config/OZW_Log.txt
		ln -s $ozwlog $domoticzdir/Config/

		[ -e $db -a checkdb $b ] && exit 0	# DB exists and is OK
# looking for a working DB, newer first
		for f in $(ls -t $bckbase*.db); do
			if checkdb $f; then
				cp -a $f $db
				exit 0
			fi
		done
		;;
	stop)
# save DB if it is OK
		[ -f $db ] && checkdb $db && cp -a $db $bckbase$(date "+%H").db ||  exit 1
		;;
esac

exit 0
Domoticz saves the on disk every hour. It's OK for me if I lost an hour of measure.

File heartbeat is for watchdog. For watchdog to monitor Domoticz, put in an lua script _time_XXX.lua :

Code: Select all

-- pour le watchdog
file = io.open("/tmp/domoticz.heartbeat", "w")
file:close()
and in /etc/watchdog.conf :

Code: Select all

file = /tmp/domoticz.heartbeat
change = 300
Watchdog can be dangerous: if things go wrong, Raspberry can reboot continuously. To avoid continuous reboot, I add to rc.local :

Code: Select all

# BdA added for the watchdog
lbf="/root/lastboot"
actuel=$(date +%s)
if [ -f "$lbf" ]; then
  last=$(cat "$lbf")
  if (( actuel - last < 120 )); then
    systemctl stop watchdog
  fi
fi
echo $actuel > "$lbf"
date | mailx -s "Raspberry $(hostname) boot" [email protected]
PBdA
MasterCATZ
Posts: 11
Joined: Friday 14 July 2017 21:24
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: A sdcard friendly config on Raspberry

Post by MasterCATZ »

PBdA wrote:
File heartbeat is for watchdog. For watchdog to monitor Domoticz, put in an lua script _time_XXX.lua :

Code: Select all

-- pour le watchdog
file = io.open("/tmp/domoticz.heartbeat", "w")
file:close()

you have me lost at this stage
I do not know where to add this code what is the file name/location?

Also for some reason, the hourly backups are not working the DB is been written to the ramdrive (/tmpfs), but no DB sync is happening (home/pi/domoticz), also it is not grabbing the existing DB that is already written to disk.

sudo nano /etc/fstab

Code: Select all

tmpfs   /tmpfs    tmpfs    defaults,noatime,nosuid,mode=0755,size=128m    0 0

sudo nano /usr/local/bin/set-domoticz

Code: Select all

#! /bin/bash

# path to adapt
domoticzdir="/home/pi/domoticz"      # where lives Domoticz
db="/tmpfs/domoticz.db"         # BD path in a tmpfs filesystem
#ozwlog="/tmpfs/OZW_Log.txt"      # OpenZwave log path in a tmpfs filesystem
heartbeat="/tmpfs/domoticz.heartbeat"   # heartbeat file for watchdog
########################################################################
bckbase="$domoticzdir/backups/hourly/backup-hour-"

checkdb() {
   if [ "$(sqlite3 -batch -bail -cmd 'pragma integrity_check;' -cmd '.quit' $1 2> /dev/null)" == ok ]; then
      rm -f $1-shm $1-wal
      return 0
   fi
   return 1
}

case "$1" in
    start)
      touch $heartbeat
      rm -f $domoticzdir/Config/OZW_Log.txt
      ln -s $ozwlog $domoticzdir/Config/

      [ -e $db -a checkdb $b ] && exit 0   # DB exists and is OK
# looking for a working DB, newer first
      for f in $(ls -t $bckbase*.db); do
         if checkdb $f; then
            cp -a $f $db
            exit 0
         fi
      done
      ;;
   stop)
# save DB if it is OK
      [ -f $db ] && checkdb $db && cp -a $db $bckbase$(date "+%H").db ||  exit 1
      ;;
esac

exit 0

sudo nano /etc/systemd/system/domoticz.service

Code: Select all

[Unit]
Description=Domoticz home automation
After=network.target

[Service]
ExecStartPre=/usr/local/bin/set-domoticz start
ExecStart=/home/pi/domoticz/domoticz -www 8080 -sslwww 0 -dbase /tmpfs/domoticz.db
ExecStopPost=/usr/local/bin/set-domoticz stop
Restart=always

[Install]
WantedBy=multi-user.target

is their another program I need to install or something?
Last edited by MasterCATZ on Saturday 15 July 2017 0:26, edited 8 times in total.
PBdA
Posts: 15
Joined: Monday 14 December 2015 16:51
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.5893
Location: Colomiers, France
Contact:

Re: A sdcard friendly config on Raspberry

Post by PBdA »

/etc/watchdog.conf config file tells watchdog that file /tmp/domoticz.heartbeat should not older than 300 seconds. LUA time script renew file every minute. That way, if domoticz hangs for some reason, watchdog will reboot Raspberry after 5 minutes.
PBdA
MasterCATZ
Posts: 11
Joined: Friday 14 July 2017 21:24
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: A sdcard friendly config on Raspberry

Post by MasterCATZ »

Thanks for the fast reply, and the code to build from,

I edited the original post so that you might be able to help fill in the information I am missing
I have no idea what to do with the lua code

and unsure why the db is not being synced

my temp path is tmpfs and my domoticz location is /home/pi/domoticz

pi@raspberrypi:/tmpfs $ ls -lart
417792 Jul 15 08:32 domoticz.db

Code: Select all

total 4468
drwxr-xr-x 23 root root    4096 Jul 15 05:47 ..
-rw-r--r--  1 root root       0 Jul 15 07:35 domoticz.heartbeat
-rw-r--r--  1 root root   32768 Jul 15 07:40 domoticz.db-shm
drwxr-xr-x  2 root root     120 Jul 15 07:40 .
-rw-r--r--  1 root root  417792 Jul 15 08:32 domoticz.db
-rw-r--r--  1 root root 4120032 Jul 15 08:33 domoticz.db-wal
pi@raspberrypi:~/domoticz $ ls -lart
487424 Jul 15 05:47 domoticz.db

Code: Select all

total 10896
-rw-r--r--  1 pi   pi      35147 Nov 14  2016 License.txt
-rw-r--r--  1 pi   pi       4347 Nov 14  2016 domoticz.sh
-rwxr-xr-x  1 pi   pi        600 Nov 14  2016 updaterelease
-rwxr-xr-x  1 pi   pi        583 Nov 14  2016 updatebeta
-rw-r--r--  1 pi   pi       3414 Nov 14  2016 server_cert.pem
drwxr-xr-x  4 pi   pi       4096 Feb  3 02:05 plugins
drwxr-xr-x  8 pi   pi       4096 Apr 16 23:05 scripts
-rw-r--r--  1 pi   pi      84349 Jul  2 00:05 History.txt
drwxr-xr-x 13 pi   pi       4096 Jul  5 23:41 www
-rwxr-xr-x  1 pi   pi   10492848 Jul 10 19:07 domoticz
-rw-r--r--  1 pi   pi     487424 Jul 15 05:47 domoticz.db
drwxr-xr-x  5 root root     4096 Jul 15 07:00 backups
drwxr-xr-x  7 pi   pi       4096 Jul 15 07:29 .
drwxr-xr-x 73 pi   pi       4096 Jul 15 07:35 Config
drwxr-xr-x 26 pi   pi       4096 Jul 15 07:35 ..
-rw-r-----  1 root root      236 Jul 15 07:42 domocookie.txt
I have decided to change

Code: Select all

bckbase="$domoticzdir/backups/hourly/backup-hour-"
to

Code: Select all

bckbase="$domoticzdir/backups/RAM/backup-hour-"
as I think domoticz internal backup is over writing the scripts backup
the other thing I have done is changed all file permissions, so will see how this goes when I am shopping

I am needing to data log 50 temp sensors 1 sec polling

*edit* still no hourly backup has been made


/home/pi/domoticz/scripts/lua

is this where you make a file called
script _time_XXX.lua
Last edited by MasterCATZ on Saturday 15 July 2017 9:21, edited 1 time in total.
User avatar
Egregius
Posts: 2582
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: A sdcard friendly config on Raspberry

Post by Egregius »

PBdA
Posts: 15
Joined: Monday 14 December 2015 16:51
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.5893
Location: Colomiers, France
Contact:

Re: A sdcard friendly config on Raspberry

Post by PBdA »

Lua script should be placed in directory /home/pi/domoticz/scripts/lua/ and is name should like script_time_xxxx.lua

To be more simple, my scripts rely on Domoticz backups. Backupdir is the place where Domoticz put the backups: /home/pi/domoticz/backups/ and automatic backups should be enabled in Domoticz settings.
PBdA
PBdA
Posts: 15
Joined: Monday 14 December 2015 16:51
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.5893
Location: Colomiers, France
Contact:

Re: A sdcard friendly config on Raspberry

Post by PBdA »

And also domoticz.service should be enabled at boot by command:

Code: Select all

systemctl enable domoticz.service
To avoid a reboot you can start it first time by command:

Code: Select all

systemctl start domoticz.service
PBdA
MasterCATZ
Posts: 11
Joined: Friday 14 July 2017 21:24
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: A sdcard friendly config on Raspberry

Post by MasterCATZ »

I'll keep poking around

what I might end up doing is a sync using google drive or something

just unsure why I can not get it to load up the last db on restart
if your using the internal backup I might have thought of the problem
this is where the backup files are put
/home/pi/domoticz/backups/hourly

this might need to go another directory deep
bckbase="$domoticzdir/backups/backup-hour-"

so I will give this ago
bckbase="$domoticzdir/backups/hourly/backup-hour-"
MasterCATZ
Posts: 11
Joined: Friday 14 July 2017 21:24
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: A sdcard friendly config on Raspberry

Post by MasterCATZ »

huge facepalm I don't have sqlite3 actually installed :oops:

all working sweet :roll:
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 1 guest