crystal oscillator as backup (or with sync)
A Phase-Locked Loop will do this.



https://en.wikipedia.org/wiki/Phase-locked_loop

The simplest phase comparator is an XOR gate. This simplest loop filter is an RC filter. The VCO should be a Voltage Controlled Crystal Oscillator (VCXO).

This circuit will cleanly transition between the clocks, no risk of glitches.

The VCXO may break your budget.

You can find PLL design equations online. Surprisingly, the circuit is easy to make stable.