Bug 36009: Document koha-worker --queue elastic_index
[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     --uploaded_files      Include uploaded files.
52     --uploaded_temp_files Include temporary uploaded files.
53     --quiet|-q            Make the script avoid printing to STDOUT
54                           (useful for calling from another scripts)
55     --help|-h             Display this help message
56     --without-db-name     Do not include database name
57
58 EOF
59 }
60
61 dump_instance()
62 {
63     local name=$1
64
65     kohaconfig="/etc/koha/sites/$name/koha-conf.xml"
66     date="$(date +%Y-%m-%d)"
67
68     [ "$quiet" = "no" ] && echo "Dumping Koha site $name:"
69
70     # Dump database.
71     mysqlhost="$( xmlstarlet sel -t -v 'yazgfs/config/hostname' $kohaconfig )"
72     mysqldb="$( xmlstarlet sel -t -v 'yazgfs/config/database' $kohaconfig )"
73     mysqluser="$( xmlstarlet sel -t -v 'yazgfs/config/user' $kohaconfig )"
74     mysqlpass="$( xmlstarlet sel -t -v 'yazgfs/config/pass' $kohaconfig )"
75     backupdir="$( xmlstarlet sel -t -v 'yazgfs/config/backupdir' $kohaconfig || true )"
76     [ -z "$backupdir" ] && backupdir="/var/spool/koha/$name"
77     dbdump="$backupdir/$name-$date.sql.gz"
78     dbflag="--databases"
79     [ "$without_db_name" = "yes" ] && dbflag=""
80     if [ "$schema_only" = "yes" ]
81     then
82         schemadump="$backupdir/$name-schema-$date.sql"
83         [ "$quiet" = "no" ] && echo "* schema to $schemadump"
84         mysqldump $dbflag -d --host="$mysqlhost" --single-transaction \
85             --user="$mysqluser" --password="$mysqlpass" "$mysqldb" | sed --expression='s/ AUTO_INCREMENT=[0-9]\+//' > "$schemadump"
86         chown "root:$name-koha" "$schemadump"
87         chmod g+r "$schemadump"
88     else
89         [ "$quiet" = "no" ] && echo "* DB to $dbdump"
90         mysqldump $dbflag --host="$mysqlhost" --single-transaction \
91             --user="$mysqluser" --password="$mysqlpass" "$mysqldb" |
92             gzip > "$dbdump"
93         chown "root:$name-koha" "$dbdump"
94         chmod g+r "$dbdump"
95
96         instancefile="$name.conf"
97
98         # Dump configs, logs, etc.
99         metadump="$backupdir/$name-$date.tar.gz"
100         output="* configs, logs"
101
102         if [ "$exclude_indexes" = "yes" ]; then
103             excludes="--exclude=var/lib/koha/$name/biblios \
104                   --exclude=var/lib/koha/$name/authorities"
105         fi
106
107         if [ "$uploaded_files" = "yes" ]; then
108             # Remove leading /
109             uploaded_files_dir=$(echo $(get_upload_path $name) | cut -c 2-)
110             output="$output, uploaded files"
111         fi
112
113         if [ "$uploaded_temp_files" = "yes" ]; then
114             # Remove leading /
115             tempdir=$(echo $(get_tmp_path $name) | cut -c 2-)
116             uploaded_temp_files_dir="$tempdir/koha_${name}_upload"
117             if ! [ -d /$uploaded_temp_files_dir ]; then
118                 mkdir /$uploaded_temp_files_dir
119             fi
120             output="$output, uploaded temporary files"
121         fi
122
123         output="$output to $metadump"
124         [ "$quiet" = "no" ] && echo "$output"
125
126         tar -czf "$metadump" -C / $excludes \
127             "etc/koha/sites/$name" \
128             "etc/apache2/sites-available/$instancefile" \
129             "etc/apache2/sites-enabled/$instancefile" \
130             "var/lib/koha/$name" \
131             "var/log/koha/$name" \
132             $uploaded_files_dir \
133             $uploaded_temp_files_dir
134
135         chown "root:$name-koha" "$metadump"
136         chmod g+r "$metadump"
137
138         [ "$quiet" = "no" ] && echo "Done."
139     fi
140 }
141
142 # Default values
143 quiet="no"
144 exclude_indexes="no"
145 without_db_name="no"
146 schema_only="no"
147
148 while [ $# -gt 0 ]; do
149
150     case "$1" in
151         --schema-only)
152             schema_only="yes"
153             shift ;;
154         --exclude-indexes)
155             exclude_indexes="yes"
156             shift ;;
157         --without-db-name)
158             without_db_name="yes"
159             shift ;;
160        --uploaded_files)
161             uploaded_files="yes"
162             shift ;;
163         --uploaded_temp_files)
164             uploaded_temp_files="yes"
165             shift ;;
166         -h|--help)
167             usage ; exit 0 ;;
168         -q|--quiet)
169             quiet="yes"
170             shift ;;
171         -*)
172             die "Error: invalid option switch ($1)" ;;
173         *)
174             # We expect the remaining stuff are the instance names
175             break ;;
176     esac
177
178 done
179
180 # Read instance names
181 if [ $# -gt 0 ]; then
182     # We have at least one instance name
183     for name in "$@"; do
184
185         if is_instance $name; then
186
187             dump_instance $name
188
189         else
190             if [ "$quiet" = "no" ]; then
191                 die "Error: Invalid instance name $name"
192             else
193                 exit 1
194             fi
195         fi
196
197     done
198 else
199     if [ "$quiet" = "no" ]; then
200         die "Error: you must provide at least one instance name"
201     else
202         exit 1
203     fi
204 fi
205
206 exit 0