People often ask these as one question and they are not. An outage on the line is a software problem and it is handled: the terminal switches to offline mode and syncs later. An outage on the mains is a hardware problem and no software setting helps.
The cheap version of the fix is a small UPS that carries the till computer, the router and the receipt printer for fifteen to thirty minutes. That is enough to finish the transaction in front of you and shut down cleanly, which is the actual goal — not trading through a four-hour cut.
If cuts are daily where you are, budget for it as equipment rather than treating each one as an incident. Shops in Lusaka, Kampala and Cotonou generally end up with a UPS per counter and a plan for who shuts what down.