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