-1

I need to create a bash script that will remove folders containing backups for specific days. I need to KEEP following:

a) files\folders created on FRIDAY @ 23:30 b) files\folders created on the beginning of month (1st of month) c) the most recent two weeks

Any suggestions\starting points?

Input is much appreciated..

Tomek

4 Answers4

1

tmpreaper may help you. You can avoid deleting some files with the argument --protect.

If you name your backup with a pattern like

<year>-<month>-<day>-<day-of-week>@<hour>:<minute>

you could use something like:

tmpreaper [...] --protect 'FRIDAY@23:30' --protect '*-*-01-*' 14d /path/to/backups

see http://manpages.ubuntu.com/manpages/zesty/man8/tmpreaper.8.html

Saïmonn
  • 275
  • 1
  • 8
  • Fantastic! Cheers Saimonn- that makes better use than the find- I'm writing the script now- will post it once it is finalized. – napoleon182 May 16 '17 at 14:07
0

Try to something with find. Like this for the files that has been changed the last 14 days:

find . -mtime -14 -delete

Read the man-page of find to learn more. There is plenty of options to be used. Or for the more specific case you are looking for see this: https://stackoverflow.com/a/158235/42580

UlfR
  • 327
  • 6
  • 11
  • Hi UlfR- I don't think the find command will be suitable as i need to pickup a specific day of the week to keep (Friday)- any other suggestions? – napoleon182 May 16 '17 at 13:47
0

For GNU:

# make a list of files not created on Friday at 23:30 (I would go for 23rd hour)
# also exclude files created on the first of the month

temporary_file=$(mktemp -q /tmp/$0.XXXXXX)
if [ $? -ne 0 ]; then
    echo "$0: Can't create temp file, exiting..."
    exit 1
fi
ls -l --time-style="+%H___%a___%d" |\
    grep -v "23___Fri" |\
    grep -v "___1" |\
    cut -f 1 | sed -e 's,^,./,' | sort \
  > $temporary_file

# Now just older than two weeks; compare with list and exclude
find . -mtime +14 | sort |\
    comm -23 - $temporary_file |\
    tr "\r" "\0" |\
    xargs -0 -n 10 echo

rm $temporary_file

For BSD-style, instead of --time-style="+%H___%a", use -D "+%H___%a" etc.

When you're sure it's correct, replace "echo" after xargs with "rm".

Chris Rees
  • 165
  • 7
0

The other answers are suggesting ways to do this based on the file modification times. That will work, but I'd like to suggest an entirely different way to go about what you are trying to do.

Create the backups in directories named after today's date (for example, a directory named backup-20170516). Then you can easily see which backups were done when, and delete the directories for the backups you want to remove.

This will work even if the file modification timestamps are changed by accident, which can happen if someone uses the touch command, or if files are copied from one system to another without the right options to preserve dates.

It is easy to generate the string backup-yyyymmdd for 14 days ago in bash. Details are in https://stackoverflow.com/questions/13168463/using-date-command-to-get-previous-current-and-next-month

TomOnTime
  • 7,567
  • 6
  • 28
  • 51
  • Thanks, thats interesting suggestion. I think at the moment folders are created based on the number (this is the solution i've inherited btw). I'm still researching on the best approach to this task-> how would you go around removing files\folders? – napoleon182 May 17 '17 at 15:22