Power loss recovery macros?

What I would love to see would be a way to save the current Gcode state to a file on the Pi. That would allow to create a power loss recovery with a small PSU that keeps only the Pi on for a short time (enough to trigger that pause command and save the printer’s current position on the SD card before shutting itself cleanly. Then you would reset the motor positions (for Z maybe enter the last saved Z positions, for XY you could G28 XY) then re-heate the heaters, recover the Gcode state from the file, and resume.
Not sure if there’s a way to have that Gcode save state saved, or maybe written in a SAVE_CONFIG?