forked from openemr/openemr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
restore
executable file
·644 lines (596 loc) · 20.5 KB
/
restore
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
#!/bin/bash
# Copyright (C) 2008-2010 Rod Roark <[email protected]>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This is for restoring a backup created by the "Backup" option
# in OpenEMR's administration menu, which invokes
# interface/main/backup.php.
#
# Xdialog is supported if available... dialog support is also in
# here but is disabled, as it was found to be ugly and clumsy.
DLGCMD=""
NOBUTTONS=""
DEFAULTNO=""
LEFT=""
if [ ! -z "$DISPLAY" -a ! -z "`which Xdialog`" ]; then
DLGCMD="Xdialog"
NOBUTTONS="--no-buttons"
DEFAULTNO="--default-no"
LEFT="--left"
# elif [ ! -z `which dialog` ]; then
# DLGCMD="dialog"
fi
dlg_msg() {
if [ ! -z "$DLGCMD" ]; then
local MSG="$1"
shift
while [ ! -z "$1" ]; do
MSG="$MSG\n$1"
shift
done
$DLGCMD --title 'OpenEMR Restore' $LEFT --msgbox "$MSG" 0 0
return 0
fi
while [ ! -z "$1" ]; do
echo "$1"
shift
done
}
dlg_info() {
if [ ! -z "$DLGCMD" ]; then
if [ "$DLGCMD" = "Xdialog" ]; then
echo "$1"
fi
$DLGCMD --title 'OpenEMR Restore' $LEFT --infobox "$1" 0 0
return 0
fi
echo "$1"
}
dlg_fselect() {
if [ ! -z "$DLGCMD" ]; then
exec 3>&1
RESULT=`$DLGCMD --title 'OpenEMR Restore' --backtitle "$1" $NOBUTTONS --fselect $HOME/ 0 70 2>&1 1>&3`
CODE=$?
exec 3>&-
if [ $CODE -eq 0 ]; then
return 0
fi
echo $RESULT
exit 1
fi
echo " "
read -e -p "$1: " RESULT
}
dlg_yesno() {
if [ ! -z "$DLGCMD" ]; then
local MSG="$1"
shift
while [ ! -z "$1" ]; do
MSG="$MSG\n$1"
shift
done
$DLGCMD --title 'OpenEMR Restore' $DEFAULTNO $LEFT --yesno "$MSG" 0 0
CODE=$?
exec 3>&-
if [ $CODE -eq 0 ]; then
RESULT="1"
elif [ $CODE -eq 1 ]; then
RESULT="0"
else
exit 1
fi
return 0
fi
echo " "
while [ ! -z "$2" ]; do
echo "$1"
shift
done
read -e -p "$1 [N/y] " RESULT
RESULT=`expr "$RESULT" : "[yY]"`
return 0
}
dlg_input() {
if [ ! -z "$DLGCMD" ]; then
exec 3>&1
RESULT=`$DLGCMD --title 'OpenEMR Restore' $LEFT --inputbox "$1" 0 0 2>&1 1>&3`
CODE=$?
exec 3>&-
if [ $CODE -eq 0 ]; then
return 0
fi
echo $RESULT
exit 1
fi
echo " "
read -e -p "$1 " RESULT
}
dlg_blank_line() {
if [ -z "$DLGCMD" ]; then
echo " "
fi
}
dlg_msg "WARNING: This script is experimental." "It may have serious bugs or omissions." "Use it at your own risk!"
BAKDIR=/tmp/emr_backup
if [ $UID -ne 0 ]; then
dlg_msg "Error: This script must be executed with root privileges."
exit 1
fi
# Create and change to a clean scratch directory.
rm -rf $BAKDIR
mkdir $BAKDIR
if [ $? -ne 0 ]; then
dlg_msg "Error: Cannot create directory '$BAKDIR'."
exit 1
fi
dlg_msg "Now you will be asked for the backup file." "By default this is named emr_backup.tar, although you may have saved it as something else."
LOOKING=1
while [ $LOOKING -eq 1 ]; do
dlg_fselect "Enter path/name of backup file"
TARFILE=$RESULT
dlg_blank_line
if [ ! -f $TARFILE ]; then
dlg_msg "Error: '$TARFILE' is not found or is not a file."
else
# Extract the backup tarball into the scratch directory.
dlg_info "Extracting $TARFILE ..."
cd $BAKDIR
tar -xf $TARFILE
if [ $? -ne 0 ]; then
dlg_msg "Error: tar could not extract '$TARFILE'."
else
LOOKING=0
fi
fi
done
# Extract the OpenEMR web directory tree.
dlg_info "Extracting $BAKDIR/openemr.tar.gz ..."
mkdir openemr
cd openemr
tar zxf ../openemr.tar.gz
if [ $? -ne 0 ]; then
dlg_msg "Error: tar could not extract '$BAKDIR/openemr.tar.gz'."
exit 1
fi
OEDIR=/var/www/openemr
# Get the Site ID, it should be the only site backed up.
SITEID=`ls -1 sites | head -n 1`
# Get various parameters from the extracted files.
OEDBNAME=`grep '^\$dbase' sites/$SITEID/sqlconf.php | cut -d \' -f 2 | cut -d \" -f 2`
OEDBUSER=`grep '^\$login' sites/$SITEID/sqlconf.php | cut -d \' -f 2 | cut -d \" -f 2`
OEDBPASS=`grep '^\$pass' sites/$SITEID/sqlconf.php | cut -d \' -f 2 | cut -d \" -f 2`
SLDBNAME=''
# SL support is becoming obsolete, but we're leaving it in for now.
if [ -f ../sql-ledger.sql ]; then
SLDBNAME=`grep '^\$sl_dbname' interface/globals.php | cut -d \' -f 2`
SLDBUSER=`grep '^\$sl_dbuser' interface/globals.php | cut -d \' -f 2`
SLDBPASS=`grep '^\$sl_dbpass' interface/globals.php | cut -d \' -f 2`
fi
# Likewise, external phpGACL is discouraged because it will not work
# with multiple sites.
GADIR=''
GACONFIGDIR=''
GADBNAME=''
if [ -f ../phpgacl.tar.gz ]; then
GADIR=`grep '^\s*\$phpgacl_location' library/acl.inc | cut -d \" -f 2`
GACONFIGDIR="$GADIR"
mkdir ../phpgacl
cd ../phpgacl
dlg_info "Extracting $BAKDIR/phpgacl.tar.gz ..."
tar zxf ../phpgacl.tar.gz
if [ $? -ne 0 ]; then
dlg_msg "Error: tar could not extract '$BAKDIR/phpgacl.tar.gz'."
exit 1
fi
if [ -f ../phpgacl.sql.gz ]; then
GADBNAME=`grep '^\s*var \$_db_name' gacl.class.php | cut -d \' -f 2`
GADBUSER=`grep '^\s*var \$_db_user' gacl.class.php | cut -d \' -f 2`
GADBPASS=`grep '^\s*var \$_db_password' gacl.class.php | cut -d \' -f 2`
fi
elif [ -e gacl ]; then
grep '^\s*\$phpgacl_location' library/acl.inc > /dev/null
if [ $? -eq 0 ]; then
GACONFIGDIR="$OEDIR/gacl"
if [ -f ../phpgacl.sql.gz ]; then
cd gacl
GADBNAME=`grep '^\s*var \$_db_name' gacl.class.php | cut -d \' -f 2`
GADBUSER=`grep '^\s*var \$_db_user' gacl.class.php | cut -d \' -f 2`
GADBPASS=`grep '^\s*var \$_db_password' gacl.class.php | cut -d \' -f 2`
cd ..
fi
fi
fi
#
SLDIR=''
if [ -f ../sql-ledger.tar.gz ]; then
SLDIR=`dirname $OEDIR`/sql-ledger
fi
dlg_yesno "Do you want to specify site ID, locations or database names for the restore?"
CHANGES=$RESULT
OLDSITEID=$SITEID
dlg_blank_line
if [ $CHANGES -gt 0 ]; then
dlg_msg "Current values are shown in [brackets]. Just hit Enter to leave them as-is."
dlg_input "Site ID [$SITEID]? "
if [ ! -z "$RESULT" ]; then SITEID="$RESULT"; fi
dlg_input "OpenEMR database name [$OEDBNAME]? "
if [ ! -z "$RESULT" ]; then OEDBNAME="$RESULT"; fi
dlg_input "OpenEMR database user [$OEDBUSER]? "
if [ ! -z "$RESULT" ]; then OEDBUSER="$RESULT"; fi
dlg_input "OpenEMR database password [$OEDBPASS]? "
if [ ! -z "$RESULT" ]; then OEDBPASS="$RESULT"; fi
if [ ! -z "$GADBNAME" ]; then
dlg_input "phpGACL database name [$GADBNAME]? "
if [ ! -z "$RESULT" ]; then GADBNAME="$RESULT"; fi
OLDGADBUSER="$GADBUSER"
LOOKING=1
while [ $LOOKING -eq 1 ]; do
dlg_input "phpGACL database user [$GADBUSER]? "
if [ ! -z "$RESULT" ]; then GADBUSER="$RESULT"; fi
if [ "$GADBUSER" = "$SLDBUSER" ]; then
dlg_msg "Error: OpenEMR and phpGACL have separate databases but the same user name." "They must be different user names."
GADBUSER="$OLDGADBUSER"
else
LOOKING=0
fi
done
dlg_input "phpGACL database password [$GADBPASS]? "
if [ ! -z "$RESULT" ]; then GADBPASS="$RESULT"; fi
fi
if [ ! -z "$SLDBNAME" ]; then
OLDSLDBNAME="$SLDBNAME"
dlg_input "SQL-Ledger database name [$SLDBNAME]? "
if [ ! -z "$RESULT" ]; then SLDBNAME="$RESULT"; fi
dlg_input "SQL-Ledger database user [$SLDBUSER]? "
if [ ! -z "$RESULT" ]; then SLDBUSER="$RESULT"; fi
dlg_input "SQL-Ledger database password [$SLDBPASS]? "
if [ ! -z "$RESULT" ]; then SLDBPASS="$RESULT"; fi
fi
OLDOEDIR="$OEDIR"
dlg_input "New OpenEMR web directory [$OEDIR]? "
if [ ! -z "$RESULT" ]; then OEDIR="$RESULT"; fi
if [ ! -z "$GADIR" ]; then
OLDGADIR="$GADIR"
dlg_input "New phpGACL web directory [$GADIR]? "
if [ ! -z "$RESULT" ]; then GADIR="$RESULT"; fi
fi
if [ ! -z "$SLDIR" ]; then
OLDSLDIR="$SLDIR"
dlg_input "New SQL-Ledger web directory [$SLDIR]? "
if [ ! -z "$RESULT" ]; then SLDIR="$RESULT"; fi
dlg_input "Previous SQL-Ledger web directory [$OLDSLDIR]? "
if [ ! -z "$RESULT" ]; then OLDSLDIR="$RESULT"; fi
fi
#
fi
# Patch up $GACONFIGDIR in case it was changed.
if [ -z "$GADIR" ]; then
if [ ! -z "$GACONFIGDIR" ]; then
GACONFIGDIR="$OEDIR/gacl"
fi
else
GACONFIGDIR="$GADIR"
fi
# If phpgacl has its own database, make sure the database user is not the
# same as for openemr. This is to prevent screwups caused by persistent
# database connections in PHP.
if [ ! -z "$GADBNAME" ]; then
if [ "$GADBUSER" = "$OEDBUSER" ]; then
dlg_msg "Error: OpenEMR and phpGACL have separate databases but the same user name." "They must be different user names."
exit 1
fi
fi
# The following sanity checks are an attempt to avoid disastrous results
# from mistakes in entry of directory path names.
TRASH=`expr "$OEDIR" : "[/]"`
if [ $TRASH -ne 1 ]; then
dlg_msg "Error: The OpenEMR directory path '$OEDIR' does not start with '/'."
exit 1
fi
if [ -e "$OEDIR" -a ! -e "$OEDIR/interface/globals.php" ]; then
dlg_msg "Error: $OEDIR already exists but does not look like an OpenEMR directory." "If you are really sure you want to replace it, please remove it first."
exit 1
fi
if [ ! -z "$GADIR" ]; then
TRASH=`expr "$GADIR" : "[/]"`
if [ $TRASH -ne 1 ]; then
dlg_msg "Error: The phpGACL directory path '$GADIR' does not start with '/'."
exit 1
fi
if [ -e "$GADIR" -a ! -e "$GADIR/gacl.class.php" ]; then
dlg_msg "Error: $GADIR already exists but does not look like a phpGACL directory." "If you are really sure you want to replace it, please remove it first."
exit 1
fi
fi
if [ ! -z "$SLDIR" ]; then
TRASH=`expr "$SLDIR" : "[/]"`
if [ $TRASH -ne 1 ]; then
dlg_msg "Error: The SQL-Ledger directory path '$SLDIR' does not start with '/'."
exit 1
fi
if [ -e "$SLDIR" -a ! -e "$SLDIR/setup.pl" ]; then
dlg_msg "Error: $SLDIR already exists but does not look like a SQL-Ledger directory." "If you are really sure you want to replace it, please remove it first."
exit 1
fi
fi
if [ -e "$OEDIR" -a ! -e "$OEDIR/sites" ]; then
dlg_msg "Error: Directory '$OEDIR/sites' is missing - old release needs removal?"
exit 1
fi
if [ -e "$OEDIR/sites/$SITEID" ]; then
dlg_msg "Error: Site '$SITEID' already exists in '$OEDIR/sites'."
exit 1
fi
COLLATE="utf8_general_ci"
dlg_msg "If you have a particular requirement for the UTF-8 collation to use, " \
"then please specify it here. Hit Enter to accept the default '$COLLATE'." \
"Enter 'none' if you do not want UTF-8."
dlg_input "UTF-8 collation [$COLLATE]? "
if [ ! -z "$RESULT" ]; then COLLATE="$RESULT"; fi
TRASH=`expr "$COLLATE" : "[uU]"`
if [ $TRASH -ne 1 ]; then
COLLATE=""
fi
# Ask the user to do final sanity checking.
#
MARGS="\"Your Site ID will be '$SITEID'.\""
if [ -e "$OEDIR" ]; then
MARGS="$MARGS \"Only site-specific files will be restored to '$OEDIR/sites/$SITEID' in the existing OpenEMR web directory.\""
else
MARGS="$MARGS \"I will install a new OpenEMR web directory '$OEDIR' from the backup.\""
fi
MARGS="$MARGS \"I will restore the OpenEMR database backup to the MySQL database '$OEDBNAME'.\""
MARGS="$MARGS \"The OpenEMR database user will be '$OEDBUSER' with password '$OEDBPASS'.\""
if [ ! -z "$GADBNAME" ]; then
MARGS="$MARGS \"I will restore the phpGACL database backup to the MySQL database '$GADBNAME'.\""
MARGS="$MARGS \"The phpGACL database user will be '$GADBUSER' with password '$GADBPASS'.\""
fi
if [ -z "$COLLATE" ]; then
MARGS="$MARGS \"MySQL will use its default character set and collation.\""
else
MARGS="$MARGS \"MySQL will use character set 'utf8' with collation '$COLLATE'.\""
fi
if [ ! -z "$SLDBNAME" ]; then
MARGS="$MARGS \"I will restore the SQL-Ledger database backup to the PostgreSQL database '$SLDBNAME'.\""
MARGS="$MARGS \"The SQL-Ledger database user will be '$SLDBUSER' with password '$SLDBPASS'.\""
fi
if [ ! -z "$GADIR" ]; then
MARGS="$MARGS \"I will copy the phpGACL web directory backup to '$GADIR'.\""
fi
if [ ! -z "$SLDIR" ]; then
MARGS="$MARGS \"I will copy the SQL-Ledger web directory backup to '$SLDIR'.\""
fi
MARGS="$MARGS \" \""
MARGS="$MARGS \"Please check the above very carefully!\""
MARGS="$MARGS \"Any existing databases and directories matching these names will be DESTROYED.\""
MARGS="$MARGS \"Do you wish to continue?\""
#
eval "dlg_yesno $MARGS"
if [ $RESULT -ne 1 ]; then
exit 1
fi
dlg_blank_line
dlg_msg "In order to create MySQL databases and users on this computer, I will need to" \
"log into MySQL as its 'root' user. The next question asks for the MySQL root" \
"user's password for this server, the one that you are restoring to. This is" \
"a MySQL password, not a system login password. It might be blank."
dlg_input 'Enter the password, if any, for the MySQL root user:'
MYROOTPASS="$RESULT"
dlg_blank_line
dlg_info "Dropping old OpenEMR database if it exists ..."
mysqladmin --password="$MYROOTPASS" --force drop $OEDBNAME 2> /dev/null
dlg_info "Restoring OpenEMR database ..."
cd $BAKDIR
gunzip openemr.sql.gz
if [ $? -ne 0 ]; then
dlg_msg "Error: Could not decompress '$BAKDIR/openemr.sql.gz'."
exit 1
fi
if [ -z $COLLATE ]; then
TRASH="CREATE DATABASE $OEDBNAME"
else
TRASH="CREATE DATABASE $OEDBNAME CHARACTER SET utf8 COLLATE $COLLATE"
fi
mysql --password="$MYROOTPASS" --execute "$TRASH"
if [ $? -ne 0 ]; then
dlg_msg "Error creating MySQL database with '$TRASH'."
exit 1
fi
mysql --password="$MYROOTPASS" --execute "GRANT ALL PRIVILEGES ON $OEDBNAME.* TO '$OEDBUSER'@'localhost' IDENTIFIED BY '$OEDBPASS'" $OEDBNAME
mysql --user=$OEDBUSER --password="$OEDBPASS" $OEDBNAME < openemr.sql
if [ $? -ne 0 ]; then
dlg_msg "Error: Restore to database '$OEDBNAME' failed."
exit 1
fi
if [ ! -z "$GADBNAME" ]; then
dlg_info "Dropping old phpGACL database if it exists ..."
mysqladmin --password="$MYROOTPASS" --force drop $GADBNAME 2> /dev/null
dlg_info "Restoring phpGACL database ..."
cd $BAKDIR
gunzip phpgacl.sql.gz
if [ $? -ne 0 ]; then
dlg_msg "Error: Could not decompress '$BAKDIR/phpgacl.sql.gz'."
exit 1
fi
if [ -z $COLLATE ]; then
TRASH="CREATE DATABASE $GADBNAME"
else
TRASH="CREATE DATABASE $GADBNAME CHARACTER SET utf8 COLLATE $COLLATE"
fi
mysql --password="$MYROOTPASS" --execute "$TRASH"
if [ $? -ne 0 ]; then
dlg_msg "Error creating MySQL database with '$TRASH'."
exit 1
fi
mysql --password="$MYROOTPASS" --execute "GRANT ALL PRIVILEGES ON $GADBNAME.* TO '$GADBUSER'@'localhost' IDENTIFIED BY '$GADBPASS'" $GADBNAME
mysql --user=$GADBUSER --password="$GADBPASS" $GADBNAME < phpgacl.sql
if [ $? -ne 0 ]; then
dlg_msg "Error: Restore to database '$GADBNAME' failed."
exit 1
fi
fi
if [ ! -z "$SLDBNAME" ]; then
# Avoid local domain connections for the sql-ledger user, because a
# default postgresql configuration is likely set to require "ident"
# authentication for them.
MYPGHOST=localhost
if [ ! -z "$PGHOST" ]; then
MYPGHOST="$PGHOST";
fi
#
unset PGUSER
unset PGPASSWORD
dlg_info "Restarting Apache to close persistent database connections ..."
apache2ctl graceful
dlg_info "Dropping old SQL-Ledger database if it exists ..."
sudo -u postgres psql --command "DROP DATABASE \"$SLDBNAME\"" template1 2> /dev/null
dlg_info "Creating procedural language and database user ..."
sudo -u postgres createlang plpgsql template1 2> /dev/null
sudo -u postgres psql --command "DROP ROLE \"$SLDBUSER\"" template1 2> /dev/null
#
# This next part merits some comment. The database is best loaded by the
# sql-ledger user, otherwise we will have the nasty task of granting that
# user access to all of its tables individually, which PostgreSQL does not
# provide a convenient way of doing. However superuser privilege is needed
# to properly restore the dump. Therefore we give the new role superuser
# privilege now and revoke it after the restore is done.
#
sudo -u postgres psql --command "CREATE ROLE \"$SLDBUSER\" PASSWORD '$SLDBPASS' SUPERUSER CREATEDB CREATEROLE INHERIT LOGIN" template1
dlg_info "Creating and restoring SQL-Ledger database ..."
export PGUSER="$SLDBUSER"
export PGPASSWORD="$SLDBPASS"
psql -h $MYPGHOST --command "CREATE DATABASE \"$SLDBNAME\" WITH TEMPLATE template0" template1
if [ $? -ne 0 ]; then
dlg_msg "Error: Could not create PostgreSQL database '$SLDBNAME'."
exit 1
fi
pg_restore -h $MYPGHOST --no-owner --dbname=$SLDBNAME sql-ledger.sql
if [ $? -ne 0 ]; then
dlg_msg "Error: Restore to database '$SLDBNAME' failed."
exit 1
fi
unset PGUSER
unset PGPASSWORD
sudo -u postgres psql --command "ALTER ROLE \"$SLDBUSER\" NOSUPERUSER" template1
if [ $? -ne 0 ]; then
dlg_info "Warning: ALTER ROLE failed."
fi
sudo -u postgres psql --command "ANALYZE" $SLDBNAME
fi
if [ -e "$OEDIR" ]; then
dlg_info "Restoring site subdirectory ..."
mv $BAKDIR/openemr/sites/$OLDSITEID $OEDIR/sites/$SITEID
if [ $? -ne 0 ]; then
dlg_msg "Error: Cannot create directory '$OEDIR/sites/$SITEID'."
exit 1
fi
else
dlg_info "Restoring OpenEMR web directory tree ..."
mv $BAKDIR/openemr $OEDIR
if [ $? -ne 0 ]; then
dlg_msg "Error: Cannot create directory '$OEDIR'."
exit 1
fi
fi
#
if [ $CHANGES -gt 0 ]; then
if [ ! -z "$SLDIR" ]; then
dlg_info "Modifying $OEDIR/interface/globals.php ..."
cd $OEDIR/interface
mv -f globals.php globals.php.old
sed "s^sl_dbname *= '.*'^sl_dbname = '$SLDBNAME'^" globals.php.old | \
sed "s^sl_dbuser *= '.*'^sl_dbuser = '$SLDBUSER'^" | \
sed "s^sl_dbpass *= '.*'^sl_dbpass = '$SLDBPASS'^" \
> globals.php
fi
#
dlg_info "Modifying $OEDIR/sites/$SITEID/sqlconf.php ..."
cd $OEDIR/sites/$SITEID
mv sqlconf.php sqlconf.php.old
sed "s^dbase\s*=\s*['\"].*['\"]^dbase\t= '$OEDBNAME'^" sqlconf.php.old | \
sed "s^login\s*=\s*['\"].*['\"]^login\t= '$OEDBUSER'^" | \
sed "s^pass\s*=\s*['\"].*['\"]^pass\t= '$OEDBPASS'^" > sqlconf.php
#
# Logic to fix the path to ws_server.pl in includes/config.php was removed.
# Not gonna worry about this because SL is deprecated, the old logic was not
# very robust, and this can be fixed up manually easily enough.
fi
if [ ! -z "$GADIR" ]; then
dlg_info "Restoring phpGACL web directory tree ..."
mkdir -p $GADIR
rm -rf $GADIR
mv $BAKDIR/phpgacl $GADIR
if [ $? -ne 0 ]; then
dlg_msg "Error: Cannot create directory '$GADIR'."
exit 1
fi
#
if [ $CHANGES -gt 0 ]; then
dlg_info "Modifying $OEDIR/library/acl.inc ..."
cd $OEDIR/library
mv -f acl.inc acl.inc.old
sed "s^phpgacl_location *= *\"/.*\"^phpgacl_location = \"$GADIR\"^" acl.inc.old > acl.inc
fi
fi
if [ ! -z "$GACONFIGDIR" -a $CHANGES -gt 0 -a ! -z "$GADBNAME" ]; then
dlg_info "Modifying $GACONFIGDIR/gacl.class.php ..."
cd $GACONFIGDIR
mv -f gacl.class.php gacl.class.php.old
sed "s^db_name *= *'.*'^db_name = '$GADBNAME'^" gacl.class.php.old | \
sed "s^db_user *= *'.*'^db_user = '$GADBUSER'^" | \
sed "s^db_password *= *'.*'^db_password = '$GADBPASS'^" > gacl.class.php
#
dlg_info "Modifying $GACONFIGDIR/gacl.ini.php ..."
cd $GACONFIGDIR
mv -f gacl.ini.php gacl.ini.php.old
sed "s^db_name[ \t]*= *\".*\"^db_name\t\t\t= \"$GADBNAME\"^" gacl.ini.php.old | \
sed "s^db_user[ \t]*= *\".*\"^db_user\t\t\t= \"$GADBUSER\"^" | \
sed "s^db_password[ \t]*= *\".*\"^db_password\t\t= \"$GADBPASS\"^" > gacl.ini.php
fi
if [ ! -z "$SLDIR" ]; then
dlg_info "Restoring SQL-Ledger web directory tree ..."
mkdir -p $SLDIR
rm -rf $SLDIR
mkdir $SLDIR
cd $SLDIR
if [ $? -ne 0 ]; then
dlg_msg "Error: Creating $SLDIR failed."
exit 1
fi
tar zxf $BAKDIR/sql-ledger.tar.gz
if [ $? -ne 0 ]; then
dlg_msg "Error: Extracting '$BAKDIR/sql-ledger.tar.gz' failed."
exit 1
fi
#
if [ $CHANGES -gt 0 ]; then
# SQL-Ledger stores passwords in an obfuscated form.
SLDBHASH=`perl -e "print pack u, '$SLDBPASS'"`
#
dlg_info "Modifying $SLDIR/ws_server.pl ..."
cd $SLDIR
mv -f ws_server.pl ws_server.pl.old
sed "s^$OLDSLDIR^$SLDIR^" ws_server.pl.old > ws_server.pl
chmod a+x ws_server.pl
#
dlg_info "Modifying $SLDIR/users/admin.conf ..."
cd $SLDIR/users
cp -f admin.conf admin.conf.old
sed "s^dbname => '.*'^dbname => '$SLDBNAME'^" admin.conf.old | \
sed "s^dbuser => '.*'^dbuser => '$SLDBUSER'^" | \
sed "/dbpasswd =>/ c \ dbpasswd => '$SLDBHASH'," | \
sed "s^dbname=$OLDSLDBNAME^dbname=$SLDBNAME^" > admin.conf
#
dlg_info "Modifying $SLDIR/users/members ..."
cd $SLDIR/users
cp -f members members.old
sed "s^dbname=.*^dbname=$SLDBNAME^g" members.old | \
sed "s^dbuser=.*^dbuser=$SLDBUSER^g" | \
sed "/dbpasswd=/ c dbpasswd=$SLDBHASH" > members
fi
fi
dlg_msg "All done."