Notice that viral DNA is not the only foreign DNA that a bacterial can meet during its bacterial adventures. Plasmids, conjugated DNA and DNA inserted by transformation exist, and it may confer ecological advantages. Because of that, even if the enzymes would recognize only non genomic DNA and destroy it effectively, it's still more useful to have some criteria and allow to stay some alien DNA, while destroying some other.
Restriction sites are pretty small, so the chances of getting one target in a viral genome is quite big. Bacterial DNA is protected by methylation, which is recognized by this enzymes and prevents them to break the DNA.
Restriction enzymes are not the only means bacteria have in order to defend from foreign DNA. Bacteria usually carry exonucleases that recognize single stranded DNA and linear DNA, which is common in viral genomes, but not in non-infected bacterial cells. This enzyme lacks the specificity that restriction systems possess, and can work combined (for instance, a restriction cleavage may form linear DNA, which will be detected by exonucleases as potentially harmful DNA).
In addition to this, bacteria and archaeons have a special antiviral defense, named CRISPR-CAS system, that acts like an immune system. CRISPR-CAS inserts in a special region of the bacterial genome viral DNA fragments from previous infection. When a new virus infects the cell, those viral fragments are expressed as a special kind of RNA (CRISPR-RNA) which, if the virus has already infected this cell, will attach to viral DNA and will serve as a marker. Then, the RNA-marked DNA is recognized by special nucleases. This defense system allows to obtain acquired resistance that (unlike vertebrate immune system) is also inheritable.
Concluding, not all foreign DNA is harmful, since plasmids and other could provide some advantage to the cell. Restriction-modification systems aren't the only defense system that the bacteria has against foreign DNA, but all of them must be able to differentiate between desirable and harmful DNA. The combination of many different defense systems allow to patch the limitations of them individually, and so different species will invest differently in one or other, depending of the ecological pressures they have to face.