49ers survive on defense, beat Giants at MetLife