If an enemy raids your settlement and destroys at least 40% of it, you are provided with a 12-hour period wherein you can rebuild. Make the most of it. Don't attack anyone during that time, as you'll lose your shields. Instead, make any defensive upgrades to towers, so that they'll be stronger and ready to work on your behalf once the shields are gone.