Skip to content

Commit

Permalink
Fix database restoring when database didn't exist.
Browse files Browse the repository at this point in the history
  • Loading branch information
spAnser authored Sep 6, 2017
1 parent 08fc6a3 commit 6e02591
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions root/data/restore-db.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
db_name="{%= domain_name %}_cmsdb_dev"
latest="./cms-latest-backup.sql"

create_db ()
{
db_exists=`mysql -u root --skip-column-names -e "show databases like '$db_name'"`
if [ "$db_exists" != "$db_name" ]; then
mysql -u root -e "create database $db_name character set UTF8 collate utf8_bin"
fi
}

database_imported="False"
while [[ ${database_imported} == "False" ]]; do
files=($(find -E . -type f -regex "^./backup/.*$"))
Expand All @@ -23,6 +31,7 @@ while [[ ${database_imported} == "False" ]]; do

# if input == length of backup files list they must've picked latest
if [ "${Choice}" -eq "${#files[@]}" ]; then
create_db
mysql -uroot $db_name < $latest
database_imported="True"
# else import the user choice
Expand All @@ -31,10 +40,7 @@ while [[ ${database_imported} == "False" ]]; do
if [ "${Choice}" -gt "${#files[@]}" ]; then
echo 'Please choose a number from the list.'
else
db_exists=`mysql -u root --skip-column-names -e "show databases like '$db_name'"`
if [ "$db_exists" != "$db_name" ]; then
mysql -u root -e "create database $db_name character set UTF8 collate utf8_bin"
fi
create_db
mysql -uroot $db_name < ${files[$Choice]}
database_imported="True"
fi
Expand Down

0 comments on commit 6e02591

Please sign in to comment.