Skip to content
restore_app.sh 3.67 KiB
Newer Older
#!/bin/bash

#- Goal: restore application's database and data files
#- Author: B. Mauclaire - bm@ekimia.fr - 2019/11/13
#- Usage:
#  * Argument: application's name, database container's ID
#  * Run in the directory containing vm archives + db.env + docker-compose.yml: so docker app directory's name isn't necessary
#  * Vm archive's name is built with the database's name, so files' name are generic in this script
#  * Data files will be restored in "data" directory as data file archive containes subdirectory.
#=== Functions ===================================================================#

#--- Print script's help/usage:
function print_help () 
{
   echo "Usage: ./restore_piwigo.sh APLICATION_NAME(piwigo/prestashop/nextcloud) CONTAINER_DATABASE_ID [DB_ENGINE(mysql/mariadb/pgsql)]"
   exit 1
}
#--- Main program tasks:
function main_proc () 
{
   echo ""
   echo "***** Restore $APP_NAME application data *****"

   #-- Manage some variable used by the script:
   check_arguments
   DATA_DIR='data'
   docker-compose stop
   
   #-- Restore:
   db_informations
   restore_db
   restore_data
   update_app_config
   
   #-- Restart containers:
   docker-compose restart
   
   echo ""
   echo "***** End of $APP_NAME restore *****"
}


#--- Check APP_NAME argument:
function check_arguments ()
{
   if [ "$APP_NAME" != "piwigo" ] && [ "$APP_NAME" != "prestashop" ] && [ "$APP_NAME" != "nextcloud" ] ; then
      echo "Application's name must be one of piwigo/prestashop/nextcloud."
      exit 1
   fi
}
#--- Filling database variable from db.env:
function db_informations ()
{
   echo "*** Filling database variable from db.env ***"
   #-- Use db.env and pwg.env to get user + pswd allready in varaibles without regexp:
   source ./db.env
   case "$DB_ENGINE" in
      mysql)
         #- Fill the following variables: MYSQL_ROOT_PASSWORD, MYSQL_DATABASE, MYSQL_USER, MYSQL_PASSWORD, PUID, PGID, TZ.
         DB_NAME="$MYSQL_DATABASE"
         DB_USER="$MYSQL_USER"
         DB_PWD="$MYSQL_PASSWORD"
         ;;
      mariab) ;;
      pgsql) ;;
   esac
}
#--- Restore database:
function restore_db ()
{
   echo "*** Database restore from sql dump ***"

   #-- Restore database from sql archive:
   case "$DB_ENGINE" in
      mysql)
         #//// A TESTER ////#
         ## docker exec -it $CONT_DB_ID bash -c "mysqldump $MYSQL_DATABASE -u $ -p${DB_PWD}" < ${DB_NAME}.sql
         cat ${DB_NAME}.sql | docker exec -i $CONT_DB_ID /usr/bin/mysql '"$DB_NAME"' -u '"$DB_USER"' --password='"$DB_PWD"'
         ;;
      mariadb) ;;
      pgsql) ;;
   esac
   
   echo "*** Database dump restored! ***"
}

#--- Restore data files:
function restore_data ()
{
   echo "*** Restore application data from ${DB_NAME}_*.tgz to container directory ***"
   echo "Start uncompressing archive(s) (be patient)..."
   cd $APP_DIR
   #tar xzf ../${DB_NAME}_${APP_DATA1}.tgz
   tar xzf ../*.tgz
   echo "*** Archive(s) *.tgz restored! ***"
}


#--- Update application configuration files with container's setup:
#-- This function depends on application
function update_app_config ()
{
   case "$APP_NAME" in
      piwigo)
         #-- Update db_host name to container's id:
         sed -i -e 's/localhost/$CONT_DB_ID/' "$APP_DIR/config/database.inc.php"
      ;;
      prestashop)
         #-- Todo:
      ;;
      nextcloud)
         #-- Todo:
      ;;
   esac
}



#=== Main program ===================================================================#
#--- Manage arguments:
case $# in
   2) APP_NAME="$1" ; CONT_DB_ID="$2" ; DB_ENGINE="mysql"
      main_proc
      ;;
   3) APP_NAME="$1" ; CONT_DB_ID="$2" ; DB_ENGINE="$3"
      main_proc
      ;;
   *) print_help ;;
esac
#=== End of script ==================================================================#