I suspect that there are 2 possible problems here (one solvable, the other not).
If your backup does not include the activity that is being referred to by the cmid, then it will not be possible to recreate the mapping on restore (as the backup & restore process only knows about mappings for activities
that it handles in the current run).
If your backup does include the correct activity, then it will be possible to handle the mapping, but the activity in question may not have been restored at the point when the block is being restored, so you'll have to handle it at the end of the restore process.
As you restore each block, either a) store the id + cmid in a static array OR b) set the cmid to the the negative of the previous value (e.g. id "15" becomes id "-15") during the block restore.
Then in the after_course_restore() function (I think that's the right name, I haven't checked the code), either a) loop through the static array, look up the mapping for each cmid, then write it into the record referenced by the id OR b) find all negative cmid values in the table, negate them again (e.g. "-15" converts back to "15"), look up the mapping and write them into the database
In both cases, you still need to handle the case when there is no mapping possible (e.g. the backup and restore excluded the relevant activity, or it was referencing an activity from a different course, or the activity failed to restore, because the relevant activity plugin wasn't available on the destination site).