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