every trip. The was mostly because one of their early missions, the cosmonaut landed in a remote part of Russia with a lot of bears far from where he was expected to land, so he had to fend for himself for a while there before they could rescue him. They brought something similar to a small sawed-off shotgun for a while, and then replaced it with a 9mm pistol (so much for defending against bears, though).
Recently for trips to the ISS, they give the astronauts the choice if they want to bring a gun with them, and none of them do anymore, so its been years since there has been a gun in space.