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