3 # koha-dump: dump all contents and configs for a Koha site
4 # Copyright 2010 Catalyst IT, Ltd
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.
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.
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/>.
22 # include helper functions
23 if [ -f "/usr/share/koha/bin/koha-functions.sh" ]; then
24 . "/usr/share/koha/bin/koha-functions.sh"
26 echo "Error: /usr/share/koha/bin/koha-functions.sh not present." 1>&2
30 # Make sure the files we create are not accessible by anyone else.
35 local scriptname=$(basename $0)
40 This script dumps your Koha instance data for backup or migration.
42 The schema only option can be used to compare your existing database schema
43 to the expected Koha structure.
46 $scriptname [--quiet|-q] [--exclude-indexes] instancename1 [instancename2...]
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
63 kohaconfig="/etc/koha/sites/$name/koha-conf.xml"
64 date="$(date +%Y-%m-%d)"
66 [ "$quiet" = "no" ] && echo "Dumping Koha site $name:"
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 [ "$without_db_name" = "yes" ] && dbflag=""
77 if [ "$schema_only" = "yes" ]
79 schemadump="$backupdir/$name-schema-$date.sql"
80 [ "$quiet" = "no" ] && echo "* schema to $schemadump"
81 mysqldump $dbflag -d --host="$mysqlhost" \
82 --user="$mysqluser" --password="$mysqlpass" "$mysqldb" | sed --expression='s/ AUTO_INCREMENT=[0-9]\+//' > "$schemadump"
83 chown "root:$name-koha" "$schemadump"
84 chmod g+r "$schemadump"
86 [ "$quiet" = "no" ] && echo "* DB to $dbdump"
87 mysqldump $dbflag --host="$mysqlhost" \
88 --user="$mysqluser" --password="$mysqlpass" "$mysqldb" |
90 chown "root:$name-koha" "$dbdump"
93 instancefile="$name.conf"
95 # Dump configs, logs, etc.
96 metadump="$backupdir/$name-$date.tar.gz"
97 [ "$quiet" = "no" ] && echo "* configs, logs to $metadump"
99 if [ "$exclude_indexes" = "yes" ]; then
100 excludes="--exclude=var/lib/koha/$name/biblios \
101 --exclude=var/lib/koha/$name/authorities"
104 tar -czf "$metadump" -C / $excludes \
105 "etc/koha/sites/$name" \
106 "etc/apache2/sites-available/$instancefile" \
107 "etc/apache2/sites-enabled/$instancefile" \
108 "var/lib/koha/$name" \
111 chown "root:$name-koha" "$metadump"
112 chmod g+r "$metadump"
114 [ "$quiet" = "no" ] && echo "Done."
125 while [ $# -gt 0 ]; do
132 exclude_indexes="yes"
135 without_db_name="yes"
143 die "Error: invalid option switch ($1)" ;;
145 # We expect the remaining stuff are the instance names
151 # Read instance names
152 if [ $# -gt 0 ]; then
153 # We have at least one instance name
156 if is_instance $name; then
161 if [ "$quiet" = "no" ]; then
162 die "Error: Invalid instance name $name"
170 if [ "$quiet" = "no" ]; then
171 die "Error: you must provide at least one instance name"