3 # Copyright (C) 2008-2021 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"
29 if [ ! -z "$DLGCMD" ]; then
32 while [ ! -z "$1" ]; do
36 $DLGCMD --title 'OpenEMR Restore' $LEFT --msgbox "$MSG" 0 0
39 while [ ! -z "$1" ]; do
46 if [ ! -z "$DLGCMD" ]; then
47 if [ "$DLGCMD" = "Xdialog" ]; then
50 $DLGCMD --title 'OpenEMR Restore' $LEFT --infobox "$1" 0 0
57 if [ ! -z "$DLGCMD" ]; then
59 RESULT
=`$DLGCMD --title 'OpenEMR Restore' --backtitle "$1" $NOBUTTONS --fselect $HOME/ 0 70 2>&1 1>&3`
62 if [ $CODE -eq 0 ]; then
69 read -e -p "$1: " RESULT
73 if [ ! -z "$DLGCMD" ]; then
76 while [ ! -z "$1" ]; do
80 $DLGCMD --title 'OpenEMR Restore' $DEFAULTNO $LEFT --yesno "$MSG" 0 0
83 if [ $CODE -eq 0 ]; then
85 elif [ $CODE -eq 1 ]; then
93 while [ ! -z "$2" ]; do
97 read -e -p "$1 [N/y] " RESULT
98 RESULT
=`expr "$RESULT" : "[yY]"`
103 if [ ! -z "$DLGCMD" ]; then
105 RESULT
=`$DLGCMD --title 'OpenEMR Restore' $LEFT --inputbox "$1" 0 0 2>&1 1>&3`
108 if [ $CODE -eq 0 ]; then
115 read -e -p "$1 " RESULT
119 if [ -z "$DLGCMD" ]; then
124 dlg_msg
"WARNING: This script is experimental." "It may have serious bugs or omissions." "Use it at your own risk!"
126 BAKDIR
=/tmp
/emr_backup
128 if [ $UID -ne 0 ]; then
129 dlg_msg
"Error: This script must be executed with root privileges."
133 # Create and change to a clean scratch directory.
136 if [ $?
-ne 0 ]; then
137 dlg_msg
"Error: Cannot create directory '$BAKDIR'."
141 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."
144 while [ $LOOKING -eq 1 ]; do
145 dlg_fselect
"Enter path/name of backup file"
148 if [ ! -f $TARFILE ]; then
149 dlg_msg
"Error: '$TARFILE' is not found or is not a file."
151 # Extract the backup tarball into the scratch directory.
152 dlg_info
"Extracting $TARFILE ..."
155 if [ $?
-ne 0 ]; then
156 dlg_msg
"Error: tar could not extract '$TARFILE'."
163 # Extract the OpenEMR web directory tree.
164 dlg_info
"Extracting $BAKDIR/openemr.tar.gz ..."
167 tar zxf ..
/openemr.
tar.gz
168 if [ $?
-ne 0 ]; then
169 dlg_msg
"Error: tar could not extract '$BAKDIR/openemr.tar.gz'."
173 OEDIR
=/var
/www
/openemr
175 # Get the Site ID, it should be the only site backed up.
176 SITEID
=`ls -1 sites | head -n 1`
178 # Get various parameters from the extracted files.
179 OEDBNAME
=`grep '^\$dbase' sites/$SITEID/sqlconf.php | cut -d \' -f 2 | cut -d \" -f 2`
180 OEDBUSER
=`grep '^\$login' sites/$SITEID/sqlconf.php | cut -d \' -f 2 | cut -d \" -f 2`
181 OEDBPASS
=`grep '^\$pass' sites/$SITEID/sqlconf.php | cut -d \' -f 2 | cut -d \" -f 2`
183 dlg_yesno
"Do you want to specify site ID, locations or database names for the restore?"
190 if [ $CHANGES -gt 0 ]; then
191 dlg_msg
"Current values are shown in [brackets]. Just hit Enter to leave them as-is."
192 dlg_input
"Site ID [$SITEID]? "
193 if [ ! -z "$RESULT" ]; then SITEID
="$RESULT"; fi
194 dlg_input
"OpenEMR database name [$OEDBNAME]? "
195 if [ ! -z "$RESULT" ]; then OEDBNAME
="$RESULT"; fi
196 dlg_input
"OpenEMR database user [$OEDBUSER]? "
197 if [ ! -z "$RESULT" ]; then OEDBUSER
="$RESULT"; fi
198 dlg_input
"OpenEMR database password [$OEDBPASS]? "
199 if [ ! -z "$RESULT" ]; then OEDBPASS
="$RESULT"; fi
201 dlg_input
"New OpenEMR web directory [$OEDIR]? "
202 if [ ! -z "$RESULT" ]; then OEDIR
="$RESULT"; fi
206 # The following sanity checks are an attempt to avoid disastrous results
207 # from mistakes in entry of directory path names.
209 TRASH
=`expr "$OEDIR" : "[/]"`
210 if [ $TRASH -ne 1 ]; then
211 dlg_msg
"Error: The OpenEMR directory path '$OEDIR' does not start with '/'."
214 if [ -e "$OEDIR" -a ! -e "$OEDIR/interface/globals.php" ]; then
215 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."
219 if [ -e "$OEDIR" -a ! -e "$OEDIR/sites" ]; then
220 dlg_msg
"Error: Directory '$OEDIR/sites' is missing - old release needs removal?"
224 if [ -e "$OEDIR/sites/$SITEID" ]; then
225 dlg_msg
"Error: Site '$SITEID' already exists in '$OEDIR/sites'."
229 COLLATE
="utf8_general_ci"
230 dlg_msg
"If you have a particular requirement for the UTF-8 collation to use, " \
231 "then please specify it here. Hit Enter to accept the default '$COLLATE'." \
232 "Enter 'none' if you do not want UTF-8."
233 dlg_input
"UTF-8 collation [$COLLATE]? "
234 if [ ! -z "$RESULT" ]; then COLLATE
="$RESULT"; fi
235 TRASH
=`expr "$COLLATE" : "[uU]"`
236 if [ $TRASH -ne 1 ]; then
240 # Ask the user to do final sanity checking.
242 MARGS
="\"Your Site ID will be '$SITEID'.\""
243 if [ -e "$OEDIR" ]; then
244 MARGS
="$MARGS \"Only site-specific files will be restored to '$OEDIR/sites/$SITEID' in the existing OpenEMR web directory.\""
246 MARGS
="$MARGS \"I will install a new OpenEMR web directory '$OEDIR' from the backup.\""
248 MARGS
="$MARGS \"I will restore the OpenEMR database backup to the MySQL database '$OEDBNAME'.\""
249 MARGS
="$MARGS \"The OpenEMR database user will be '$OEDBUSER' with password '$OEDBPASS'.\""
250 if [ -z "$COLLATE" ]; then
251 MARGS
="$MARGS \"MySQL will use its default character set and collation.\""
253 MARGS
="$MARGS \"MySQL will use character set 'utf8' with collation '$COLLATE'.\""
256 MARGS
="$MARGS \"Please check the above very carefully!\""
257 MARGS
="$MARGS \"Any existing databases and directories matching these names will be DESTROYED.\""
258 MARGS
="$MARGS \"Do you wish to continue?\""
260 eval "dlg_yesno $MARGS"
261 if [ $RESULT -ne 1 ]; then
267 dlg_msg
"In order to create MySQL databases and users on this computer, I will need to" \
268 "log into MySQL as its 'root' user. The next question asks for the MySQL root" \
269 "user's password for this server, the one that you are restoring to. This is" \
270 "a MySQL password, not a system login password. It might be blank."
271 dlg_input
'Enter the password, if any, for the MySQL root user:'
276 dlg_info
"Dropping old OpenEMR database if it exists ..."
277 mysqladmin
--password="$MYROOTPASS" --force drop
$OEDBNAME 2> /dev
/null
279 dlg_info
"Restoring OpenEMR database ..."
281 gunzip openemr.sql.gz
282 if [ $?
-ne 0 ]; then
283 dlg_msg
"Error: Could not decompress '$BAKDIR/openemr.sql.gz'."
287 if [ -z $COLLATE ]; then
288 TRASH
="CREATE DATABASE $OEDBNAME"
290 TRASH
="CREATE DATABASE $OEDBNAME CHARACTER SET utf8 COLLATE $COLLATE"
292 mysql
--password="$MYROOTPASS" --execute "$TRASH"
293 if [ $?
-ne 0 ]; then
294 dlg_msg
"Error creating MySQL database with '$TRASH'."
298 mysql
--password="$MYROOTPASS" --execute "CREATE USER '$OEDBUSER'@'localhost' IDENTIFIED BY '$OEDBPASS'" $OEDBNAME
299 mysql
--password="$MYROOTPASS" --execute "GRANT ALL PRIVILEGES ON $OEDBNAME.* TO '$OEDBUSER'@'localhost' WITH GRANT OPTION" $OEDBNAME
301 mysql
--user=$OEDBUSER --password="$OEDBPASS" $OEDBNAME < openemr.sql
302 if [ $?
-ne 0 ]; then
303 dlg_msg
"Error: Restore to database '$OEDBNAME' failed."
307 if [ -e "$OEDIR" ]; then
308 dlg_info
"Restoring site subdirectory ..."
309 mv $BAKDIR/openemr
/sites
/$OLDSITEID $OEDIR/sites
/$SITEID
310 if [ $?
-ne 0 ]; then
311 dlg_msg
"Error: Cannot create directory '$OEDIR/sites/$SITEID'."
315 dlg_info
"Restoring OpenEMR web directory tree ..."
316 mv $BAKDIR/openemr
$OEDIR
317 if [ $?
-ne 0 ]; then
318 dlg_msg
"Error: Cannot create directory '$OEDIR'."
323 if [ $CHANGES -gt 0 ]; then
324 dlg_info
"Modifying $OEDIR/sites/$SITEID/sqlconf.php ..."
325 cd $OEDIR/sites
/$SITEID
326 mv sqlconf.php sqlconf.php.old
327 sed "s^dbase\s*=\s*['\"].*['\"]^dbase\t= '$OEDBNAME'^" sqlconf.php.old | \
328 sed "s^login\s*=\s*['\"].*['\"]^login\t= '$OEDBUSER'^" | \
329 sed "s^pass\s*=\s*['\"].*['\"]^pass\t= '$OEDBPASS'^" > sqlconf.php