How to prevent sleep while a Time Machine backup is in progress? I'm one of those greatly suffering from the problem with TM in which the entire backup sparsebundle on a NAS gets corrupted if the Mac goes to sleep in middle of a backup operation. There's a long process for recovering the backup, but it would be far better to avoid the corruption in the first place.

Is there any way to hack the sleep function so that the Mac refuses to go to sleep if a TM backup is currently in progress? Or, if that's not possible, is there at least some way to show a confirmation dialog when manually choosing "sleep" from the apple menu, to prompt for a manual verification that no TM is currently running?

Clarification:
One of the situations I need to protect against, is my habit of manually choosing "sleep" from the apple menu. Hence, something like Caffeine which only prevents automatic sleep, isn't a solution for me.
I have solved this with a bash script. It involves caffeinate built-in command and NoSleepCtrl (prevents sleep on lid close).

In my setup, I have TimeMachine disabled and run it using this command. It should not be difficult to create a wrapper, which would execute it periodically and either ask user for confirmation or wake the computer up and do it automatically.

My experience and steps for TM backup corruption on QNAP.