Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have some folder with files with the names as "example_YY_MM_DD-HH.zip" and I have 3 backups per day. I need this:

1) For backups older than 1 week I need only one per day (at midnight '00' at HH)

2) For backups older than month I need only one backup per week.

3) For backups older than one year I need only one backup per month (first day of month)

I have only this and I dont know what put to IF part.

Thanks

#!/bin/sh
export NOW=$(date +"%d-%m-%Y")
export OUTPUT=/media/backup/logs/delete-old.txt
export BACKUP_DIR=

for file in $BACKUP_DIR
do
if $file

fi
done
share|improve this question

2 Answers 2

up vote 1 down vote accepted

This should work (if you set the correct full or relative path it is currently on the directory test)

#! /bin/bash

nowday=$(date +"%d")
nowmonth=$(date +"%m")
nowyear=$(date +"%y")

#number of days since 1 Jan 1970 (today)
nowdays=$(($(date --date="20$nowyear-$nowmonth-$nowday" +"%s")/86400))

backup_dir="test/*"
#echo $backup_dir
for file in $backup_dir
do
  hour=${file: -6: -4}
  day=${file: -9: -7}
  month=${file: -12: -10}
  year=${file: -15: -13}
  #number of days since 1 Jan 1970 (file)
  days=$(($(date --date="20$year-$month-$day" +"%s")/86400))
  if ((days < nowdays-365)); then
    # more than one year
    if ((10#$day == 1))&&((10#$hour == 0)); then
      #day is 1 hour is 0 (we keep) use this space if you want to copy or something!
      :
    else
      #Wrong day or hour
      rm $file
    fi
  else if ((days < nowdays-31)); then
    # more than one month (31 days)
    if (((10#$day == 1))||((10#$day == 8))||((10#$day == 15))||((10#$day == 22))||((10#$day == 29)))&&((10#$hour == 0)); then
      #day is 1,8,15,22,29 hour is 0 (we keep) use this space if you want to copy or something!
      :
    else
      #Wrong day or hour
      rm $file
    fi
  else if ((days < nowdays-7)); then
    # more than one week
    if ((10#$hour == 0)); then
      #hour is zero se this space if you want to copy or something!
      :
    else
      #Wrong hour
      rm $file
    fi
  else
    # less than one week (we keep) use this space if you want to copy or something!
    :
  fi fi fi


done

I have defined a month as 31 days always for checking if over 1 month. For once a week I pick days 1 8 15 22 and 29, so there is always the first day of the month. This also works on the back catalogue because it checks for midnight for each file.

The : lines are just place holders in case you want to put code there.

Give it a check before running it on everything, just in case there is an error!

share|improve this answer
    
Thanks, I only edit variable definiton of hour,day,... and backup_dir and it is working very good. :-) Hour example as this (with minus not working) 'hour=${file: 27: 2}' –  zorbon.cz Dec 28 '13 at 19:24

It looks like these are some methods to accomplish what you are looking for.

bash script to find old files based off date in file name

share|improve this answer
    
You should post the relevant info in your answer then link to the source you used, if for some reason the linked answer was deleted, you answer would be useless. (it is also likely to result in more upvotes if you have all the info in your answer) –  TrailRider Dec 28 '13 at 18:36
    
@TheHerk Yes, I found it, but I not fully understand it. :-) I used Bash many times, but not in this. I have this in PowerShell and that have only half of lines than Bash in example link. Can you more explane this? Or what from it I need? Thanks –  zorbon.cz Dec 28 '13 at 18:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.