Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
compose.libre.sh
Manage
Activity
Members
Labels
Plan
Issues
18
Issue boards
Milestones
Wiki
Code
Merge requests
3
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
libre.sh
compose.libre.sh
Merge requests
!186
WIP: Resolve "Restore from legacy backup"
Code
Review changes
Check out branch
Download
Patches
Plain diff
Open
WIP: Resolve "Restore from legacy backup"
182-restore-from-legacy-backup
into
master
Overview
0
Commits
27
Pipelines
0
Changes
2
Open
Michel Memeteau
requested to merge
182-restore-from-legacy-backup
into
master
5 years ago
Overview
0
Commits
27
Pipelines
0
Changes
1
Expand
Closes
#182
👍
0
👎
0
Merge request reports
Viewing commit
02e9c4e4
Prev
Next
Show latest version
1 file
+
14
−
13
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
02e9c4e4
Restore: bugs correction about archive and database restore
· 02e9c4e4
Benjamin
authored
5 years ago
utils/restore_app.sh
0 → 100755
+
156
−
0
Options
#!/bin/bash
#=== 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
#- Usage:
# * Argument: application's name, database container's ID/NAME, database engine
# * The script has to be ran with sudo in order to be able to write in volume's directories.
# * 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: sudo ./restore_app.sh APLICATION_NAME(piwigo/prestashop/nextcloud) DATABASE_CONTAINER_ID/NAME [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'
#-- Restore:
db_informations
restore_db
restore_data
update_app_config
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 already in variables without regexp:
source
.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
"
DB_ROOT_USER
=
"root"
DB_ROOT_PWD
=
"
$MYSQL_ROOT_PASSWORD
"
;;
"pgsql"
)
#- // TO TEST //#
DB_NAME
=
"
$POSTGRES_DB
"
DB_USER
=
"
$POSTGRES_USER
"
DB_PWD
=
"
$POSTGRES_PASSWORD
"
;;
esac
}
#--- Restore database:
function
restore_db
()
{
echo
"*** Database restore from sql dump ***"
#-- Restore database from sql archive:
#- Database container name is 'db' and it's assumed that it is define like that in app's docker-compose.yml file.
case
$DB_ENGINE
in
"mysql"
|
"mariadb"
)
docker-compose
exec
-T
db bash
-c
"mysql -u '"
$DB_ROOT_USER
"' --password='"
${
DB_ROOT_PWD
}
"' '"
$DB_NAME
"'"
<
${
DB_NAME
}
.sql
#docker exec -i $CONT_DB_ID bash -c "mysql -u '"$DB_ROOT_USER"' -p${DB_ROOT_PWD} '"$DB_NAME"'" < ${DB_NAME}.sql
#cat ${DB_NAME}.sql | docker exec -i $CONT_DB_ID /usr/bin/mysql '"$DB_NAME"' -u '"$DB_ROOT_USER"' --password='"$DB_ROOT_PWD"'
;;
"pgsql"
)
#- // TO TEST //#
docker-compose
exec
-T
db bash
-c
"PGPASSWORD='"
$DB_PWD
"' psql -U '"
$DB_USER
"' '"
$DB_NAME
"'"
<
${
DB_NAME
}
.sql
#docker exec -i $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! ***"
}
#--- Restore data files:
function
restore_data
()
{
echo
"*** Restore application data from
${
APP_NAME
}
_*.tgz to container directory ***"
echo
"** Start uncompressing archive(s) (be patient)... **"
cd
$APP_DIR
for
archfile
in
../
*
.tgz
;
do
echo
"Uncompress
$archfile
..."
tar
-xzf
$archfile
done
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 databse's name in docker-compose.yml, assummed 'db'. See docker-compose.yml for "config" volume name:
#sed -i -e 's/localhost/db/' "$DATA_DIR/config/database.inc.php"
sed
-i
-e
's/localhost/$CONT_DB_ID/'
"
$DATA_DIR
/local/config/database.inc.php"
mv
$DATA_DIR
/local/config/config.inc.php
$DATA_DIR
/local/config/config.inc.php.old
;;
"prestashop"
)
#-- Todo:
#Delete the admin dir if exists
#Change db name and mysql root password in config/settings.inc.php
;;
"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 ==================================================================#
Loading