Bug 22516: Remove remaining calls to lastincrement
[koha.git] / debian / scripts / koha-dump
1 #!/bin/sh
2 #
3 # koha-dump: dump all contents and configs for a Koha site
4 # Copyright 2010  Catalyst IT, Ltd
5
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15
16 # You should have received a copy of the GNU General Public License
17 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19
20 set -e
21
22 # include helper functions
23 if [ -f "/usr/share/koha/bin/koha-functions.sh" ]; then
24     . "/usr/share/koha/bin/koha-functions.sh"
25 else
26     echo "Error: /usr/share/koha/bin/koha-functions.sh not present." 1>&2
27     exit 1
28 fi
29
30 # Make sure the files we create are not accessible by anyone else.
31 umask 0077
32
33 usage()
34 {
35     local scriptname=$(basename $0)
36
37     cat <<EOF
38 $scriptname
39
40 This script dumps your Koha instance data for backup or migration.
41
42 Usage:
43 $scriptname [--quiet|-q] [--include-indexes|-i] instancename1 [instancename2...]
44 $scriptname -h|--help
45
46     --exclude-indexes     Include Zebra indexes on the backup.
47     --quiet|-q            Make the script avoid printing to STDOUT
48                           (useful for calling from another scripts).
49     --help|-h             Display this help message
50
51 EOF
52 }
53
54 dump_instance()
55 {
56     local name=$1
57
58     kohaconfig="/etc/koha/sites/$name/koha-conf.xml"
59     date="$(date +%Y-%m-%d)"
60
61     [ "$quiet" = "no" ] && echo "Dumping Koha site $name:"
62
63     # Dump database.
64     mysqlhost="$( xmlstarlet sel -t -v 'yazgfs/config/hostname' $kohaconfig )"
65     mysqldb="$( xmlstarlet sel -t -v 'yazgfs/config/database' $kohaconfig )"
66     mysqluser="$( xmlstarlet sel -t -v 'yazgfs/config/user' $kohaconfig )"
67     mysqlpass="$( xmlstarlet sel -t -v 'yazgfs/config/pass' $kohaconfig )"
68     backupdir="$( xmlstarlet sel -t -v 'yazgfs/config/backupdir' $kohaconfig || true )"
69     [ -z "$backupdir" ] && backupdir="/var/spool/koha/$name"
70     dbdump="$backupdir/$name-$date.sql.gz"
71     [ "$quiet" = "no" ] && echo "* DB to $dbdump"
72     mysqldump --databases --host="$mysqlhost" \
73         --user="$mysqluser" --password="$mysqlpass" "$mysqldb" |
74         gzip > "$dbdump"
75     chown "root:$name-koha" "$dbdump"
76     chmod g+r "$dbdump"
77
78     instancefile="$name.conf"
79
80     # Dump configs, logs, etc.
81     metadump="$backupdir/$name-$date.tar.gz"
82     [ "$quiet" = "no" ] && echo "* configs, logs to $metadump"
83
84     if [ "$exclude_indexes" = "yes" ]; then
85         excludes="--exclude=var/lib/koha/$name/biblios \
86                   --exclude=var/lib/koha/$name/authorities"
87     fi
88
89     tar -czf "$metadump" -C / $excludes \
90         "etc/koha/sites/$name" \
91         "etc/apache2/sites-available/$instancefile" \
92         "etc/apache2/sites-enabled/$instancefile" \
93         "var/lib/koha/$name" \
94         "var/log/koha/$name"
95
96     [ "$quiet" = "no" ] && echo "Done."
97 }
98
99 # Default values
100 quiet="no"
101 exclude_indexes="no"
102
103 while [ $# -gt 0 ]; do
104
105     case "$1" in
106         --exclude-indexes)
107             exclude_indexes="yes"
108             shift ;;
109         -h|--help)
110             usage ; exit 0 ;;
111         -q|--quiet)
112             quiet="yes"
113             shift ;;
114         -*)
115             die "Error: invalid option switch ($1)" ;;
116         *)
117             # We expect the remaining stuff are the instance names
118             break ;;
119     esac
120
121 done
122
123 # Read instance names
124 if [ $# -gt 0 ]; then
125     # We have at least one instance name
126     for name in "$@"; do
127
128         if is_instance $name; then
129
130             dump_instance $name
131
132         else
133             if [ "$quiet" = "no" ]; then
134                 die "Error: Invalid instance name $name"
135             else
136                 exit 1
137             fi
138         fi
139
140     done
141 else
142     if [ "$quiet" = "no" ]; then
143         die "Error: you must provide at least one instance name"
144     else
145         exit 1
146     fi
147 fi
148
149 exit 0