Answer the question
In order to leave comments, you need to log in
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}"
Answer the question
In order to leave comments, you need to log in
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
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
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). 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.
Thanks to all who signed up.
An interesting option about the number of files - how is it implemented?
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 ))/"
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question