The peculiar low mass X-ray binary XSS J12270-4859, associated with the Fermi/LAT source 2FGL J1227.7-4853, was in a X-ray, gamma-ray and optical low-luminosity persistent state for about a decade until the end of 2012, when it has entered into the dimmest state ever observed. The nature of the compact object has been controversial until the detection of a 1.69ms radio pulsar early 2014. We present optical spectroscopy and optical/near-IR photometry during the previous brighter and in the recent faint states. We determine the first spectroscopic orbital ephemeris and an accurate orbital period of 6.91246(5) h. We infer a mid G-type donor star and a distance d= 1.8-2.0 kpc. The donor spectral type changes from G5 V to F5 V between inferior and superior conjunction, a signature of strong irradiation effects. We infer a binary inclination 45 o i 65 o and a highly undermassive donor, M 2 ∼ 0.06 − 0.12 M ⊙ for a neutron star mass in the range 1.4-3 M ⊙ . Thus this binary joins as the seventh member the group of "redbacks". In the high state, the emission lines reveal the presence of an accretion disc. They tend to vanish at the donor star superior conjunction, where also flares are preferentially observed together with the occurrence of random dips. This behaviour could be related to the propeller mechanism of the neutron star recently proposed to be acting in this system during the high state. In the low state, the emission lines are absent at all orbital phases indicating that accretion has completely switched-off and that XSS J12270-4859 has transited from an accretion-powered to a rotation-powered phase.