Expand lifespan of SD-card or SSD drive

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
EddyG
Posts: 1042
Joined: Monday 02 November 2015 5:54
Target OS: -
Domoticz version:

Expand lifespan of SD-card or SSD drive

Post by EddyG »

If you use the 'data' section in dzVents scripts or use executeShellCommand in your scripts with a callback, then data is (frequently) written to the default path /home/pi/domoticz/scripts/dzVents/data.
This method save write cycles to the SD-card or SSD drive.
You should have rsync installed or change the rsync command to the cp command
You might have to do some small changes if you run Domoticz as a different user.
First create a tmpfs at boot of this path in /etc/fstab with

Code: Select all

tmpfs                                       /home/pi/domoticz/scripts/dzVents/data  tmpfs  rw,size=1M  0   0
Size of 1 Mb should be enough.
Then create a backup directory with

Code: Select all

mkdir /home/pi/domoticz/scripts/dzVents/data_backup
Now create a service that does the job with

Code: Select all

sudo nano /lib/systemd/system/ramdisk-sync.service
and paste the following code

Code: Select all

[Unit]
Before=umount.target

[Service]
Type=oneshot
User=root
ExecStartPre=/bin/chown -Rf pi:pi /home/pi/domoticz/scripts/dzVents/data/
ExecStart=/usr/bin/rsync -ar /home/pi/domoticz/scripts/dzVents/data_backup/ /home/pi/domoticz/scripts/dzVents/data/
ExecStop=/usr/bin/rsync -ar /home/pi/domoticz/scripts/dzVents/data/ /home/pi/domoticz/scripts/dzVents/data_backup/
ExecStopPost=/bin/chown-Rf pi:pi /home/pi/domoticz/scripts/dzVents/data_backup
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target
Execute the following commands

Code: Select all

sudo systemctl daemon-reload
sudo systemctl enable ramdisk-sync.service
sudo systemctl start ramdisk-sync.service
sudo reboot
Now you are done and have a 1M ramdrive for the Domoticz data which will be saved to the backup dir if you shutdown or reboot the system and will be copied back from that backup dir to the data dir at boot of the system.
willemd
Posts: 642
Joined: Saturday 21 September 2019 17:55
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.1
Location: The Netherlands
Contact:

Re: Expand lifespan of SD-card or SSD drive

Post by willemd »

Thanks, done, this should be in the wiki
lost
Posts: 660
Joined: Thursday 10 November 2016 9:30
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Expand lifespan of SD-card or SSD drive

Post by lost »

For all FS use (especially logs etc...), you may also add this in your /etc/sysctl.conf:
vm.dirty_ratio=60
vm.dirty_expire_centisecs=120000

=> More caching time+volume => more writes to media can be merged reducing flash wear. Note this may be at the expand of more losses in case of sudden power fail.
My system uses a "mini ups" that saves my internet access modem/router 12V + 5V PI USB power as well & can last several hours.
Toulon7559
Posts: 858
Joined: Sunday 23 February 2014 17:56
Target OS: Raspberry Pi / ODroid
Domoticz version: mixed
Location: Hengelo(Ov)/NL
Contact:

Re: Expand lifespan of SD-card or SSD drive

Post by Toulon7559 »

A function for 'safe' datastorage with reduced wear in RAMDisk or in External Disk seems of value for all use of Domoticz, not only for experienced 'scripters', and not only for dzVents.
Suggestion to make it a simple, standard function of Domoticz under tab Setup as companion for the manual Backup/Restore functions related to domoticz.db:
e.g. layout as suggested in this message?
Setup for related base-functions of Domoticz and for dzVents, Python etc. certainly best handled by the experts in this forum:
for most 'standard'-users probably a maze to find the appropriate setup directly linked to the capabilites of the underlying O.S. and applications .........
Set1 = RPI-Zero+RFXCom433+S0PCM+Shield for BMP180/DS18B20/RS485+DDS238-1ZNs
Set2 = RPI-3A++RFLinkGTW+ESP8266s+PWS_WS7000
Common = KAKUs+3*PVLogger+PWS_TFA_Nexus
plus series of 'satellites' for dedicated interfacing, monitoring & control.
JackVeraart
Posts: 12
Joined: Thursday 25 June 2020 13:41
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.1
Location: Netherlands, near Bergen op Zoom
Contact:

Re: Expand lifespan of SD-card or SSD drive

Post by JackVeraart »

I run my complete Domoticz from a ram disk for some years now. Without problems.

In short:
My /home/pi/domoticz folder is a ram disk and /home/pi/Domoticz_disk_copy is a copy of that on the uSD card.
I changed /etc/init.d/domoticz.sh so that with a stop of Domoticz everything is copied to the uSD card and with a start everything is copied to the ram drive.
A restart is the same as stop and start so to make a regular backup you can put a restart of your Domoticz in cron.

I had no issues during all the years I use this. I had no unplanned power failures and when I pull the plug of my Raspberry Pi to test this and put it back to boot it, it simply starts with what was there during the last backup.
Off course when you can not loose any data due to power failure, do not do this.

Details on how to get this up and running including the full /etc/init.d/domoticz.sh script are in https://github.com/JackV2020/Domoticz-Run-From-RAM-Disk
R. Pi 3 : pivpn, Pi-Hole, minidlna,....... Domoticz with own plugins only
R. Pi 4 : pivpn, NextCloud, minidlna... Domoticz with own plugins only
Toon 1 & 2 with own apps
Some plugins, apps and notes : https://github.com/JackV2020
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest