Federer survives scare to reach second round