Again ... me no do Windows. Appears no Windows experts wanna jump in here. Shame! Shame!
Anyhoo ...
Gateway time out error is related to your DB server.
This might help:
https://stackoverflow.com/questions/45710999/504-gateway-timeout-from-an-iis-server
Differences in a math course from a larger language course - obviously modules used in the course.
So visit the math course via GUI and see what's there? How many quizzes? Use any special addons ... like WIRIS?
See: https://docs.moodle.org/400/en/Mathematics
So the math course fails ... what's left in moodledata/temp/backup/builddiretory/ ?
Since you say you can backup the course from cli ... send that backup to a test directory outside of moodledata. Uncompress the .mbz file in that test directory. That, then, is everything moodle will attempt to restore. Should see a moodle_backup.xml file there which is like a road map for the entire course. Inpecting that might provide a clue as well as other .xml files with their related directories.
'SoS', Ken