Bug 7216 - koha-restore does not correctly set home
[koha.git] / debian / scripts / koha-restore
1 #!/bin/sh
2 #
3 # koha-restore: restore a Koha site from a dump (from koha-dump)
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
23 die() {
24     echo "$@" 1>&2
25     exit 1
26 }
27
28
29 # Parse command line.
30 [ "$#" = 2 ] || die "Usage: $0 sqldump configdump"
31 sqldump="$1"
32 configdump="$2"
33
34
35 # Verify that no files in the config dump exist on the filesystem.
36 anyexists=no
37 tar -tf "$configdump" |
38 while read x
39 do
40     if [ -e "/$x" ]
41     then
42         anyexists=yes
43         echo "ERROR: File exists: /$x" 1>&2
44     fi
45 done
46 if [ "$anyexists" = yes ]
47 then
48     die "Config dump $configdump has files that exist on the filesystem."
49 fi
50
51
52 # Create user and group.
53 name=$(tar tf "$configdump" | 
54        sed -n '/^etc\/koha\/sites\/\([^/]*\)\/$/s//\1/p')
55 username="$name-koha"
56 adduser --no-create-home --disabled-login --gecos "Koha instance $username" \
57     --home "/var/lib/koha/$name" --quiet "$username"
58
59
60 # Create dirs. Some of them will be in the tarball, but not all, e.g.,
61 # /var/run and /var/lock.
62 koha-create-dirs "$name"
63
64
65 # Unpack tarball.
66 tar -C / -xf "$configdump"
67
68
69 # Re-create database and database user.
70 mysqldb="koha_$name"
71 mysqluser="koha_$name"
72 mysqlpwd="$( xmlstarlet sel -t -v 'yazgfs/config/pass' /etc/koha/sites/$name/koha-conf.xml )"
73 zcat "$sqldump" | mysql --defaults-extra-file=/etc/mysql/koha-common.cnf
74 mysql --defaults-extra-file=/etc/mysql/koha-common.cnf <<eof || true
75 DROP USER '$mysqluser';
76 eof
77 mysql --defaults-extra-file=/etc/mysql/koha-common.cnf << eof || true
78 CREATE USER '$mysqluser' IDENTIFIED BY '$mysqlpwd';
79 GRANT ALL PRIVILEGES ON $mysqldb.* TO '$mysqluser';
80 FLUSH PRIVILEGES;
81 eof
82 koha-rebuild-zebra --full "$name"
83
84
85 # Restart Apache.
86 /etc/init.d/apache2 restart
87