A variational principle for gradient flows in metric spaces