koha.upgrade bugfixes. Should work...
[koha.git] / misc / koha.upgrade
1 #!/usr/bin/perl -w
2
3 #use diagnostics;
4 use strict; # please develop with the strict pragma
5 use Install;
6
7 $::language='en';
8
9 if ($<) {
10         print "\n\nYou must run koha.upgrade as root.\n\n";
11         exit;
12 }
13
14 my $input;
15 loadconfigfile();
16
17
18 ($::kohaversion) || ($::kohaversion='unknown version');
19 $::newversion=`cat koha.version`;
20 chomp $::newversion;
21 if ($::newversion =~ /RC/) {
22         releasecandidatewarning();
23 }
24
25 print qq|
26
27 ================
28 = Koha Upgrade =
29 ================
30
31 You are attempting to upgrade from Koha $::kohaversion to $::newversion.
32
33 We recommend that you do a complete backup of all your files before upgrading.
34 This upgrade script will make a backup copy of your files for you.
35
36 Would you like to proceed?  (Y/[N]):
37 |;
38
39 my $answer = <STDIN>;
40 chomp $answer;
41
42 unless ($answer =~/y/i) {
43         print qq|
44
45 Aborting.  Please re-run koha.upgrade when you are ready to upgrade Koha.
46 |;
47         exit;
48 } else {
49         print "Great! continuing upgrade... \n";
50 };
51
52 checkperlmodules();
53
54 my $backupdir='/usr/local/koha/backups';
55 print "Please specify a backup directory [$backupdir]: ";
56
57 $answer = <STDIN>;
58 chomp $answer;
59
60 if ($answer) {
61         $backupdir=$answer;
62 }
63
64 if (! -e $backupdir) {
65         my $result=mkdir ($backupdir, oct(770));
66         if ($result==0) {
67                 my @dirs = split(m#/#, $backupdir);
68                 my $checkdir='';
69                 foreach (@dirs) {
70                         $checkdir.="$_/";
71                         unless (-e "$checkdir") {
72                                 mkdir($checkdir, 0775);
73                         }
74                 }
75         }
76 }
77
78 chmod 0770, $backupdir;
79
80 # Backup MySql database
81 #
82 #
83 my $mysql;
84 my $mysqldir;
85
86 foreach my $mysql (qw(/usr/local/mysql
87                                         /opt/mysql
88                                         /usr
89                         )) {
90         if ( -d $mysql  && -f "$mysql/bin/mysqladmin") {
91                 $mysqldir=$mysql;
92         }
93 }
94 if (!$mysqldir){
95         print "I don't see mysql in the usual places.\n";
96         for (;;) {
97                 print "Where have you installed mysql? ";
98                 chomp($mysqldir = <STDIN>);
99                 last if -f "$mysqldir/bin/mysqladmin";
100         print <<EOP;
101
102 I can't find it there either. If you compiled mysql yourself,
103 please give the value of --prefix when you ran configure.
104
105 The file mysqladmin should be in bin/mysqladmin under the directory that you
106 provide here.
107
108 EOP
109 #'
110         }
111 } else {
112 print "Doing backup\n";
113 }
114
115 my ($sec, $min, $hr, $day, $month, $year) = (localtime(time))[0,1,2,3,4,5];
116 $month++;
117 $year+=1900;
118 my $date= sprintf "%4d-%02d-%02d_%02d:%02d:%02d", $year, $month, $day,$hr,$min,$sec;
119
120 open (MD, "$mysqldir/bin/mysqldump --user=$::user --password=$::pass --host=$::hostname $::database|");
121
122 (open BF, ">$backupdir/Koha.backup_$date") || (die "Error opening up backup file $backupdir/Koha.backup_$date: $!\n");
123
124 my $itemcounter=0;
125 my $bibliocounter=0;
126 my $biblioitemcounter=0;
127 my $membercounter=0;
128
129 while (<MD>) {
130         (/insert into items /i) && ($itemcounter++);
131         (/insert into biblioitems /i) && ($biblioitemcounter++);
132         (/insert into biblio /i) && ($bibliocounter++);
133         (/insert into borrowers /i) && ($membercounter++);
134         print BF $_;
135 }
136
137 close BF;
138 close MD;
139
140 my $filels=`ls -hl $backupdir/Koha.backup_$date`;
141 chomp $filels;
142 printf qq|
143
144 Backed up:
145
146 %6d biblio entries
147 %6d biblioitems entries
148 %6d items entries
149 %6d borrowers
150
151 File Listing
152 ---------------------------------------------------------------------
153 $filels
154 ---------------------------------------------------------------------
155
156 Does this look right? ([Y]/N):
157 |, $bibliocounter, $biblioitemcounter, $itemcounter, $membercounter;
158
159 $answer = <STDIN>;
160 chomp $answer;
161
162 if ($answer=~/^n/i) {
163     print qq|
164
165 Aborting.  The database dump is located in:
166
167         $backupdir/Koha.backup_$date
168
169 |;
170     exit;
171 } else {
172         print "Great! continuing upgrade... \n";
173 };
174
175
176
177 if ($::opacdir && $::intranetdir) {
178     print qq|
179
180 I believe that your old files are located in:
181
182   OPAC:      $::opacdir
183   INTRANET:  $::intranetdir
184
185
186 Does this look right?  ([Y]/N):
187 |;
188     $answer = <STDIN>;
189     chomp $answer;
190
191     if ($answer =~/n/i) {
192         $::intranetdir='';
193         $::opacdir='';
194     } else {
195         print "Great! continuing upgrade... \n";
196     }
197 }
198
199
200 if (!$::opacdir || !$::intranetdir) {
201     $::intranetdir='';
202     $::opacdir='';
203     while (!$::intranetdir) {
204         print "Please specify the location of your INTRANET files: ";
205
206         $answer = <STDIN>;
207         chomp $answer;
208
209         if ($answer) {
210             $::intranetdir=$answer;
211         }
212         if (! -e "$::intranetdir/htdocs") {
213             print "\nCouldn't find the htdocs directory here.  That doesn't look right.\nPlease enter another location.\n\n";
214             $::intranetdir='';
215         }
216     }
217     while (!$::opacdir) {
218         print "Please specify the location of your OPAC files: ";  
219
220         $answer = <STDIN>;
221         chomp $answer;
222
223         if ($answer) {
224             $::opacdir=$answer;
225         }
226         if (! -e "$::opacdir/htdocs") {
227             print "\nCouldn't find the htdocs directory here.  That doesn't look right.\nPlease enter another location.\n\n";
228             $::opacdir='';
229         }
230     }
231 }
232
233
234
235 print "\n\nBacking up old Koha scripts...\n";
236 print     "===============================\n\n";
237
238 mkdir "$backupdir/kohafiles-$date", 0770;
239 mkdir "$backupdir/kohafiles-$date/intranet", 0770;
240 mkdir "$backupdir/kohafiles-$date/opac", 0770;
241
242 my $result=system("cp -R $::intranetdir/* $backupdir/kohafiles-$date/intranet/");
243 if ($result) {
244     print "Error encounted when copying $::intranetdir to $backupdir/kohafiles-$date/intranet/\n";
245     exit;
246 } else {
247     system("rm -rf $::intranetdir/*");
248 }
249 $result=system("cp -R $::opacdir/* $backupdir/kohafiles-$date/opac/");
250 if ($result) {
251     print "Error encounted when copying $::opacdir to $backupdir/kohafiles-$date/opac/\n";
252     exit;
253 } else {
254     system("rm -rf $::opacdir/*");
255 }
256
257    print "Creating $::intranetdir/htdocs...\n";
258    mkdir ("$::intranetdir/htdocs", oct(750));
259    print "Creating $::intranetdir/cgi-bin...\n";
260    mkdir ("$::intranetdir/cgi-bin", oct(750));
261    print "Creating $::intranetdir/modules...\n";
262    mkdir ("$::intranetdir/modules", oct(750));
263    print "Creating $::intranetdir/scripts...\n";
264    mkdir ("$::intranetdir/scripts", oct(750));
265    chmod (oct(770), "$::opacdir");
266    print "Creating $::opacdir/htdocs...\n";
267    mkdir ("$::opacdir/htdocs", oct(750));
268    print "Creating $::opacdir/cgi-bin...\n";
269    mkdir ("$::opacdir/cgi-bin", oct(750));
270
271 my $httpduser;
272 my $realhttpdconf;
273
274 foreach my $httpdconf (qw(/usr/local/apache/conf/httpd.conf
275                       /usr/local/etc/apache/httpd.conf
276                       /usr/local/etc/apache/apache.conf
277                       /var/www/conf/httpd.conf
278                       /etc/apache/conf/httpd.conf
279                       /etc/apache/conf/apache.conf
280                       /etc/apache-ssl/conf/apache.conf
281                       /etc/httpd/conf/httpd.conf
282                       /etc/httpd/conf/commonhttpd.conf
283                       /etc/httpd/httpd.conf)) {
284    if ( -f $httpdconf ) {
285             $realhttpdconf=$httpdconf;
286             open (HTTPDCONF, $httpdconf) or warn "Insufficient privileges to open $httpdconf for reading.\n";
287       while (<HTTPDCONF>) {
288          if (/^\s*User\s+"?([-\w]+)"?\s*$/) {
289             $httpduser = $1;
290          }
291       }
292       close(HTTPDCONF);
293    }
294 }
295
296 unless ($httpduser) {
297     print qq|
298 I was not able to determine the user that Apache is running as.  This
299 information is necessary in order to set the access privileges correctly on
300 /etc/koha.conf.  This user should be set in one of the Apache configuration
301 files using the "User" directive.
302 |;
303     print "What is your Apache user? ";
304     chomp($input = <STDIN>);
305
306     if ($input) {
307         $httpduser = $input;
308     } else {
309         $httpduser='Undetermined';
310     }
311 }
312
313 print "\n\nINSTALLING KOHA...\n";
314 print "\n\n==================\n";
315 print "Copying intranet-html files to $::intranetdir/htdocs...\n";
316 system("cp -R intranet-html/* $::intranetdir/htdocs/");
317 print "Copying intranet-cgi files to $::intranetdir/cgi-bin...\n";
318 system("cp -R intranet-cgi/* $::intranetdir/cgi-bin/");
319 print "Copying script files to $::intranetdir/scripts...\n";
320 system("cp -R scripts/* $::intranetdir/scripts/");
321 print "Copying module files to $::intranetdir/modules...\n";
322 system("cp -R modules/* $::intranetdir/modules/");
323 print "Copying opac-html files to $::opacdir/htdocs...\n";
324 system("cp -R opac-html/* $::opacdir/htdocs/");
325 print "Copying opac-cgi files to $::opacdir/cgi-bin...\n";
326 system("cp -R opac-cgi/* $::opacdir/cgi-bin/");
327 system("touch $::opacdir/cgi-bin/opac");
328
329 system("chown -R root:$httpduser $::opacdir");
330 system("chown -R root:$httpduser $::intranetdir");
331
332
333 # Copy custom templates and reports back in
334
335 opendir D, "$backupdir/kohafiles-$date/intranet/htdocs/";
336 my @dirlist=readdir D;
337 foreach (@dirlist) {
338     (next) if (/^\./);
339     (next) if ($_ eq 'default');
340     (next) if ($_ eq 'doc');
341     (next) if ($_=~/^koha-/);
342     (next) if (-e "$::intranetdir/htdocs/$_");
343     print "Restoring custom intranet templates $_...\n";
344     system("cp -a $backupdir/kohafiles-$date/intranet/htdocs/$_ $::intranetdir/htdocs/");
345 }
346
347 opendir D, "$backupdir/kohafiles-$date/opac/htdocs/";
348 @dirlist=readdir D;
349 foreach (@dirlist) {
350     (next) if (/^\./);
351     (next) if ($_ eq 'default');
352     (next) if ($_ eq 'doc');
353     (next) if ($_=~/^koha-/);
354     (next) if (-e "$::opacdir/htdocs/$_");
355     print "Restoring custom opac template $_...\n";
356     system("cp -a $backupdir/kohafiles-$date/opac/htdocs/$_ $::opacdir/htdocs/");
357 }
358
359
360
361
362
363 unless ($::kohalogdir && -e $::kohalogdir) {
364     $::kohalogdir='/var/log/koha';
365     print "\n\nDirectory for logging by Z39.50 daemon [$::kohalogdir]: ";
366     chomp($input = <STDIN>);
367     if ($input) {
368         $::kohalogdir=$input;
369     }
370 }
371
372 unless (-e "$::kohalogdir") {
373     my $result = mkdir 0770, "$::kohalogdir";
374     if ($result==0) {
375         my @dirs = split(m#/#, $::kohalogdir);
376         my $checkdir='';
377         foreach (@dirs) {
378             $checkdir.="$_/";
379             unless (-e "$checkdir") {
380                 mkdir($checkdir, 0775);
381             }
382         }
383     }
384 }
385
386 chmod 0750, "$::intranetdir/scripts/z3950daemon/z3950-daemon-launch.sh";
387 chmod 0750, "$::intranetdir/scripts/z3950daemon/z3950-daemon-shell.sh";
388 chmod 0750, "$::intranetdir/scripts/z3950daemon/processz3950queue";
389 chown(0, (getpwnam($httpduser)) [3], "$::intranetdir/scripts/z3950daemon/z3950-daemon-shell.sh") or warn "can't chown $::intranetdir/scripts/z3950daemon/z3950-daemon-shell.sh: $!";
390 chown(0, (getpwnam($httpduser)) [3], "$::intranetdir/scripts/z3950daemon/processz3950queue") or warn "can't chown $::intranetdir/scripts/z3950daemon/processz3950queue: $!";
391
392
393
394 my $kccontents='';open (KC, "/etc/koha.conf");
395 my $kc;
396 while (<KC>) {
397     if (/^\s*includes\s*=/) {
398         $kccontents.="includes=$::intranetdir/htdocs/includes\n";
399         $kc->{'includes'}=1;
400     } elsif (/^\s*httpduser\s*=/) {
401         $kccontents.="httpduser=$httpduser\n";
402         $kc->{'httpduser'}=1;
403     } elsif (/^\s*kohaversion\s*=/) {
404         $kccontents.="kohaversion=$::newversion\n";
405         $kc->{'kohaversion'}=1;
406     } elsif (/^\s*kohalogdir\s*=/) {
407         $kccontents.="kohalogdir=$::kohalogdir\n";
408         $kc->{'kohalogdir'}=1;
409     } elsif (/^\s*intranetdir\s*=/) {
410         $kccontents.="intranetdir=$::intranetdir\n";
411         $kc->{'intranetdir'}=1;
412     } elsif (/^\s*opacdir\s*=/) {
413         $kccontents.="opacdir=$::opacdir\n";
414         $kc->{'opacdir'}=1;
415     } else {
416         $kccontents.="$_";
417     }
418 }
419
420 unless (defined($kc->{'kohaversion'})) {
421     $kccontents.="kohaversion=$::newversion\n";
422 }
423 unless (defined($kc->{'includes'})) {
424     $kccontents.="includes=$::intranetdir/htdocs/includes\n";
425 }
426 unless (defined($kc->{'httpduser'})) {
427     $kccontents.="httpduser=$httpduser\n";
428 }
429 unless (defined($kc->{'intranetdir'})) {
430     $kccontents.="intranetdir=$::intranetdir\n";
431 }
432 unless (defined($kc->{'opacdir'})) {
433     $kccontents.="opacdir=$::opacdir\n";
434 }
435 unless (defined($kc->{'kohalogdir'})) {
436     $kccontents.="kohalogdir=$::kohalogdir\n";
437 }
438
439
440 system("mv /etc/koha.conf /etc/koha.conf.backup");
441
442 open (KC, ">/etc/koha.conf") || warn "Couldn't open /etc/koha.conf for writing.";
443 print KC $kccontents;
444 close KC;
445
446
447 print qq|
448
449 Upgrading Database
450 ==================
451 |;
452 system ("perl -I $::intranetdir/modules scripts/updater/updatedatabase");
453
454
455 print qq|
456
457 ==================
458 = Authentication =
459 ==================
460
461 This release of Koha has a new authentication module.  If you are not already
462 using basic authentication on your intranet, you will be required to log in to
463 access some of the features of the intranet.  You can log in using the userid
464 and password from the /etc/koha.conf configuration file at any time.  Use the
465 "Members" module to add passwords for other accounts and set their permissions.
466 |;
467 print "Press the <ENTER> key to continue: ";
468 <STDIN>;
469
470
471
472 print qq|
473
474 ==========================
475 = Koha Upgrade Completed =
476 ==========================
477 The Koha Upgrade is finished.
478 If you are upgrading from a version of Koha
479 prior to 1.2.1, it is likely that you will have to modify your Apache
480 configuration to point it to the new files.
481
482 In your INTRANET VirtualHost section you should have:
483   DocumentRoot $::intranetdir/htdocs
484   ScriptAlias /cgi-bin/koha/ $::intranetdir/cgi-bin/
485   SetEnv PERL5LIB $::intranetdir/modules
486
487 In the OPAC VirtualHost section you should have:
488   DocumentRoot $::opacdir/htdocs
489   ScriptAlias /cgi-bin/koha/ $::opacdir/cgi-bin/
490   SetEnv PERL5LIB $::intranetdir/modules
491
492 You may also need to uncomment a "LoadModules env_module ... " line and restart
493 Apache.
494
495 Please report any problems you encounter through http://bugs.koha.org/
496 |;
497
498
499 my $reply=showmessage('Would you like to complete a survey about your library?', 'yn', 'y');
500 if ($reply=~/y/i) {
501     system("perl kohareporter");
502 }