How to calculate rotation for Mesh2 based on identical Mesh1 and its local vertices data?

I have two identical meshes. Their rotation is already applied in Blender (0,0,0). I would like to restore rotation for mesh2 based on mesh1 as a reference object.

I have a python script that finds the identical meshes (with the identical geometry data) and converts them to instances. Right now after linking mesh data, I lose the rotation.

The purpose of this is to align all meshes to their correct rotation after being converted to instances.

Thank you!

if the objects are identical, just set their transform matrices equal…
object1.matrix_world = object2.matrix_world