Bug 9988 - DBRev 16.12.00.023
[koha.git] / debian / scripts / koha-run-backups
blob74f879b74422d35eaab8f8cfb586a968ac790e64
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.
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/>.
17 # Daily cron job for koha.
18 # - dump all sites, except one called 'demo'
20 dirname=""
21 days="2"
23 show_help() {
24 cat <<EOH
25 $0 - performs backups of the koha installations on the system
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.
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)
36 Note: backups produced using this tool can be restored using \`koha-restore'.
37 EOH
40 CMD_LINE=`getopt -o h --long days:,output:,help -n 'koha-run-backups' -- "$@"`
42 if [ $? != 0 ] ; then show_help ; exit 1 ; fi
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
58 for name in $(koha-list --enabled | grep -Fxv demo)
60 koha-dump "$name" > /dev/null
61 if [ -z "$dirname" ]; then
62 backupdir="$( xmlstarlet sel -t -v 'yazgfs/config/backupdir' /etc/koha/sites/$name/koha-conf.xml )";
63 else
64 backupdir="$dirname/$name";
67 # Remove old dump files.
68 # FIXME: This could probably be replaced by one line of perl.
69 ls "$backupdir/" |
70 sed "s:^$name-\([0-9-]*\)\.\(sql\|tar\)\.gz$:\1:" |
71 sort -u |
72 tac |
73 sed "1,${days}d" |
74 tac |
75 while read date
77 tardump="$backupdir/$name-$date.tar.gz"
78 sqldump="$backupdir/$name-$date.sql.gz"
79 if [ -e "$tardump" ] && [ -e "$sqldump" ]
80 then
81 rm "$tardump"
82 rm "$sqldump"
83 elif [ -e "$tardump" ] || [ -e "$sqldump" ]
84 then
85 echo "Only one of a pair exists! Not removing it."
86 for x in "$tardump" "$sqldump"
88 if [ -e "$x" ]
89 then
90 echo "Exists : $x"
91 else
92 echo "Does not exist: $x"
94 done
96 done
97 done