I have a bad back from an injury, and yes sometimes riding makes it hurt. There are tricks though.
With a MTB, get a bike stem that rises up (so you aren't bent over). You don't need to be aero to exercise. Ride with no hands, sitting up properly. Things like that make a huge difference.
I don't use my road bike, kills my back. I like to load up all the tools for almost any repair, 3 liters of water, survival knife, hatchet, (firearm, depending on where I am) everything and climb hills on my mountain bike. That way I'm a total combined weight of nearly 300 lbs going up a 6% grade. It's steep enough to make you work, brutal enough to keep it short and when I get back home my back doesn't hurt at all.
Taking a break from the grind and sitting up, no hands and vaping while riding is good for the back. I can't vape if it's just me alone since I run about 80% FTP. Riding with others (much slower pace), I choo-choo train it.
Tl;Dr; Just ride with no hands and sit up.