3 # Copyright (C) 2008 Rod Roark <rod@sunsetsystems.com>
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
10 # This is for restoring a backup created by the "Backup" option
11 # in OpenEMR's administration menu, which invokes
12 # interface/main/backup.php.
14 # Xdialog is supported if available... dialog support is also in
15 # here but is disabled, as it was found to be ugly and clumsy.
21 if [ ! -z "$DISPLAY" -a ! -z "`which Xdialog`" ]; then
23 NOBUTTONS
="--no-buttons"
24 DEFAULTNO
="--default-no"
26 # elif [ ! -z `which dialog` ]; then
31 if [ ! -z "$DLGCMD" ]; then
34 while [ ! -z "$1" ]; do
38 $DLGCMD --title 'OpenEMR Restore' $LEFT --msgbox "$MSG" 0 0
41 while [ ! -z "$1" ]; do
48 if [ ! -z "$DLGCMD" ]; then
49 if [ "$DLGCMD" = "Xdialog" ]; then
52 $DLGCMD --title 'OpenEMR Restore' $LEFT --infobox "$1" 0 0
59 if [ ! -z "$DLGCMD" ]; then
61 RESULT
=`$DLGCMD --title 'OpenEMR Restore' --backtitle "$1" $NOBUTTONS --fselect $HOME/ 0 70 2>&1 1>&3`
64 if [ $CODE -eq 0 ]; then
71 read -e -p "$1: " RESULT
75 if [ ! -z "$DLGCMD" ]; then
78 while [ ! -z "$1" ]; do
82 $DLGCMD --title 'OpenEMR Restore' $DEFAULTNO $LEFT --yesno "$MSG" 0 0
85 if [ $CODE -eq 0 ]; then
87 elif [ $CODE -eq 1 ]; then
95 while [ ! -z "$2" ]; do
99 read -e -p "$1 [N/y] " RESULT
100 RESULT
=`expr "$RESULT" : "[yY]"`
105 if [ ! -z "$DLGCMD" ]; then
107 RESULT
=`$DLGCMD --title 'OpenEMR Restore' $LEFT --inputbox "$1" 0 0 2>&1 1>&3`
110 if [ $CODE -eq 0 ]; then
117 read -e -p "$1 " RESULT
121 if [ -z "$DLGCMD" ]; then
126 dlg_msg
"WARNING: This script is experimental." "It may have serious bugs or omissions." "Use it at your own risk!"
128 BAKDIR
=/tmp
/emr_backup
130 if [ $UID -ne 0 ]; then
131 dlg_msg
"Error: This script must be executed with root privileges."
135 # Create and change to a clean scratch directory.
138 if [ $?
-ne 0 ]; then
139 dlg_msg
"Error: Cannot create directory '$BAKDIR'."
143 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."
146 while [ $LOOKING -eq 1 ]; do
147 dlg_fselect
"Enter path/name of backup file"
150 if [ ! -f $TARFILE ]; then
151 dlg_msg
"Error: '$TARFILE' is not found or is not a file."
153 # Extract the backup tarball into the scratch directory.
154 dlg_info
"Extracting $TARFILE ..."
157 if [ $?
-ne 0 ]; then
158 dlg_msg
"Error: tar could not extract '$TARFILE'."
165 # Extract the OpenEMR web directory tree.
166 dlg_info
"Extracting $BAKDIR/openemr.tar.gz ..."
169 tar zxf ..
/openemr.
tar.gz
170 if [ $?
-ne 0 ]; then
171 dlg_msg
"Error: tar could not extract '$BAKDIR/openemr.tar.gz'."
175 # Get various parameters from the extracted files.
176 OEDIR
=`grep '^\$webserver_root' interface/globals.php | cut -d \" -f 2`
177 OEDBNAME
=`grep '^\$dbase' library/sqlconf.php | cut -d \' -f 2`
178 OEDBUSER
=`grep '^\$login' library/sqlconf.php | cut -d \' -f 2`
179 OEDBPASS
=`grep '^\$pass' library/sqlconf.php | cut -d \' -f 2`
181 if [ -f ..
/sql-ledger.sql
]; then
182 SLDBNAME
=`grep '^\$sl_dbname' interface/globals.php | cut -d \' -f 2`
183 SLDBUSER
=`grep '^\$sl_dbuser' interface/globals.php | cut -d \' -f 2`
184 SLDBPASS
=`grep '^\$sl_dbpass' interface/globals.php | cut -d \' -f 2`
188 if [ -f ..
/phpgacl.
tar.gz
]; then
189 GADIR
=`grep '^\s*\$phpgacl_location' library/acl.inc | cut -d \" -f 2`
192 dlg_info
"Extracting $BAKDIR/phpgacl.tar.gz ..."
193 tar zxf ..
/phpgacl.
tar.gz
194 if [ $?
-ne 0 ]; then
195 dlg_msg
"Error: tar could not extract '$BAKDIR/phpgacl.tar.gz'."
198 if [ -f ..
/phpgacl.sql.gz
]; then
199 GADBNAME
=`grep '^\s*var \$_db_name' gacl.class.php | cut -d \' -f 2`
200 GADBUSER
=`grep '^\s*var \$_db_user' gacl.class.php | cut -d \' -f 2`
201 GADBPASS
=`grep '^\s*var \$_db_password' gacl.class.php | cut -d \' -f 2`
205 if [ -f ..
/sql-ledger.
tar.gz
]; then
206 SLDIR
=`dirname $OEDIR`/sql-ledger
209 dlg_yesno
'Do you want to specify new locations and database names for the restore?'
214 if [ $CHANGES -gt 0 ]; then
215 dlg_msg
"Current values are shown in [brackets]. Just hit Enter to leave them as-is."
216 OLDOEDBNAME
="$OEDBNAME"
217 dlg_input
"OpenEMR database name [$OEDBNAME]? "
218 if [ ! -z "$RESULT" ]; then OEDBNAME
="$RESULT"; fi
219 OLDOEDBUSER
="$OEDBUSER"
220 dlg_input
"OpenEMR database user [$OEDBUSER]? "
221 if [ ! -z "$RESULT" ]; then OEDBUSER
="$RESULT"; fi
222 OLDOEDBPASS
="$OEDBPASS"
223 dlg_input
"OpenEMR database password [$OEDBPASS]? "
224 if [ ! -z "$RESULT" ]; then OEDBPASS
="$RESULT"; fi
225 if [ ! -z "$GADBNAME" ]; then
226 OLDGADBNAME
="$GADBNAME"
227 dlg_input
"phpGACL database name [$GADBNAME]? "
228 if [ ! -z "$RESULT" ]; then GADBNAME
="$RESULT"; fi
229 OLDGADBUSER
="$GADBUSER"
231 while [ $LOOKING -eq 1 ]; do
232 dlg_input
"phpGACL database user [$GADBUSER]? "
233 if [ ! -z "$RESULT" ]; then GADBUSER
="$RESULT"; fi
234 if [ "$GADBUSER" = "$SLDBUSER" ]; then
235 dlg_msg
"Error: OpenEMR and phpGACL have separate databases but the same user name." "They must be different user names."
236 GADBUSER
="$OLDGADBUSER"
241 OLDGADBPASS
="$GADBPASS"
242 dlg_input
"phpGACL database password [$GADBPASS]? "
243 if [ ! -z "$RESULT" ]; then GADBPASS
="$RESULT"; fi
245 if [ ! -z "$SLDBNAME" ]; then
246 OLDSLDBNAME
="$SLDBNAME"
247 dlg_input
"SQL-Ledger database name [$SLDBNAME]? "
248 if [ ! -z "$RESULT" ]; then SLDBNAME
="$RESULT"; fi
249 OLDSLDBUSER
="$SLDBUSER"
250 dlg_input
"SQL-Ledger database user [$SLDBUSER]? "
251 if [ ! -z "$RESULT" ]; then SLDBUSER
="$RESULT"; fi
252 OLDSLDBPASS
="$SLDBPASS"
253 dlg_input
"SQL-Ledger database password [$SLDBPASS]? "
254 if [ ! -z "$RESULT" ]; then SLDBPASS
="$RESULT"; fi
257 dlg_input
"OpenEMR web directory [$OEDIR]? "
258 if [ ! -z "$RESULT" ]; then OEDIR
="$RESULT"; fi
259 if [ ! -z "$GADIR" ]; then
261 dlg_input
"phpGACL web directory [$GADIR]? "
262 if [ ! -z "$RESULT" ]; then GADIR
="$RESULT"; fi
264 if [ ! -z "$SLDIR" ]; then
266 dlg_input
"SQL-Ledger web directory [$SLDIR]? "
267 if [ ! -z "$RESULT" ]; then SLDIR
="$RESULT"; fi
270 # The following is to figure out the proper value for $web_root.
273 DOCROOT
=`dirname $OEDIR`
274 OEFILE
=`basename $OEDIR`
275 while [ $LOOKING -eq 1 ]; do
276 dlg_yesno
"Is the Apache document root '$DOCROOT'?"
277 if [ $RESULT -eq 1 ]; then
280 OEFILE
=`basename $DOCROOT`/$OEFILE
281 DOCROOT
=`dirname $DOCROOT`
282 if [ "$DOCROOT" = "/" ]; then
284 OEFILE
=`basename $OEDIR`
285 DOCROOT
=`dirname $OEDIR`
286 dlg_msg
"Never mind, I will assume '$DOCROOT'. You may need to manually correct" "the value of \$web_root in '$OEDIR/interface/globals.php'."
294 # If phpgacl has its own database, make sure the database user is not the
295 # same as for openemr. This is to prevent screwups caused by persistent
296 # database connections in PHP.
297 if [ ! -z "$GADBNAME" ]; then
298 if [ "$GADBUSER" = "$SLDBUSER" ]; then
299 dlg_msg
"Error: OpenEMR and phpGACL have separate databases but the same user name." "They must be different user names."
304 # The following sanity checks are an attempt to avoid disastrous results
305 # from mistakes in entry of directory path names.
307 TRASH
=`expr "$OEDIR" : "[/]"`
308 if [ $TRASH -ne 1 ]; then
309 dlg_msg
"Error: The OpenEMR directory path '$OEDIR' does not start with '/'."
312 if [ -e "$OEDIR" -a ! -e "$OEDIR/interface/globals.php" ]; then
313 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."
317 TRASH
=`expr "$GADIR" : "[/]"`
318 if [ $TRASH -ne 1 ]; then
319 dlg_msg
"Error: The phpGACL directory path '$GADIR' does not start with '/'."
322 if [ -e "$GADIR" -a ! -e "$GADIR/gacl.class.php" ]; then
323 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."
327 if [ ! -z "$SLDIR" ]; then
328 TRASH
=`expr "$SLDIR" : "[/]"`
329 if [ $TRASH -ne 1 ]; then
330 dlg_msg
"Error: The SQL-Ledger directory path '$SLDIR' does not start with '/'."
333 if [ -e "$SLDIR" -a ! -e "$SLDIR/setup.pl" ]; then
334 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."
339 # Ask the user to do final sanity checking.
341 MARGS
="\"I will restore the OpenEMR database backup to the MySQL database '$OEDBNAME'.\""
342 MARGS
="$MARGS \"The OpenEMR database user will be '$OEDBUSER' with password '$OEDBPASS'.\""
343 if [ ! -z "$GADBNAME" ]; then
344 MARGS
="$MARGS \"I will restore the phpGACL database backup to the MySQL database '$GADBNAME'.\""
345 MARGS
="$MARGS \"The phpGACL database user will be '$GADBUSER' with password '$GADBPASS'.\""
347 if [ ! -z "$SLDBNAME" ]; then
348 MARGS
="$MARGS \"I will restore the SQL-Ledger database backup to the PostgreSQL database '$SLDBNAME'.\""
349 MARGS
="$MARGS \"The SQL-Ledger database user will be '$SLDBUSER' with password '$SLDBPASS'.\""
351 MARGS
="$MARGS \"I will copy the OpenEMR web directory backup to '$OEDIR'.\""
352 if [ ! -z "$GADIR" ]; then
353 MARGS
="$MARGS \"I will copy the phpGACL web directory backup to '$GADIR'.\""
355 if [ ! -z "$SLDIR" ]; then
356 MARGS
="$MARGS \"I will copy the SQL-Ledger web directory backup to '$SLDIR'.\""
359 MARGS
="$MARGS \"Please check the above very carefully!\""
360 MARGS
="$MARGS \"Any existing databases and directories matching these names will be DESTROYED.\""
361 MARGS
="$MARGS \"Do you wish to continue?\""
363 eval "dlg_yesno $MARGS"
364 if [ $RESULT -ne 1 ]; then
370 dlg_msg
"In order to create MySQL databases and users on this computer, I will need to" \
371 "log into MySQL as its 'root' user. The next question asks for the MySQL root" \
372 "user's password for this server, the one that you are restoring to. This is" \
373 "a MySQL password, not a system login password. Often it is blank."
374 dlg_input
'Enter the password, if any, for the MySQL root user:'
379 dlg_info
"Dropping old OpenEMR database if it exists ..."
380 mysqladmin
--password="$MYROOTPASS" --force drop
$OEDBNAME 2> /dev
/null
382 dlg_info
"Restoring OpenEMR database ..."
384 gunzip openemr.sql.gz
385 if [ $?
-ne 0 ]; then
386 dlg_msg
"Error: Could not decompress '$BAKDIR/openemr.sql.gz'."
389 mysqladmin
--password="$MYROOTPASS" create
$OEDBNAME
390 if [ $?
-ne 0 ]; then
391 dlg_msg
"Error: Could not create MySQL database '$OEDBNAME'."
394 mysql
--password="$MYROOTPASS" --execute "GRANT ALL PRIVILEGES ON $OEDBNAME.* TO '$OEDBUSER'@'localhost' IDENTIFIED BY '$OEDBPASS'" $OEDBNAME
395 mysql
--user=$OEDBUSER --password="$OEDBPASS" $OEDBNAME < openemr.sql
396 if [ $?
-ne 0 ]; then
397 dlg_msg
"Error: Restore to database '$OEDBNAME' failed."
401 if [ ! -z "$GADBNAME" ]; then
402 dlg_info
"Dropping old phpGACL database if it exists ..."
403 mysqladmin
--password="$MYROOTPASS" --force drop
$GADBNAME 2> /dev
/null
404 dlg_info
"Restoring phpGACL database ..."
406 gunzip phpgacl.sql.gz
407 if [ $?
-ne 0 ]; then
408 dlg_msg
"Error: Could not decompress '$BAKDIR/phpgacl.sql.gz'."
411 mysqladmin
--password="$MYROOTPASS" create
$GADBNAME
412 if [ $?
-ne 0 ]; then
413 dlg_msg
"Error: Could not create MySQL database '$GADBNAME'."
416 mysql
--password="$MYROOTPASS" --execute "GRANT ALL PRIVILEGES ON $GADBNAME.* TO '$GADBUSER'@'localhost' IDENTIFIED BY '$GADBPASS'" $GADBNAME
417 mysql
--user=$GADBUSER --password="$GADBPASS" $GADBNAME < phpgacl.sql
418 if [ $?
-ne 0 ]; then
419 dlg_msg
"Error: Restore to database '$GADBNAME' failed."
424 if [ ! -z "$SLDBNAME" ]; then
425 # Avoid local domain connections for the sql-ledger user, because a
426 # default postgresql configuration is likely set to require "ident"
427 # authentication for them.
429 if [ ! -z "$PGHOST" ]; then
435 dlg_info
"Restarting Apache to close persistent database connections ..."
437 dlg_info
"Dropping old SQL-Ledger database if it exists ..."
438 sudo
-u postgres psql
--command "DROP DATABASE \"$SLDBNAME\"" template1
2> /dev
/null
439 dlg_info
"Creating procedural language and database user ..."
440 sudo
-u postgres createlang plpgsql template1
2> /dev
/null
441 sudo
-u postgres psql
--command "DROP ROLE \"$SLDBUSER\"" template1
2> /dev
/null
443 # This next part merits some comment. The database is best loaded by the
444 # sql-ledger user, otherwise we will have the nasty task of granting that
445 # user access to all of its tables individually, which PostgreSQL does not
446 # provide a convenient way of doing. However superuser privilege is needed
447 # to properly restore the dump. Therefore we give the new role superuser
448 # privilege now and revoke it after the restore is done.
450 sudo
-u postgres psql
--command "CREATE ROLE \"$SLDBUSER\" PASSWORD '$SLDBPASS' SUPERUSER CREATEDB CREATEROLE INHERIT LOGIN" template1
451 dlg_info
"Creating and restoring SQL-Ledger database ..."
452 export PGUSER
="$SLDBUSER"
453 export PGPASSWORD
="$SLDBPASS"
454 psql
-h $MYPGHOST --command "CREATE DATABASE \"$SLDBNAME\" WITH TEMPLATE template0" template1
455 if [ $?
-ne 0 ]; then
456 dlg_msg
"Error: Could not create PostgreSQL database '$SLDBNAME'."
459 pg_restore
-h $MYPGHOST --no-owner --dbname=$SLDBNAME sql-ledger.sql
460 if [ $?
-ne 0 ]; then
461 dlg_msg
"Error: Restore to database '$SLDBNAME' failed."
466 sudo
-u postgres psql
--command "ALTER ROLE \"$SLDBUSER\" NOSUPERUSER" template1
467 if [ $?
-ne 0 ]; then
468 dlg_info
"Warning: ALTER ROLE failed."
470 sudo
-u postgres psql
--command "ANALYZE" $SLDBNAME
473 dlg_info
"Restoring OpenEMR web directory tree ..."
476 mv $BAKDIR/openemr
$OEDIR
477 if [ $?
-ne 0 ]; then
478 dlg_msg
"Error: Cannot create directory '$OEDIR'."
482 if [ $CHANGES -gt 0 ]; then
483 dlg_info
"Modifying $OEDIR/interface/globals.php ..."
485 mv -f globals.php globals.php.old
486 sed "s^$OLDOEDIR^$OEDIR^" globals.php.old | \
487 sed "/\\\$web_root *=/ c \$web_root = \"/$OEFILE\";" | \
488 sed "s^sl_dbname *= '.*'^sl_dbname = '$SLDBNAME'^" | \
489 sed "s^sl_dbuser *= '.*'^sl_dbuser = '$SLDBUSER'^" | \
490 sed "s^sl_dbpass *= '.*'^sl_dbpass = '$SLDBPASS'^" \
493 dlg_info
"Modifying $OEDIR/library/sqlconf.php ..."
495 mv sqlconf.php sqlconf.php.old
496 sed "s^dbase\t= '.*'^dbase\t= '$OEDBNAME'^" sqlconf.php.old | \
497 sed "s^login\t= '.*'^login\t= '$OEDBUSER'^" | \
498 sed "s^pass\t= '.*'^pass\t= '$OEDBPASS'^" > sqlconf.php
500 dlg_info
"Modifying $OEDIR/includes/config.php ..."
501 SLFILE
=`dirname /$OEFILE`
502 if [ "$SLFILE" = "/" ]; then
505 SLFILE
="$SLFILE/sql-ledger"
508 cp -f config.php config.php.old
509 sed "s^\"/.*/ws_server.pl\"^\"$SLFILE/ws_server.pl\"^" config.php.old
> config.php
512 if [ ! -z "$GADIR" ]; then
513 dlg_info
"Restoring phpGACL web directory tree ..."
516 mv $BAKDIR/phpgacl
$GADIR
517 if [ $?
-ne 0 ]; then
518 dlg_msg
"Error: Cannot create directory '$GADIR'."
522 if [ $CHANGES -gt 0 ]; then
523 dlg_info
"Modifying $OEDIR/library/acl.inc ..."
525 mv -f acl.inc acl.inc.old
526 sed "s^$OLDGADIR^$GADIR^" acl.inc.old
> acl.inc
528 if [ -z "$GADBNAME" ]; then
532 OLDGADBNAME
="$OLDOEDBNAME"
533 OLDGADBUSER
="$OLDOEDBUSER"
534 OLDGADBPASS
="$OLDOEDBPASS"
537 dlg_info
"Modifying $GADIR/gacl.class.php ..."
539 mv -f gacl.class.php gacl.class.php.old
540 sed "s^db_name *= *'.*'^db_name = '$GADBNAME'^" gacl.class.php.old | \
541 sed "s^db_user *= *'.*'^db_user = '$GADBUSER'^" | \
542 sed "s^db_password *= *'.*'^db_password = '$GADBPASS'^" > gacl.class.php
544 dlg_info
"Modifying $GADIR/gacl.ini.php ..."
546 mv -f gacl.ini.php gacl.ini.php.old
547 sed "s^db_name[ \t]*= *\".*\"^db_name\t\t\t= \"$GADBNAME\"^" gacl.ini.php.old | \
548 sed "s^db_user[ \t]*= *\".*\"^db_user\t\t\t= \"$GADBUSER\"^" | \
549 sed "s^db_password[ \t]*= *\".*\"^db_password\t\t= \"$GADBPASS\"^" > gacl.ini.php
553 if [ ! -z "$SLDIR" ]; then
554 dlg_info
"Restoring SQL-Ledger web directory tree ..."
559 if [ $?
-ne 0 ]; then
560 dlg_msg
"Error: Creating $SLDIR failed."
563 tar zxf
$BAKDIR/sql-ledger.
tar.gz
564 if [ $?
-ne 0 ]; then
565 dlg_msg
"Error: Extracting '$BAKDIR/sql-ledger.tar.gz' failed."
569 if [ $CHANGES -gt 0 ]; then
570 # SQL-Ledger stores passwords in an obfuscated form.
571 SLDBHASH
=`perl -e "print pack u, '$SLDBPASS'"`
573 dlg_info
"Modifying $SLDIR/ws_server.pl ..."
575 mv -f ws_server.pl ws_server.pl.old
576 sed "s^$OLDSLDIR^$SLDIR^" ws_server.pl.old
> ws_server.pl
577 chmod a
+x ws_server.pl
579 dlg_info
"Modifying $SLDIR/users/admin.conf ..."
581 cp -f admin.conf admin.conf.old
582 sed "s^dbname => '.*'^dbname => '$SLDBNAME'^" admin.conf.old | \
583 sed "s^dbuser => '.*'^dbuser => '$SLDBUSER'^" | \
584 sed "/dbpasswd =>/ c \ dbpasswd => '$SLDBHASH'," | \
585 sed "s^dbname=$OLDSLDBNAME^dbname=$SLDBNAME^" > admin.conf
587 dlg_info
"Modifying $SLDIR/users/members ..."
589 cp -f members members.old
590 sed "s^dbname=.*^dbname=$SLDBNAME^g" members.old | \
591 sed "s^dbuser=.*^dbuser=$SLDBUSER^g" | \
592 sed "/dbpasswd=/ c dbpasswd=$SLDBHASH" > members