Last active
October 16, 2025 13:29
-
-
Save kmpm/087e3ae1dd8047ab31d05ba8fa49e18e to your computer and use it in GitHub Desktop.
Restore glpi to docker swarm service setup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # back up files and database from a glpi instance running on a debian host | |
| set -e | |
| WORKDIR=./work | |
| DATE=$(date +"%Y%m%d_%H%M%S") | |
| GLPI_HOME=/opt/glpi | |
| DBUSER=glpi_app | |
| DBNAME=glpi | |
| DBBACKUP=${WORKDIR}/glpi.sql | |
| FILEBACKUP=${WORKDIR}/glpi-files.tar | |
| BACKUPFILE="backup-${DATE}.tar.gz" | |
| #if sudo test ! -f "/root/.my.cnf" ; then | |
| # echo "Error: /root/.my.cnf does not exists. Cannot continue" | |
| # exit 1 | |
| #fi | |
| function clean_workdir () { | |
| if [ -d ${WORKDIR} ]; then | |
| rm -Rf ${WORKDIR} | |
| fi | |
| if [ ! -d ${WORKDIR} ]; then | |
| mkdir -p ${WORKDIR} | |
| fi | |
| } | |
| function backup_db () { | |
| echo "Backing up database ${DBNAME}" | |
| mysqldump -u ${DBUSER} -p ${DBNAME} > ${DBBACKUP} | |
| } | |
| function backup_files() { | |
| echo "Backing up files" | |
| sudo tar -cvf ${FILEBACKUP} -C /opt/glpi config files marketplace plugins | |
| } | |
| clean_workdir | |
| backup_db | |
| backup_files | |
| cp do-restore-glpi.sh ${WORKDIR} | |
| echo "Compressing everything to ${BACKUPFILE}" | |
| tar -zvcf ${BACKUPFILE} -C ${WORKDIR} . |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Respores a backup (see do-backup-glpi.se) to db running in a container and files to a docker volume all defined in a stack | |
| # called glpi-app | |
| set -e | |
| WORKDIR=./work | |
| DATE=$(date +"%Y%m%d_%H%M%S") | |
| GLPI_DB_SERVICE=glpi-app_db | |
| GLPI_SERVICE=glpi-app_glpi | |
| DBBACKUP=${WORKDIR}/glpi.sql | |
| FILEBACKUP=${WORKDIR}/glpi-files.tar | |
| BACKUPFILE=$1 | |
| if [ ! -f ${BACKUPFILE} ]; then | |
| echo "Error: file '${BACKUPFILE}' was not found. Cannot continue" | |
| exit 1 | |
| fi | |
| function confirm () { | |
| read -p "$1 Y/n : " -n 1 -r | |
| echo #Optionally move to new line | |
| if [[ ! $REPLY =~ ^[Yy]$ ]]; then | |
| echo "Interrupted by user" | |
| exit 1 | |
| fi | |
| } | |
| function clean_workdir () { | |
| if [ -d ${WORKDIR} ]; then | |
| rm -Rf ${WORKDIR} | |
| fi | |
| if [ ! -d ${WORKDIR} ]; then | |
| mkdir -p ${WORKDIR} | |
| fi | |
| } | |
| done | |
| # get first service instance | |
| DB_INSTANCE=$( docker ps -q -f name=glpi-app_db | head -n1) | |
| OUTPUT=pv | |
| echo "DATABASE=$MYSQL_DATABASE" | |
| echo "USER=$MYSQL_USER" | |
| echo "PWD=$MYSQL_PASSWORD" | |
| echo "INSTANCE=$DB_INSTANCE" | |
| echo "OUTPUT=$OUTPUT" | |
| confirm "Will possibly destroy and leave the database inoperable" | |
| ${OUTPUT} ${DBBACKUP} | docker exec -i $DB_INSTANCE mysql -u $MYSQL_USER --password=$MYSQL_PASSWORD $MYSQL_DATABASE | |
| echo "Database restored" | |
| } | |
| function restore_files() { | |
| echo "Getting volume paths from service $GLPI_SERVICE" | |
| VOLUME_NAME=$(docker service inspect $GLPI_SERVICE | jq '.[0].Spec.TaskTemplate.ContainerSpec.Mounts[] | select(."Target" == "/var/glpi") | ."Source"' -r) | |
| VOLUME_PATH=$(docker volume inspect $VOLUME_NAME | jq -c -r '.[0].Mountpoint') | |
| echo "VOLUME_NAME=${VOLUME_NAME}" | |
| echo "VOLUME_PATH=${VOLUME_PATH}" | |
| if sudo test ! -d ${VOLUME_PATH} ; then | |
| echo "Error: Cannot find path $VOLUME_PATH" | |
| exit 1 | |
| fi | |
| confirm "Will possibly overwrite files" | |
| sudo tar -xvf ${FILEBACKUP} -C ${VOLUME_PATH} | |
| } | |
| function decompress() { | |
| echo "Extracting workfiles from ${BACKUPFILE} to ${WORKDIR}" | |
| tar -zxf ${BACKUPFILE} -C ${WORKDIR} | |
| } | |
| clean_workdir | |
| decompress | |
| restore_db | |
| #restore_files | |
| echo "Done" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # restore directly using the output from do-backup-glpi.sh | |
| set -e | |
| WORKDIR=./work | |
| DATE=$(date +"%Y%m%d_%H%M%S") | |
| GLPI_HOME=/opt/glpi | |
| DBUSER=glpi_app | |
| DBNAME=glpi | |
| DBBACKUP=${WORKDIR}/glpi.sql | |
| FILEBACKUP=${WORKDIR}/glpi-files.tar | |
| BACKUPFILE=$1 | |
| if [ ! -f ${BACKUPFILE} ]; then | |
| echo "Error: file '${BACKUPFILE}' was not found. Cannot continue" | |
| exit 1 | |
| fi | |
| if sudo test ! -f "/root/.my.cnf" ; then | |
| echo "Error: /root/.my.cnf does not exists. Cannot continue" | |
| exit 1 | |
| fi | |
| function confirm () { | |
| read -p "$1 Y/n : " -n 1 -r | |
| echo #Optionally move to new line | |
| if [[ ! $REPLY =~ ^[Yy]$ ]]; then | |
| echo "Interrupted by user" | |
| exit 1 | |
| fi | |
| } | |
| function clean_workdir () { | |
| if [ -d ${WORKDIR} ]; then | |
| rm -Rf ${WORKDIR} | |
| fi | |
| if [ ! -d ${WORKDIR} ]; then | |
| mkdir -p ${WORKDIR} | |
| fi | |
| } | |
| function restore_db () { | |
| if [ ! -f ${DBBACKUP} ]; then | |
| echo "Error: database backup ${DBBACKUP} does not exist. Cannot continue" | |
| exit 1 | |
| fi | |
| echo "Restoring database ${DBNAME} from ${DBBACKUP}" | |
| confirm "Will remove and recreate database" | |
| echo " ... removing existing database" | |
| sudo mysql -e "DROP DATABASE IF EXISTS ${DBNAME};" | |
| echo " ... recreating database" | |
| sudo mysql -e "CREATE DATABASE ${DBNAME} /*\!40100 DEFAULT CHARACTER SET utf8 */;" | |
| sudo mysql -e "CREATE USER IF NOT EXISTS '${DBUSER}'@'localhost' IDENTIFIED BY 'password';" | |
| echo " ... restoring from ${DBBACKUP}" | |
| sudo mysql ${DBNAME} < ${DBBACKUP} | |
| sudo mysql -e "GRANT ALL PRIVILEGES ON ${DBNAME}.* TO '${DBUSER}'@'localhost'; " | |
| sudo mysql -e "FLUSH PRIVILEGES; " | |
| } | |
| function restore_files() { | |
| echo "Restoring files to ${GLPI_HOME}" | |
| confirm "Will possibly overwrite files" | |
| if [ ! -d ${GLPI_HOME} ]; then | |
| sudo mkdir -p ${GLPI_HOME} | |
| fi | |
| sudo tar -xvf ${FILEBACKUP} -C ${GLPI_HOME} | |
| } | |
| function decompress() { | |
| echo "Extracting workfiles from ${BACKUPFILE} to ${WORKDIR}" | |
| tar -zxf ${BACKUPFILE} -C ${WORKDIR} | |
| } | |
| clean_workdir | |
| decompress | |
| restore_db | |
| restore_files | |
| echo "Done" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment