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