It may surprise you to know that, like you, bacteria can also get “sick.” Specifically, they can be infected by some viruses. Since this has been happening for billions of years, bacteria have evolved some defence systems (similar to your own immune system) that help them fight the infection before anything serious can happen. Since bacterial cells are much more simple than a human being, their defence system is also a little less complex, and tends to mostly involve cutting viral DNA into pieces. As a result, viral DNA can’t be transcribed into usable mRNA molecules, and thus can’t be used to make functional proteins. Chopped up viral DNA also can’t be replicated by the host cell’s enzymes. In this way the viral invasion is stopped.
Scientists have known about one such system for many years – it’s known as the Restriction-Modification System. The bacteria that use the Restriction-Modification System, attach a small molecule called a “methyl group” to very specific sequences of their own DNA, and then produce enzymes that recognize and cut DNA at those same sequences, but only on DNA which does not have the same “methylation” pattern. In this way, foreign DNA (which does not have the right methylation pattern) can be immediately recognized and destroyed.
More recently, scientists discovered a different system which they called CRISPR/CAS9. This system is a little different from the Restriction System, but it accomplishes the same thing – it cuts foreign viral DNA so it can’t be used to replicate the virus.