Sunday, September 16, 2012

Shell script to search for text in a file and copy that file

I have a mail server which running Kerio in CentOS 5.X. The total mail backup system is very simple. All the mails that pass in and out is automatically copied to a folder in that days name by Kerio in a folder called archive. So I have all the .eml files of all user' incoming and outgoing mails in a single folder for a particular day. I keep 3 months of those files on the server itself at any given point if time. The older ones I copy to an external USB hard disk. I also copy every day' email to a DVD as a compressed zip file. So I have two copies of all my mails for the last 3 years.

The recovery of mails happen very rarely as per user request and it used to take several days some times. Maximum time was used to search and find the particular user' mail from Terra bytes of mails. So I was searching for a script to search for the user' email address and copy the search result to a folder. I found the script which is as below.

find /path/to/mails  -name '*.eml' -exec grep -l "xyz@example.com" {} \; -exec cp {} /path/to/copy \;

Thanks to Quintet for this suggestion

0 comments: