Bug 24340: allow koha-sip --disable inst
[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] [--exclude-indexes] 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     --without-db-name     Do not include database name
51
52 EOF
53 }
54
55 dump_instance()
56 {
57     local name=$1
58
59     kohaconfig="/etc/koha/sites/$name/koha-conf.xml"
60     date="$(date +%Y-%m-%d)"
61
62     [ "$quiet" = "no" ] && echo "Dumping Koha site $name:"
63
64     # Dump database.
65     mysqlhost="$( xmlstarlet sel -t -v 'yazgfs/config/hostname' $kohaconfig )"
66     mysqldb="$( xmlstarlet sel -t -v 'yazgfs/config/database' $kohaconfig )"
67     mysqluser="$( xmlstarlet sel -t -v 'yazgfs/config/user' $kohaconfig )"
68     mysqlpass="$( xmlstarlet sel -t -v 'yazgfs/config/pass' $kohaconfig )"
69     backupdir="$( xmlstarlet sel -t -v 'yazgfs/config/backupdir' $kohaconfig || true )"
70     [ -z "$backupdir" ] && backupdir="/var/spool/koha/$name"
71     dbdump="$backupdir/$name-$date.sql.gz"
72     [ "$quiet" = "no" ] && echo "* DB to $dbdump"
73     dbflag="--databases"
74     [ "$without_db_name" = "yes" ] && dbflag=""
75     mysqldump $dbflag --host="$mysqlhost" \
76         --user="$mysqluser" --password="$mysqlpass" "$mysqldb" |
77         gzip > "$dbdump"
78     chown "root:$name-koha" "$dbdump"
79     chmod g+r "$dbdump"
80
81     instancefile="$name.conf"
82
83     # Dump configs, logs, etc.
84     metadump="$backupdir/$name-$date.tar.gz"
85     [ "$quiet" = "no" ] && echo "* configs, logs to $metadump"
86
87     if [ "$exclude_indexes" = "yes" ]; then
88         excludes="--exclude=var/lib/koha/$name/biblios \
89                   --exclude=var/lib/koha/$name/authorities"
90     fi
91
92     tar -czf "$metadump" -C / $excludes \
93         "etc/koha/sites/$name" \
94         "etc/apache2/sites-available/$instancefile" \
95         "etc/apache2/sites-enabled/$instancefile" \
96         "var/lib/koha/$name" \
97         "var/log/koha/$name"
98
99     [ "$quiet" = "no" ] && echo "Done."
100 }
101
102 # Default values
103 quiet="no"
104 exclude_indexes="no"
105 without_db_name="no"
106
107 while [ $# -gt 0 ]; do
108
109     case "$1" in
110         --exclude-indexes)
111             exclude_indexes="yes"
112             shift ;;
113         --without-db-name)
114             without_db_name="yes"
115             shift ;;
116         -h|--help)
117             usage ; exit 0 ;;
118         -q|--quiet)
119             quiet="yes"
120             shift ;;
121         -*)
122             die "Error: invalid option switch ($1)" ;;
123         *)
124             # We expect the remaining stuff are the instance names
125             break ;;
126     esac
127
128 done
129
130 # Read instance names
131 if [ $# -gt 0 ]; then
132     # We have at least one instance name
133     for name in "$@"; do
134
135         if is_instance $name; then
136
137             dump_instance $name
138
139         else
140             if [ "$quiet" = "no" ]; then
141                 die "Error: Invalid instance name $name"
142             else
143                 exit 1
144             fi
145         fi
146
147     done
148 else
149     if [ "$quiet" = "no" ]; then
150         die "Error: you must provide at least one instance name"
151     else
152         exit 1
153     fi
154 fi
155
156 exit 0