Bug 29881: libdbd-sqlite2-perl is unavailable on deb12 (koha-common wont install)
[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=""
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 /path     The directory that the resulting files will be placed into.
32                        (default: /var/spool/koha)
33     --days 2           The number of days to keep backups around for
34                        (default: 2)
35     --exclude-indexes  Exclude Zebra indexes from the backups (default: false)
36
37 Note: backups produced using this tool can be restored using \`koha-restore'.
38 EOH
39 }
40
41 exclude_indexes=""
42
43 CMD_LINE=`getopt -o h --long days:,output:,help,exclude-indexes -n 'koha-run-backups' -- "$@"`
44
45 if [ $? != 0 ] ; then show_help ; exit 1 ; fi
46
47 eval set -- "$CMD_LINE"
48 while true ; do
49     case "$1" in
50         -h|--help)
51             show_help; exit;;
52         --days)
53             days=$2; shift 2 ;;
54         --output)
55             dirname=$2; shift 2 ;;
56         --exclude-indexes)
57             exclude_indexes='--exclude-indexes'; shift ;;
58         --) shift ; break ;;
59         *) echo "Unknown error parsing the command line!" ; exit 1 ;;
60     esac
61 done
62
63 for name in $(koha-list --enabled | grep -Fxv demo)
64 do
65     koha-dump ${exclude_indexes} "$name" > /dev/null
66     if [ -z "$dirname" ]; then
67         backupdir="$( xmlstarlet sel -t -v 'yazgfs/config/backupdir' /etc/koha/sites/$name/koha-conf.xml )";
68     else
69         backupdir="$dirname/$name";
70     fi
71
72     # Remove old dump files.
73     # FIXME: This could probably be replaced by one line of perl.
74     ls "$backupdir/" |
75     sed "s:^$name-\([0-9-]*\)\.\(sql\|tar\)\.gz$:\1:" |
76     sort -u |
77     tac |
78     sed "1,${days}d" |
79     tac |
80     while read date
81     do
82         tardump="$backupdir/$name-$date.tar.gz"
83         sqldump="$backupdir/$name-$date.sql.gz"
84         if [ -e "$tardump" ] && [ -e "$sqldump" ]
85         then
86             rm "$tardump"
87             rm "$sqldump"
88         elif [ -e "$tardump" ] || [ -e "$sqldump" ]
89         then
90             echo "Only one of a pair exists! Not removing it."
91             for x in "$tardump" "$sqldump"
92             do
93                 if [ -e "$x" ]
94                 then
95                     echo "Exists        : $x"
96                 else
97                     echo "Does not exist: $x"
98                 fi
99             done
100         fi
101     done
102 done
103