Event on Reset. Write to EEPROM

One way to solve this is via pseudo non-volatile storage. Just write the data as often as you want and if there is an unexpected shutdown, it will contain the last tidbit of information you saved.

I'm a fan of these EERAM devices. It only requires a single capacitor, and only writes to the real EEPROM when it detects a shutdown. There are no limits to how many write cycles as long as there is power to the device. It would take close to 100K shutdowns before it will degrade the underlying non-volatile storage. And they are cheap

https://www.mouser.com/datasheet/2/268/47L64_Data_Sheet_DS20006168B-1901722.pdf