It is a mitzva to wage a defensive war against Israel’s enemies. This mitzva is even greater than the mitzva of saving human life, as one is not required to risk his own life in order to save the life of another, or even multiple lives. In contrast, it is a mitzva – incumbent upon every individual – to risk one’s life to save the Jewish people from their enemies (Mishpat Kohen §143; Tzitz Eliezer 13:100; see Peninei Halakha: Collected Essays II 11:3).
Therefore, if enemies attack Israel, it is a mitzva to wage war against them even if this will endanger lives and require Shabbat desecration. Indeed, Rambam rules: “There is a mitzva incumbent upon all capable Jews to come to the aid of their brothers who are under siege, and to save them from non-Jews on Shabbat; they may not delay until after Shabbat…” (MT 2:23). Similarly, if it is known that enemies or terrorists are planning to attack Jews, it is a mitzva to attack them in order to deter them. If there is a strategic objective served by attacking them on Shabbat, we attack on Shabbat (Heikhal Yitzĥak, OĤ 37:3; Amud Ha-yemini §16; see Rema 329:6).
Furthermore, it is also a mitzva to wage war to prevent future danger, even though doing so will put lives at risk and require Shabbat desecration. This is in accordance with the statement of the Sages that if enemies come to pillage border towns, even if they are taking only straw and hay, “we attack them with weapons and desecrate Shabbat on their account” (Eruvin 45a). We do this because if our enemies know that they can steal without repercussions, they will ultimately end up attacking people. This is also the ruling of Shulĥan Arukh (329:6). Accordingly, it is a mitzva to perform ongoing security operations on Shabbat, to protect our borders from our enemies. Nowadays, the entire country of Israel has the status of border towns with respect to preventing terror attacks (R. Shlomo Goren). Therefore, throughout Israel, it is a mitzva to perform ongoing security operations on Shabbat, to protect life and property.
If Jews who do not observe Shabbat go on a hike on Shabbat in an area where it is necessary to travel with an armed escort, and there is no way to prevent them from hiking in the first place, it is a mitzva for soldiers to protect them, even if this entails Shabbat desecration. Even though it is the hikers’ Shabbat desecration that creates the need for security, nevertheless, since they are in fact in a dangerous place, they must be protected from the enemy (R. Goren, Meshiv Milĥama 1:7 and 2:110; see Ha-tzava Ka-halakha ch. 21). However, soldiers may not help them desecrate Shabbat. Thus, soldiers may not open checkpoints for them so that they can pass through, give them travel permits, or board their bus to enable them to set out. Only after the group is already underway may soldiers provide security for them.
Bodies of fallen soldiers may be retrieved from the battlefield on Shabbat to ensure that they do not fall into enemy hands. Even though technically one may not desecrate Shabbat in order to save corpses, since abandoning bodies harms the morale of soldiers, and since Israeli society is willing to free terrorists in order to retrieve bodies captured by the enemy, retrieving them from the battlefield prevents danger to life. After the bodies are recovered, one may not desecrate Shabbat any further to care for the bodies (Meshiv Milĥama vol. 1, p. 61 and 2:117; Ha-tzava Ka-halakha ch. 20).
The war to conquer the Eretz Yisrael is considered a milĥemet mitzva (obligatory war). Accordingly, when there is a tactical advantage, one may initiate an attack even on Shabbat, as our ancestors did in the days of Yehoshua when they conquered Yeriĥo on Shabbat (y. Shabbat 1:8; Tur, OĤ 249:1).
In Meshiv Milĥama 1:2, R. Goren analyzes the Gemara’s dictum, “‘until it has been reduced’ means even on Shabbat.” He extrapolates that permission to violate Shabbat for war is even more sweeping than permission to violate Shabbat to save a life. Specifically, a milĥemet mitzva completely suspends (hutra) Shabbat, whereas saving a life only supersedes (doĥeh) Shabbat. When Rambam limits starting a siege to the beginning of the week, he is referring to local battles, but an all-out war has no Shabbat limitations. It is as if Shabbat has been canceled entirely. However, many view the legitimacy of fighting on Shabbat as an expanded form of saving a life, since the soldiers are engaged in saving many lives. Others, based on MT 30:13, maintain that even for a milĥemet mitzva, we do not begin a siege at the end of the week. This is the opinion of Radbaz 4:77. Tur, OĤ 249, states that it is a milĥemet reshut that we do not start in the days preceding Shabbat, but we may start a milĥemet mitzva even on Shabbat itself. The entire disagreement is limited to a case in which there is no tactical advantage to beginning the war on Shabbat; but if starting the war on Shabbat is likely to save lives, then all agree that we may start the war on Shabbat. See Ha-tzava Ka-halakha 25:13, pp. 250-251.