Linux script - help!!

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

Good luck

Thank you very much for the helpful syntax. But can you attend to point number 2 as well :slight_smile:

---------- Post updated at 10:42 PM ---------- Previous update was at 10:41 PM ----------

It will be helpful to find and Tar in the same step

you can use something like:

find /var -type f -name "file_201201*mtd" -mtime +365 2>/dev/null | tar cvfj file_201201.tar.bz2 {} +

Thanks very much again, but can you explain it quickly :slight_smile:

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

Thanks, will try this and let you know
I'm using Fedora btw.

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

old_files.txt is containing the following:

/var/lib/mysql/files/files_2012010918.MYD
/var/lib/mysql/files/files_2012010600.MYI
/var/lib/mysql/files/files_2012010406.frm
/var/lib/mysql/files/files_2012010712.frm
/var/lib/mysql/files/files_2012010900.MYD
/var/lib/mysql/files/files_2012020912.MYI
/var/lib/mysql/files/files_2012020400.frm
/var/lib/mysql/files/files_2012020706.MYD
/var/lib/mysql/files/files_2012020800.MYI

What I want here is to compress every month alone ex: files_201201.tar.bz2 and cdrs_201202.tar.bz2 in the required destination.

Your input is highly appreciated!

---------- Post updated at 04:22 PM ---------- Previous update was at 12:55 AM ----------

Any update?