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