How to save data in arduino when it's power goes off
Look up EEPROM. it is a memory that saves content even though power is off. 

You can write data in EEPROM and when power comes back read it back.

there are safe ways to write in EEPROM without corrupting it.