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