Wikipedia says “a line in the sand” is a metaphor referring to a point beyond which no one can proceed, or an act with consequences that are permanent and irreversible. The article states the origin is unknown.
A suggested Biblical link seems like a stretch to me. (John 8:6 reads: This they said, testing Him, that they might have something of which to accuse Him. But Jesus stooped down and wrote on the ground with His finger, as though He did not hear. [NKJV] I don’t see the current meaning of “a line in the sand” here.)
Wikipedia lists instances of an “actual line being drawn in several historical, or legendary, military events” even if the phrase wasn’t used.
A National Geographic article says “one of the earliest recorded instances of anyone drawing a line in the sand took place in ancient Rome around 168 B.C.” when a Roman envoy “drew a line in the sand around [the opposing king] and told him he had to decide [if he would accede to Roman demands] before he crossed it.”
Word Detective quotes William Safire as writing the phrase originated with a legend from “the siege of the Alamo in 1836, when William Barret Travis drew a line in the sand with his sword and urged those willing to stay and defend the fort to step across it. Unfortunately, this heroic story seems to have been invented by a 19th century promoter long after the fall of the Alamo. But the myth itself probably greatly popularized the phrase, so it does count as an origin of sorts even if the incident itself was apocryphal.”
The official Alamo website says that on Day 12 of the siege “According to one account, Travis draws a line in the sand and asks the garrison to make a decision to stay or leave. Only one man, Moses Rose, chooses to leave.” But the site doesn’t claim this legend as the source of the phrase.
Since education in the 1800s emphasized “the classics” more than we do today, it seems plausible to me that 19th century writers would have known about this tradition. Safire’s unnamed promoter may be the source of the specific phrase, based on ancient Roman history.