Orientational ordering is key to functional materials with switching capability, such as nematic liquid crystals and ferromagnetic and ferroelectric materials. We explored the confinement of nematic liquid crystals in bicontinuous porous structures with smooth surfaces that locally impose normal orientational order on the liquid crystal. We find that frustration leads to a high density of topological defect lines permeating the porous structures, and that most defect lines are made stable by looping around solid portions of the confining material. Because many defect trajectories are possible, these systems are highly metastable and efficient in memorizing the alignment forced by external fields. Such memory effects have their origin in the topology of the confining surface and are maximized in a simple periodic bicontinuous cubic structure. We also show that nematic liquid crystals in random porous networks exhibit a disorder-induced slowing-down typical of glasses that originates from activated collisions and rearrangements of defect lines. Our findings offer the possibility to functionalize orientationally ordered materials through topological confinement.