CRON Job

Hi all,

Pretty new to CRON but have been messing with Ubuntu for a while now
Here is the situation/problem I have run into you.

I currently run a very basic back procedure by executing a daily CRON job which copies all data from one directory to a USB drive & date stamps the copy:-

cp -r /var/ /media/USB$(date +/%F)

Which has been working prefectly well, however when I checked it last I realised the USB drive had filled up & there was no space to continue the backup.

Is there an easy way to check the date & overwrite the oldest copy, therfore never filling the drive up so the backup will always be successful.

Thanks for looking
2scoops

This will remove the 30days old files and keep the new ones.. In the place of "dot" use the USB path

find . -mtime +30 -exec rm {} \; ; cp -r /var/ /media/USB$(date +/%F)

thanks for posting so promptly

so

find /media/USB$(date +/%F) will locate the folder

-mtime will work out what age the folder is depending on on the argument (+30 equates to 30 days)

-exec is to execute

rm {} will then remove the oldest folder

then it will run the original script again

Is that it in a nutshell :slight_smile: