Release new version 0.14.0.
[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 # mail address where reports should be sent to
26 if [ -z "$STORAGE_ADMIN" ] ; then
27   echo "Error: \$STORAGE_ADMIN is not set. Exiting." >&2
28   exit 2
29 fi
30
31 if ! cd "$MIRROR_DIRECTORY" ; then
32   echo "Error: could not change directory to $MIRROR_DIRECTORY" >&2
33   exit 3
34 fi
35
36 # we want to always keep a few images, no matter how old they are
37 # but get rid of the oldest ones first of course :)
38 # so: how many images do we want to keep? DAYS=3 usually means 'keep 4 images'
39 DAYS=3
40
41 REMOVE_ME=""
42 for flavour in grml-medium_squeeze   grml-medium_wheezy   grml-medium_sid   grml-small_squeeze   grml-small_wheezy  grml-small_sid \
43                grml64-medium_squeeze grml64-medium_wheezy grml64-medium_sid grml64-small_squeeze grml64-small_wheezy grml64-small_sid \
44                grml64_squeeze grml64_wheezy grml64_sid grml_squeeze grml_wheezy grml_sid ; do
45   FILE_COUNT=$(ls -1 $flavour/$flavour*.iso | wc -l)
46   if [ "$FILE_COUNT" -gt "$DAYS" ] ; then
47      FILES=$(ls -1 $flavour/$flavour*.iso | tail -"$DAYS")
48      OLD_FILES=$(ls $flavour/$flavour*.iso | grep -v "$FILES")
49      for file in $OLD_FILES ; do
50          REMOVE_ME="$REMOVE_ME $(find $file -mtime +$DAYS)"
51      done
52   fi
53 done
54
55 [ -d .archive ] || mkdir .archive
56
57 for file in $REMOVE_ME ; do
58     test -f ${file}     && rm -f $file
59     # ... but keep their md5sum / sha1sum:
60     test -f ${file}.md5  && mv ${file}.md5  .archive
61     test -f ${file}.sha1 && mv ${file}.sha1 .archive
62 done
63
64 # inform on successful removal:
65 if [ "$(echo "$REMOVE_ME" | tr -d ' ' )" != "" ] ; then
66    echo "deleted files $REMOVE_ME" | mail -s "daily-builds cleanup script" "$STORAGE_ADMIN"
67 fi
68
69 ## END OF FILE #################################################################