How to backup and restore /opt with Time Machine on macOS High Sierra?
How can I get Time Machine to backup /opt on High Sierra?

You don't need to do anything special to back it up: Time Machine backs up all folders on your Mac.
However, you may get the impression that /opt (and other folders like /bin or  /cores, as you mention in your comment) are not included because the Finder hides those folders by default:

But don't let the Finder mislead you:

either press ShiftCommand. to let the Finder display all folders (press ShiftCommand. again to hide them),
or open Terminal, list the contents of the backup folder and you will see that everything is there:



How can I restore /opt?

Proceed as follows:

Open a Finder window.
Enter Time Machine.
Press ShiftCommandG and enter / to display the root folder of your drive.
Press ShiftCommand. to display hidden folders. You should see /opt now.
Browse the timeline on the right-hand side and select the desired date.
Select /opt.
Press Restore. Note that Time Machine will probably ask whether you want to keep a copy of your current files.