hi,
Use a 12Vdc low coil current SPCO relay.
Power the relay from a 12V mains power supply, so that the relay is energised while the mains supply is OK.
Connect one of the fan 12V power wires onto the normally closed set of relay contacts.
When the mains fails the relay will de-energise, the contacts will close and the fan will run until the mains is restored.