Evolution and maintenance of microbe-mediated protection under occasional pathogen attack