Power failure battery backup

I want to send some data with Pro Mini and Rf24l01. It needs to work no matter if power is cut off. So, it needs a batteries.

How to set it, so it works from a power supply, but when power switch off, it goes on with batteries. When power gets back, switch to mains again, and charge that battery.

My pick are 18650 batteries with tp4056 module. But what circuit to use to switch source when mains goes off?

I think you need to look into relays or switching Mosfet.

More specifications would be useful.

Do you know the power draw of your unit?
Do you know the maximum current draw of the unit (when it is doing its work)?
Do you know the idle current (the lowest)?

What is considered the worst case scenario for a brown/power out?
What is the usual power out duration?

What is your circuit doing? Is it polling for data, sleeping and waking after x seconds etc...
Some design changes could even make this a completely mains-less device removing the need for "back-up" almost completely.
For example, a reading an hour can happily be done on even a power capacitor bank + solar panel trickle for basic stuff.

But what circuit to use to switch source when mains goes off?

Two diodes, similar to this:

What you need is a <50cent "TP4056 Li-Po Charger protected" - does the charging and protecting battery from overcharge and under discharge voltages. Power an 8Mhz arduino directly from the "load" leads, or a 16Mhz (5V) one with a boost converter.

the module can be powered by usb, or 6v solar panels. See it how is done in this highly educative movie from the guy with a swiss accent. #154 Solar Charger for Microcontrollers - YouTube

You can do a site search on "backup battery charger".

Sorry for late respond...

It's pro mini 3,3V 16MHz which reads temp with NTC a send data via rf24l01 every 30sec.

It needs to operate no matter if power goes down, and there are times it goes down. So it is not necessary do switch immediately, but it needs to switch to battery. When power comes on, switch to power supply and battery starts charging if necessary.

It seems that beside charger and batteries i only need this...

Please correct me if i am wrong.