Think you might have to work with a copy of the mbz file in another directory ... like 'test'. In 'test' extract the backup file ... it's a gzipped archive. After extraction in 'test' check out the .xml files for users ... the context mappings *might be* other names the course had for teacher, student, etc. Or the context mappings might have to do with some other area in the original course where teacher of that course could have set such things.
Can't tell you specifics ... but you might need to edit the xml's related, then re-pack the .mbz file.
You did say 'sometimes' ... so we could be looking at the tip of the iceburg here.
Also, check your apache error logs to see if there is any hint given about the restore.
Also, check moodledata/temp/backup/ for a directory that has a long name consisting of letters and numbers. That directory was being used to restore the course. Since it failed, the directory might still exist and hasn't been cleaned up by task/cron. Can't use it to restore again, but it's already un-compressed and using a shell one could with an editor like nano inspect the .xml files there ... looking for any clues about 'context mappings'.
'spirit of sharing', Ken