Bug 30511: Don't lock up entire database while running koha-dump
[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 The schema only option can be used to compare your existing database schema
43 to the expected Koha structure.
44
45 Usage:
46 $scriptname [--quiet|-q] [--exclude-indexes] instancename1 [instancename2...]
47 $scriptname -h|--help
48
49     --schema-only         Dump only the database schema
50     --exclude-indexes     Include Zebra indexes on the backup
51     --quiet|-q            Make the script avoid printing to STDOUT
52                           (useful for calling from another scripts)
53     --help|-h             Display this help message
54     --without-db-name     Do not include database name
55
56 EOF
57 }
58
59 dump_instance()
60 {
61     local name=$1
62
63     kohaconfig="/etc/koha/sites/$name/koha-conf.xml"
64     date="$(date +%Y-%m-%d)"
65
66     [ "$quiet" = "no" ] && echo "Dumping Koha site $name:"
67
68     # Dump database.
69     mysqlhost="$( xmlstarlet sel -t -v 'yazgfs/config/hostname' $kohaconfig )"
70     mysqldb="$( xmlstarlet sel -t -v 'yazgfs/config/database' $kohaconfig )"
71     mysqluser="$( xmlstarlet sel -t -v 'yazgfs/config/user' $kohaconfig )"
72     mysqlpass="$( xmlstarlet sel -t -v 'yazgfs/config/pass' $kohaconfig )"
73     backupdir="$( xmlstarlet sel -t -v 'yazgfs/config/backupdir' $kohaconfig || true )"
74     [ -z "$backupdir" ] && backupdir="/var/spool/koha/$name"
75     dbdump="$backupdir/$name-$date.sql.gz"
76     dbflag="--databases"
77     [ "$without_db_name" = "yes" ] && dbflag=""
78     if [ "$schema_only" = "yes" ]
79     then
80         schemadump="$backupdir/$name-schema-$date.sql"
81         [ "$quiet" = "no" ] && echo "* schema to $schemadump"
82         mysqldump $dbflag -d --host="$mysqlhost" --single-transaction \
83             --user="$mysqluser" --password="$mysqlpass" "$mysqldb" | sed --expression='s/ AUTO_INCREMENT=[0-9]\+//' > "$schemadump"
84         chown "root:$name-koha" "$schemadump"
85         chmod g+r "$schemadump"
86     else
87         [ "$quiet" = "no" ] && echo "* DB to $dbdump"
88         mysqldump $dbflag --host="$mysqlhost" --single-transaction \
89             --user="$mysqluser" --password="$mysqlpass" "$mysqldb" |
90             gzip > "$dbdump"
91         chown "root:$name-koha" "$dbdump"
92         chmod g+r "$dbdump"
93
94         instancefile="$name.conf"
95
96         # Dump configs, logs, etc.
97         metadump="$backupdir/$name-$date.tar.gz"
98         [ "$quiet" = "no" ] && echo "* configs, logs to $metadump"
99
100         if [ "$exclude_indexes" = "yes" ]; then
101             excludes="--exclude=var/lib/koha/$name/biblios \
102                       --exclude=var/lib/koha/$name/authorities"
103         fi
104
105         tar -czf "$metadump" -C / $excludes \
106             "etc/koha/sites/$name" \
107             "etc/apache2/sites-available/$instancefile" \
108             "etc/apache2/sites-enabled/$instancefile" \
109             "var/lib/koha/$name" \
110             "var/log/koha/$name"
111
112         chown "root:$name-koha" "$metadump"
113         chmod g+r "$metadump"
114
115         [ "$quiet" = "no" ] && echo "Done."
116
117     fi
118 }
119
120 # Default values
121 quiet="no"
122 exclude_indexes="no"
123 without_db_name="no"
124 schema_only="no"
125
126 while [ $# -gt 0 ]; do
127
128     case "$1" in
129         --schema-only)
130             schema_only="yes"
131             shift ;;
132         --exclude-indexes)
133             exclude_indexes="yes"
134             shift ;;
135         --without-db-name)
136             without_db_name="yes"
137             shift ;;
138         -h|--help)
139             usage ; exit 0 ;;
140         -q|--quiet)
141             quiet="yes"
142             shift ;;
143         -*)
144             die "Error: invalid option switch ($1)" ;;
145         *)
146             # We expect the remaining stuff are the instance names
147             break ;;
148     esac
149
150 done
151
152 # Read instance names
153 if [ $# -gt 0 ]; then
154     # We have at least one instance name
155     for name in "$@"; do
156
157         if is_instance $name; then
158
159             dump_instance $name
160
161         else
162             if [ "$quiet" = "no" ]; then
163                 die "Error: Invalid instance name $name"
164             else
165                 exit 1
166             fi
167         fi
168
169     done
170 else
171     if [ "$quiet" = "no" ]; then
172         die "Error: you must provide at least one instance name"
173     else
174         exit 1
175     fi
176 fi
177
178 exit 0