how to switch to backup battery when power fail?
Diodes are a very simple option if you've got the voltage headroom.

Another good reference design is the arduino power selection circuit:



The P channel mosfet on the USB VIN prevents back powering of the usb line, and also doesn't introduce a diode voltage drop.

One thing you'll need to check for safety is the leakage from the 24v supply into the battery. Most semi-conductors like diodes leak a little bit in the uA range. If you're not careful the battery could be overcharged by this and potentially burst into flames!

There are old-school options you could also consider. For example, use a spdt relay with a coil connected to your power supply. Connect COM to the MCU, NO to the power supply, and NC to the battery. You'll need a large cap or something to supply power for a few ms while the relay switches.