Newer
Older
#=== Informations ===================================================================#
#- Goal: restore application's database and data files
#- Author: B. Mauclaire - bm@ekimia.fr - 2019/11/13
#- Licence: GNU Affero General Public License v3.0
# * Argument: application's name, database container's ID, atabase engine
# * 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 application's name, so files' name are generic in this script.
# * Data files will be restored in "data" directory as data file archives contain subdirectory.
#=== Functions ===================================================================#
#--- Print script's help/usage:
function print_help ()
{
echo "Usage: ./restore_app.sh APLICATION_NAME(piwigo/prestashop/nextcloud) CONTAINER_DATABASE_ID [DB_ENGINE(mysql/mariadb/pgsql)]"
#--- 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" | "mariadb")
#- 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"
;;
"pgsql")
#- // TO TEST //#
DB_NAME="$POSTGRES_DB"
DB_USER="$POSTGRES_USER"
DB_PWD="$POSTGRES_PASSWORD"
;;
function restore_db ()
{
echo "*** Database restore from sql dump ***"
#-- Restore database from sql archive:
case $DB_ENGINE in
"mysql" | "mariadb")
#- // TO TEST //#
docker exec -it $CONT_DB_ID bash -c "mysqldump '"$DB_NAME"' -u '"$DB_USER"' -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"'
;;
"pgsql")
#- // TO TEST //#
docker exec -it $CONT_DB_ID bash -c "PGPASSWORD='"$DB_PWD"' psql -U '"$DB_USER"' '"$DB_NAME"'" < ${DB_NAME}.sql
#cat ${DB_NAME}.sql | docker exec -i $CONT_DB_ID PGPASSWORD='"$DB_PWD"' /usr/bin/psql -U '"$DB_USER"' '"$DB_NAME"'
;;
esac
echo "*** Database dump restored! ***"
}
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 be container's id. See docker-compose.yml for "config" volume name:
sed -i -e 's/localhost/$CONT_DB_ID/' "$APP_DIR/config/database.inc.php"
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 ==================================================================#