We deal with the geometrical inverse problem of the shape reconstruction of cavities in a bounded linear isotropic medium by means of boundary data. The problem is addressed from the point of view of optimal control: the goal is to minimize in the class of Lipschitz domains a Kohn-Vogelius type functional with a perimeter regularization term which penalizes the perimeter of the cavity to be reconstructed. To solve numerically the optimization problem, we use a phase-field approach, approximating the perimeter functional with a Modica-Mortola relaxation and modeling the cavity as an inclusion with a very small elastic tensor. We provide a detailed analysis showing the robustness of the algorithm through some numerical experiments.