In the mid-1100s, the Fatimid caliphate was crumbling, and Egypt had descended into a condition of near anarchy. The official head of state was the Caliph, but the true power was the Egyptian vizier, and various Egyptian governors competed with each other for the position, often with great violence. During the 1150s, Shawar was the Fatimid governor of Upper Egypt for five years, but he made a deal with the Sultan of Damascus, Nur ed Din, deserted the Fatimids and joined the Seljuks in Syria. Shawar initially used the Sultan's aid, took control of Cairo, and quickly killed his predecessor and his predecessor's entire family. But nine months later, Shawar himself was overthrown by one of his lieutenants, Dirgham. He again sought assistance from Sultan Nur ad-Din, who sent one of his generals, Shirkuh, to settle the dispute. Dirgham was killed, and Shawar was restored to power. However, Shawar then argued with Shirkuh, and allied with the Crusader king, Amalric I of Jerusalem, who attacked Shirkuh at Bilbeis in August-October, 1164 (see Crusader invasion of Egypt). The siege ended with a stalemate, and both Shirkuh and Amalric agreed to withdraw from Egypt.
In 1166, Shirkuh attempted another attack, but Shawar called for reinforcements from Amalric, who arrived at the same time in January 1167. Fighting continued into Egypt, as far as al Babayn, just south of Cairo. There, Shirkuh's army achieved a major victory over Amalric in March. This resulted in another stalemate, and both Shirkuh and Amalric again simultaneously withdrew their respective forces in August 1167, leaving Shawar in power, though Amalric left a garrison in Cairo, and Egypt was required to pay increased tribute to Amalric's government in Jerusalem.
In the winter of 1168, Amalric again attacked Egypt, and Shawar switched alliances again, this time going back to Shirkuh, who he had betrayed in 1164. Shirkuh and Shawar attempted to force the Crusader garrison out of Egypt, but Amalric pressed on, until his army was camped south of Fustat. Seeing Amalric's invasion imminent, Shawar ordered the burning of his own city, to keep it from Amalric's hands. Shirkuh forced Amalric to withdraw, and then conquered Egypt with his own forces. In January 1169, Cairo fell, and Shirkuh had Shawar executed. Shirkuh was named the new vizier, but his reign lasted only two months. Already an obese man, he died of "indigestion", and was succeeded in the viziership by his nephew, Saladin.
Shawar ordered that Fustat be evacuated. He forced [the citizens] to leave their money and property behind and flee for their lives with their children. In the panic and chaos of the exodus, the fleeing crowd looked like a massive army of ghosts.... Some took refuge in the mosques and bathhouses...awaiting a Christian onslaught similar to the one in Bilbeis. Shawar sent 20,000 naphtha pots and 10,000 lighting bombs [mish'al] and distributed them throughout the city. Flames and smoke engulfed the city and rose to the sky in a terrifying scene. The blaze raged for 54 days....