bug fix (#7559)
[openemr.git] / contrib / util / restore
blobcd2290e2d80be4a382667a25e2815f43a7e0df16
1 #!/bin/bash
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.
17 DLGCMD=""
18 NOBUTTONS=""
19 DEFAULTNO=""
20 LEFT=""
21 if [ ! -z "$DISPLAY" -a ! -z "`which Xdialog`" ]; then
22 DLGCMD="Xdialog"
23 NOBUTTONS="--no-buttons"
24 DEFAULTNO="--default-no"
25 LEFT="--left"
28 dlg_msg() {
29 if [ ! -z "$DLGCMD" ]; then
30 local MSG="$1"
31 shift
32 while [ ! -z "$1" ]; do
33 MSG="$MSG\n$1"
34 shift
35 done
36 $DLGCMD --title 'OpenEMR Restore' $LEFT --msgbox "$MSG" 0 0
37 return 0
39 while [ ! -z "$1" ]; do
40 echo "$1"
41 shift
42 done
45 dlg_info() {
46 if [ ! -z "$DLGCMD" ]; then
47 if [ "$DLGCMD" = "Xdialog" ]; then
48 echo "$1"
50 $DLGCMD --title 'OpenEMR Restore' $LEFT --infobox "$1" 0 0
51 return 0
53 echo "$1"
56 dlg_fselect() {
57 if [ ! -z "$DLGCMD" ]; then
58 exec 3>&1
59 RESULT=`$DLGCMD --title 'OpenEMR Restore' --backtitle "$1" $NOBUTTONS --fselect $HOME/ 0 70 2>&1 1>&3`
60 CODE=$?
61 exec 3>&-
62 if [ $CODE -eq 0 ]; then
63 return 0
65 echo $RESULT
66 exit 1
68 echo " "
69 read -e -p "$1: " RESULT
72 dlg_yesno() {
73 if [ ! -z "$DLGCMD" ]; then
74 local MSG="$1"
75 shift
76 while [ ! -z "$1" ]; do
77 MSG="$MSG\n$1"
78 shift
79 done
80 $DLGCMD --title 'OpenEMR Restore' $DEFAULTNO $LEFT --yesno "$MSG" 0 0
81 CODE=$?
82 exec 3>&-
83 if [ $CODE -eq 0 ]; then
84 RESULT="1"
85 elif [ $CODE -eq 1 ]; then
86 RESULT="0"
87 else
88 exit 1
90 return 0
92 echo " "
93 while [ ! -z "$2" ]; do
94 echo "$1"
95 shift
96 done
97 read -e -p "$1 [N/y] " RESULT
98 RESULT=`expr "$RESULT" : "[yY]"`
99 return 0
102 dlg_input() {
103 if [ ! -z "$DLGCMD" ]; then
104 exec 3>&1
105 RESULT=`$DLGCMD --title 'OpenEMR Restore' $LEFT --inputbox "$1" 0 0 2>&1 1>&3`
106 CODE=$?
107 exec 3>&-
108 if [ $CODE -eq 0 ]; then
109 return 0
111 echo $RESULT
112 exit 1
114 echo " "
115 read -e -p "$1 " RESULT
118 dlg_blank_line() {
119 if [ -z "$DLGCMD" ]; then
120 echo " "
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."
130 exit 1
133 # Create and change to a clean scratch directory.
134 rm -rf $BAKDIR
135 mkdir $BAKDIR
136 if [ $? -ne 0 ]; then
137 dlg_msg "Error: Cannot create directory '$BAKDIR'."
138 exit 1
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."
143 LOOKING=1
144 while [ $LOOKING -eq 1 ]; do
145 dlg_fselect "Enter path/name of backup file"
146 TARFILE=$RESULT
147 dlg_blank_line
148 if [ ! -f $TARFILE ]; then
149 dlg_msg "Error: '$TARFILE' is not found or is not a file."
150 else
151 # Extract the backup tarball into the scratch directory.
152 dlg_info "Extracting $TARFILE ..."
153 cd $BAKDIR
154 tar -xf $TARFILE
155 if [ $? -ne 0 ]; then
156 dlg_msg "Error: tar could not extract '$TARFILE'."
157 else
158 LOOKING=0
161 done
163 # Extract the OpenEMR web directory tree.
164 dlg_info "Extracting $BAKDIR/openemr.tar.gz ..."
165 mkdir openemr
166 cd openemr
167 tar zxf ../openemr.tar.gz
168 if [ $? -ne 0 ]; then
169 dlg_msg "Error: tar could not extract '$BAKDIR/openemr.tar.gz'."
170 exit 1
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?"
185 CHANGES=$RESULT
186 OLDSITEID=$SITEID
188 dlg_blank_line
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
200 OLDOEDIR="$OEDIR"
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 '/'."
212 exit 1
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."
216 exit 1
219 if [ -e "$OEDIR" -a ! -e "$OEDIR/sites" ]; then
220 dlg_msg "Error: Directory '$OEDIR/sites' is missing - old release needs removal?"
221 exit 1
224 if [ -e "$OEDIR/sites/$SITEID" ]; then
225 dlg_msg "Error: Site '$SITEID' already exists in '$OEDIR/sites'."
226 exit 1
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
237 COLLATE=""
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.\""
245 else
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.\""
252 else
253 MARGS="$MARGS \"MySQL will use character set 'utf8' with collation '$COLLATE'.\""
255 MARGS="$MARGS \" \""
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
262 exit 1
265 dlg_blank_line
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:'
272 MYROOTPASS="$RESULT"
274 dlg_blank_line
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 ..."
280 cd $BAKDIR
281 gunzip openemr.sql.gz
282 if [ $? -ne 0 ]; then
283 dlg_msg "Error: Could not decompress '$BAKDIR/openemr.sql.gz'."
284 exit 1
287 if [ -z $COLLATE ]; then
288 TRASH="CREATE DATABASE $OEDBNAME"
289 else
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'."
295 exit 1
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."
304 exit 1
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'."
312 exit 1
314 else
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'."
319 exit 1
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
332 dlg_msg "All done."