Page 1 of 1

Expand lifespan of SD-card or SSD drive

Posted: Thursday 10 February 2022 11:55
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.

Re: Expand lifespan of SD-card or SSD drive

Posted: Friday 09 September 2022 20:00
by willemd
Thanks, done, this should be in the wiki

Re: Expand lifespan of SD-card or SSD drive

Posted: Saturday 10 September 2022 10:55
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.

Re: Expand lifespan of SD-card or SSD drive

Posted: Saturday 10 September 2022 11:49
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 .........

Re: Expand lifespan of SD-card or SSD drive

Posted: Friday 31 March 2023 11:10
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