93457ab1c1fef97385819f327f830e80b13eb54a
[grml-live.git] / buildd / cleanup.sh
1 #!/bin/sh
2 # Filename:      cleanup.sh
3 # Purpose:       clean up daily builds directory - remove old files
4 # Authors:       grml-team (grml.org), (c) Michael Prokop <mika@grml.org>
5 # Bug-Reports:   see http://grml.org/bugs/
6 # License:       This file is licensed under the GPL v2 or any later version.
7 ################################################################################
8
9 set -e
10
11 dir="$(dirname $0)"
12
13 if [ -r /etc/grml/grml-buildd.conf ] ; then
14   . /etc/grml/grml-buildd.conf
15 elif [ -r "${dir}/grml-buildd.conf" ] ; then
16   . "${dir}/grml-buildd.conf"
17 fi
18
19 # directory where daily ISOs are stored locally
20 if [ -z "$MIRROR_DIRECTORY" ] ; then
21   echo "Error: \$MIRROR_DIRECTORY is not set. Exiting." >&2
22   exit 1
23 fi
24
25 if [ -z "$FLAVOURS" ] ; then
26   echo "Error: \$FLAVOURS is not set. Exiting." >&2
27   exit 2
28 fi
29
30 if ! cd "$MIRROR_DIRECTORY" ; then
31   echo "Error: could not change directory to $MIRROR_DIRECTORY" >&2
32   exit 3
33 fi
34
35 # we want to always keep a few images, no matter how old they are
36 # but get rid of the oldest ones first of course :)
37 # so: how many images do we want to keep? DAYS=3 usually means 'keep 4 images'
38 DAYS=3
39
40 REMOVE_ME=""
41 for flavour in $FLAVOURS; do
42   FILE_COUNT=$(ls -1 $flavour/$flavour*.iso | wc -l)
43   if [ "$FILE_COUNT" -gt "$DAYS" ] ; then
44      FILES=$(ls -1 $flavour/$flavour*.iso | tail -"$DAYS")
45      OLD_FILES=$(ls $flavour/$flavour*.iso | grep -v "$FILES")
46      for file in $OLD_FILES ; do
47          REMOVE_ME="$REMOVE_ME $(find $file -mtime +$DAYS)"
48      done
49   fi
50 done
51
52 [ -d .archive ] || mkdir .archive
53
54 for file in $REMOVE_ME ; do
55     test -f ${file}     && rm -f $file
56     # ... but keep their md5sum / sha1sum:
57     test -f ${file}.md5  && mv ${file}.md5  .archive
58     test -f ${file}.sha1 && mv ${file}.sha1 .archive
59 done
60
61 # inform on successful removal:
62 if [ "$(echo "$REMOVE_ME" | tr -d ' ' )" != "" ] ; then
63    echo "Removed previous files $REMOVE_ME" | logger -t grml-buildd
64 fi
65
66 ## END OF FILE #################################################################