Merge remote-tracking branch 'kc/new/enh/bug_6540' into kcmaster
[koha.git] / debian / scripts / koha-run-backups
1 #!/bin/sh
2 # Copyright 2010-2011  Catalyst IT, Ltd
3
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13
14 # You should have received a copy of the GNU General Public License
15 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 # Daily cron job for koha.
18 # - dump all sites, except one called 'demo'
19
20 dirname="/var/spool/koha"
21 days="2"
22
23 show_help() {
24     cat <<EOH
25 $0 - performs backups of the koha installations on the system
26
27 This allows automation of backing up the koha data and configuration to the
28 filesystem. It will keep the past so many backups, discarding older ones.
29
30 Options:
31     --output:   the directory that the resulting files will be placed into.
32                 (default: /var/spool/koha)
33     --days:     the number of days to keep backups around for
34                 (default: 2)
35
36 Note: backups produced using this tool can be restored using \`koha-restore'.
37 EOH
38 }
39
40 CMD_LINE=`getopt -o h --long days:,output:,help -n 'koha-run-backups' -- "$@"`
41
42 if [ $? != 0 ] ; then show_help ; exit 1 ; fi
43
44 eval set -- "$CMD_LINE"
45 while true ; do
46     case "$1" in
47         -h|--help)
48             show_help; exit;;
49         --days)
50             days=$2; shift 2 ;;
51         --output)
52             dirname=$2; shift 2 ;;
53         --) shift ; break ;;
54         *) echo "Unknown error parsing the command line!" ; exit 1 ;;
55     esac
56 done
57
58 for name in $(koha-list --enabled | grep -Fxv demo)
59 do
60     koha-dump "$name" > /dev/null
61
62     # Remove old dump files.
63     # FIXME: This could probably be replaced by one line of perl.
64     ls "$dirname/$name/" | 
65     sed "s:^$name-\([0-9-]*\)\.\(sql\|tar\)\.gz$:\1:" |
66     sort -u |
67     tac |
68     sed "1,${days}d" |
69     tac |
70     while read date
71     do
72         tardump="$dirname/$name/$name-$date.tar.gz"
73         sqldump="$dirname/$name/$name-$date.sql.gz"
74         if [ -e "$tardump" ] && [ -e "$sqldump" ]
75         then
76             rm "$tardump"
77             rm "$sqldump"
78         elif [ -e "$tardump" ] || [ -e "$sqldump" ]
79         then
80             echo "Only one of a pair exists! Not removing it."
81             for x in "$tardump" "$sqldump"
82             do
83                 if [ -e "$x" ]
84                 then
85                     echo "Exists        : $x"
86                 else
87                     echo "Does not exist: $x"
88                 fi
89             done
90         fi
91     done
92 done
93