I have my database stored on an external harddrive hooked up to my Pi, which I also use to store all of my camera footage from my security system running MotionEye on the same Pi.
I just had my card fail on me 3 days ago, but what saved me, is that I have a live, bootable backup of my SD card that is updated at midnight every night. So I have a backup SD card in a tiny reader stuck in one of the USB ports, and at midnight, it mounts the partitions and rsync’s only the changes between the 2 cards.
When my card failed 3 days ago, I shutdown the Pi, popped out the backup SD from the reader and put it into my Pi in place of the dead one and I was backup and running with my full Home Assistant setup in a few minutes, without any loss of data. I have upgraded both my primary and backup to use 32 GB cards, mainly because they are cheap and I have a lot of other projects I am running on the same Pi and could use the extra space.
So once my card fails, I swap it with the backup and put another brand new 32GB in the card reader so that the initial backup starts over again.
Here is the github for the script:
https://github.com/billw2/rpi-clone
I have 2 copies of the script…
1 unaltered that asks questions, such as verification if you want to overwrite the SD card, etc, and if you want to unmount after the backup. That way I can run a manual backup if I want, in cases when I make a lot of changes and want to make sure those changes are backed up without having to wait till midnight.
and
1 that I edited to mark out the prompts so that I can run it automated with a cron job.
This is the best option for a backup, because it backs up the live running system, and is a fully bootable backup, so the backup card is ready to be booted on a moments notice should anything happen to your primary card.
UPDATE**
I forgot a couple of extra things:
-
I try to boot from the back up at least once every couple weeks, just to ensure that it is still a good booting copy.
-
I also run a separate script that creates a .img of my current running system every other day, again, without shutting down, so if for some reason my live backup SD card will not boot, I have a .img that I can dd to a new card just like you would the noobs .img or a raspbian .img, so I can just copy it to a new SD card and be back up and running again without no more than 1 day behind on data. I have it set to be limited to 3 backups, so I only have 3 .img files at a time and it removes the oldest one before backuping up a new one.
Here is where you can get the script for creating .img files:
https://www.linux-tips-and-tricks.de/en/raspberry/303-pi-creates-automatic-backups-of-itself
There is a LOT of information on that page, so read through it at your leisure, but it is necessary to understand exactly how the script works and how to use it.