S
S
smartlight2012-07-11 09:49:48
linux
smartlight, 2012-07-11 09:49:48

How to delete unnecessary backups?

Good morning omniscient All.
There is a server on Linux Debian.
A backup is being made. the folder structure is like this:

BACKUP_DIR="${BACKUP_ROOTDIR}/mysql/${YEAR}/${MONTH}/${DAY}"


But I just can't figure out how to delete unnecessary backups, for example, older than a month or two.

How to implement it correctly?
Thanks to all.

Answer the question

In order to leave comments, you need to log in

10 answer(s)
A
Alukardd, 2012-07-11
@Alukardd

If it is stupidly older than a certain period, then we run something like this through cron:

find ${BACKUP_ROOTDIR} -type f -mtime +30 -delete

Will delete all file older than 30 days.

B
bugman, 2012-07-11
@bugman

Get a list of files - ls, find
Sort them - sort
Get all lines except the last N - head, tail
Generate a delete command - sed, xargs

A
Alukardd, 2012-07-11
@Alukardd

As promised, I threw a small script ...

#!/bin/bash

##################################
path=/tmp/testdir/2012
age=20
lcount=5
##################################

old="/tmp/oldest"
new="/tmp/newest"

find $path -type f -mtime +$age -printf '%[email protected] %p\n' | sort -n > $old
find $path -type f -mtime -$age > $new

count=$(wc -l $new | cut -d ' ' -f 1)
countold=$(wc -l $old | cut -d ' ' -f 1)
if ; then
  if ; then
    while read line; do
      rm -rf $line
    done <<< $(cat $old | cut -d ' ' -f 2)
  else
    let need=$lcount-$count
    if ; then
      need=$countold
    fi
    if ; then
      echo qq
      exit 1
    fi
    tail -n $need $old >> $new
    sed -i 1,${need}d $old
    while read line; do
      rm -rf $line
    done <<< $(cat $old | cut -d ' ' -f 2)
  fi
fi

rm -f $new $old
The bottom line is that he will leave everything that fits the date, even though there will be 200 backups. If the date fits less than $lcount (I wrote 5 for example), then it will take the missing ones from those that are older than the specified date (it should take the dates closest to the desired one, if I made a mistake, then it’s not difficult to change - there tail or head, the list is already sorted).
Separated parameters can be taken as arguments, I did not bother.

N
Nikolai Turnaviotov, 2012-07-11
@foxmuldercp

In my publication about the processing of video surveillance archives using shell tools, there is a bunch of scripts, you can also see how it was done there, this is how archives of camera recordings are cleaned for me.

S
smartlight, 2012-07-11
@smartlight

Thanks to all who signed up.
An interesting option about the number of files - how is it implemented?

@
@sledopit, 2012-07-11
_

What a cool directory structure. It is possible once a month to bang a directory for the month before last. Then we can assume that you need to bang only one directory with a month at a time, and it is not at all difficult to determine it:

[ $(date +%m) -gt 2 ] && \
echo rm -r "${BACKUP_ROOTDIR}/mysql/$(date +%Y)/$(( $(date +%m) - 2 ))/"  || \
echo rm -r "${BACKUP_ROOTDIR}/mysql/$(( $(date +%Y) - 1 ))/$(( $(date +%m) ++ 10 ))/"

R
Riateche, 2012-07-12
@Riateche

You can use the ready script AutoMySQLBackup .

N
Nikolai Turnaviotov, 2012-08-06
@foxmuldercp

In my publications there is an article on processing video archives, one of the scripts just does what you need.
I'll be glad if it's useful

B
Barmunk, 2013-09-29
@Barmunk

and you can do the same, but on a remote ftp, on which backups are poured?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question