I need to create a bash script to run through crontab, to do the below job:
1- find files that are over 12 months old in /var "files are like file_201201222.mtd file_201201333.mtd
2- Tar them in /backup like: file_201201.tar.bz2 "every month in one tar file"
3- remove them after that
4- Send an email with the files deleted
Your help is highly appreciated!
Thanks in Advance
you can find files older than 12 months with this command: find /var -name "*.mtd" -ctime +360
to remove them after it finds them use find ./ -name "*" -ctime +360 -exec rm {} \;
sending an email can be done with the mail -s "Subject" user@tosend.com < /tmp/maildata.txt
ctime will also match files whose attributes have been recently changed, so in this case I would go with mtime instead.
Also, you don't mention your operating system so I'm gonna assume you are using GNU find which have some neat extra features. You could try:
find /var -type f -name "file_*.mtd" -mtime +365 | xargs tar -jcf somefile.tar.bz2 --remove-files && mailx -s "Some Stuff" user@domain.com
Keep in mind that the above command will only match files that are actually older than 12 months, regardless of their name (as long as they match the model "file_*.mtd" ).
That however doesn't take care of the monthly tar, so here's a better (untested) approach. You may need to change the date format but that is left as an exercise for you.
for month in $(seq -f %02g 1 12); do
find /var -type f -name "file_$(date +%Y)${month}$(date +%d).mtd" -mtime +365 | \
xargs tar -jcf "file_$(date +%Y)${month}.tar.bz2" --remove-files \
&& echo "Some Stuff" | mailx -s "Subject" user@domain.com
done
I've had various issues with trying to use mtime and ctime in the past, so my usual method is to touch a file with a date time stamp of, say, a year ago
touch -t 20130813 ref_file
and then use
find . ! -newer ref_file
to find files that are older than the specific date
Please find attached my script and let's see how we can fix it:
#!/bin/bash
rm /tmp/old_files.txt
# This is script will remove 12+ months old CDRs after being compressed based
# Firstly Introduce our variables
TO=akamel@xyz.com
SOURCE=/var/lib/mysql/files
DEST=/backup/local/files
SUBJECT="Old files backup process on the Server"
# Here we find all the 12+ months old files and put the results in file
find $SOURCE -mtime +580 -name "files_2*" >> /tmp/old_files.txt
# Here we will loop the output file and start our compression
# Here we will remove the old files
# Send an email to the team to ensure that everything is OK
echo "Please find d attached the files need to be removed" | mail -s "$SUBJECT" -a /tmp/old_files.txt akamel@xyz.com