Spending three nights or two nights, the 11th and 12th of Zhul-Hijjah, at Mina is necessary (wajib), according to the three Imams of the Islamic law. The Hanafi school regards sleeping at Mina as sunnah only.
Ibn ‘Abbas said, “After you have thrown the pebbles you may spend the night wherever you wish.” This is reported by Ibn Shaibah. Mujahid said, “There is no harm if one spends the first portion of the night at Makkah, and the last one at Mina, or the first portion of the night at Mina and the last one at Makkah.” Ibn Hazm said, “A pilgrim who does not spend the night at Mina does wrong, but he does not incur any penalty.
There is consensus that in the case of those who have a genuine excuse, like water carriers, and shepherds, sleeping the night at Mina is not necessary, and if they leave out anything they don’t incur any penalty.
The uncle of the Prophet (peace be upon him), Abbas, sought and got permission from the Prophet (peace be upon him) to spend these nights (supposed to be spent in Mina) at Makkah so that he could attend to his functions as provider of drinking water to the pilgrims. This was reported by Bukhari and others.
‘Asam bin ‘Adi reported that Prophet (peace be upon him) excused the shepherds from sleeping at Mina. This is reported in the Sunan, and Tirmizhi considers it a sound hadith.