Roger Federer had a tough battle against Roberto Bautista Agut but eventually won out in three sets to reach the quarter-finals of the Shanghai Masters.