Am not entirely sure how to do this due to my lack of shell knowledge.

NOW=$(date +"%Y.%m.%d.%T")

tar czf /backups/web_backup_$NOW.tgz /web/

Can you please assist me to delete old backups so that it only keeps:

  1. Last 3 days
  2. One backup each from last 3 weeks
  3. One backup each for last 3 months
link|improve this question

80% accept rate
feedback

2 Answers

up vote 0 down vote accepted

something like this: (just an idea)

mkdir -p monthly
mkdir -p weekly

ln backup_$NOW.tgz weekly/

# find current month
month=$(date +%Y-%m-)
# find the first file of the current month in the weekly folder
first_monthly=$(ls --sort=time -1 weekly/*$month* 2>/dev/null | tail -1)
# and put it in the monthly folder
ln -f $first_monthly monthly/

# we need only 5 weekly backups
ls --sort=time -1 weekly/* 2>/dev/null | tail -n+6 >> /tmp/deletebackups.txt
# we need only 5 monthly backups
ls --sort=time -1 monthly/* 2>/dev/null | tail -n+6 >> /tmp/deletebackups.txt

# delete the extra files
#rm $(cat /tmp/deletebackups.txt) 2>/dev/null
xargs --arg-file /tmp/deletebackups.txt rm
link|improve this answer
You need to do tail -n+6 to keep 5. There's a doubled "=" in the assignment to first_monthly. The last line would be better as: xargs --arg-file /tmp/deletebackups.txt rm. – Dennis Williamson Sep 28 '10 at 15:53
@Dennis sorry but my lack of shell scripting knowledge makes it difficult for me to even make these changes. can you please post the final script. – elated Sep 29 '10 at 7:58
hmm...I've updated my post with Dennis Willimasons corrections, but I think you will be better off using a utility that already does what you want. Maybe sbackup or rsnapshot? – Jure1873 Sep 29 '10 at 13:57
feedback

Look at rsnapshot. It might do what you want out of the box.

link|improve this answer
You definitely want to use rsnapshot. – Phil Hollenback Jan 10 '11 at 6:13
feedback

Your Answer

 
or
required, but never shown

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