Probably originally a (probably euphemistic) alteration of Lord defend thee or a similar phrase, but perhaps influenced in form by association with Scots lockanties, perhaps itself an alteration of lack-a-day.