From 8cedacf97c1c2616182e0fece2eda5aa568507f5 Mon Sep 17 00:00:00 2001
From: David Beniamine <david.beniamine@tetras-libre.fr>
Date: Wed, 20 Nov 2024 17:16:06 +0100
Subject: [PATCH] Backup and upgrade commands

---
 .gitignore |  2 ++
 backup.sh  | 12 +++++++++++
 glpi       | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++--
 3 files changed, 70 insertions(+), 2 deletions(-)
 create mode 100755 backup.sh

diff --git a/.gitignore b/.gitignore
index f8a2c9a..63fca78 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,4 @@
 .*.sw?
 .env
+backups/
+patch
diff --git a/backup.sh b/backup.sh
new file mode 100755
index 0000000..d867951
--- /dev/null
+++ b/backup.sh
@@ -0,0 +1,12 @@
+#!/bin/bash
+echo "Backing up glpi DB"
+DIR=$(dirname $0)
+echo $DIR
+cd $DIR
+mkdir -p backups
+out="backups/mysqldump_$(date +%Y%m%d_%H%M).sql.gz"
+. .env
+docker-compose exec -T db mariadb-dump --all-databases -u root -p$MYSQL_ROOT_PASSWORD  --default-character-set=utf8 | gzip > $out
+ls -lh $out
+echo "Done"
+
diff --git a/glpi b/glpi
index 1dc4036..21255e1 100755
--- a/glpi
+++ b/glpi
@@ -12,6 +12,8 @@ is_docker() {
 usage() {
     echo -e "Usage $0 <command> [args]\n"
     echo -e "COMMANDS\n"
+    echo "backup"
+    echo -e "\tcreate a database backup"
     echo "bash"
     echo -e "\topens a bash terminal in front container or just run bash"
     echo "cache"
@@ -28,6 +30,8 @@ usage() {
     echo -e "\t restart the docker stack or apache2"
     echo "up"
     echo -e "\t starts the stack"
+    echo "upgrade"
+    echo -e "\tupgrades glpi"
 }
 
 if [ "$(is_docker)" -eq 1 ]; then
@@ -48,10 +52,58 @@ mysql="mysql -u $user $db -p$pass"
 
 DIR="$(dirname $0)"
 
+upgrade_glpi() {
+    prefix="/var/www/html"
+    # Check if current version si the latest
+    cur_vers=$(echo $($cmd find $prefix/version/ -type f) | cut -d '/' -f 6 | tr -d '\r')
+    echo "Current GLPI version $cur_vers"
+    backup_dir="$prefix/../glpi-$cur_vers"
+    latest_ver=$(curl -s -L https://github.com/glpi-project/glpi/releases/latest | grep '<h1' | tail -n 1 | sed -e 's/<[^>]*>//g' -e 's/\s*//g')
+    echo "Latest GLPI version $latest_ver"
+    extract_dir="$prefix/../glpi-$latest_ver"
+    if [ $cur_vers == $latest_ver ]; then
+        echo "GLPI is up to date, nothing to do"
+        return
+    fi
+    if [ "$(echo $cur_vers | cut -d . -f 1)" -ne "$(echo $latest_ver | cut -d . -f 1)" ]; then
+        read -p "New version $latest_ver is a major upgrade do you want to upgrade ? (yes/no)" $answer
+        if [ "$answer" != "yes" ]; then
+            echo "Aborting"
+            return
+        fi
+    fi
+    # Backup DB
+    $0 backup
+    # 2 Save old files
+    $cmd mkdir $backup_dir
+    $cmd sh -c "rm -rf $backup_dir/*"
+    $cmd sh -c "mv $prefix/* $backup_dir/"
+    $cmd ls $backup_dir
+    # 3 Download glpi files
+    $cmd mkdir -p $extract_dir
+    $cmd ls $extract_dir
+    $cmd curl -L -o glpi_$latest_ver.tar.gz https://github.com/glpi-project/glpi/releases/download/$latest_ver/glpi-$latest_ver.tgz
+    $cmd ls $extract_dir
+    $cmd tar xvzf glpi_$latest_ver.tar.gz -C $extract_dir
+    $cmd ls $extract_dir
+    $cmd sh -c "mv $extract_dir/glpi/* $prefix/"
+    $cmd ls $extract_dir
+    # 4 Restore  plugins
+    $cmd sh -c "cp -rv $backup_dir/plugins/* $prefix/plugins/"
+    # 5 run php bin/console db:update
+    $0 console db:update
+    # Activate plugins
+    $0 console plugin:install --username=glpi --all
+    $0 console plugin:activate --all
+}
+
 action=$1
 shift
 # Keep actions sorted
 case $action in
+    "backup")
+        ./backup.sh
+        ;;
     "bash")
         $cmd bash
         ;;
@@ -76,10 +128,12 @@ case $action in
     "up")
         $cmdup $@
         ;;
+    "upgrade")
+        upgrade_glpi
+        ;;
     *)
-        echo "ERROR: No command given"
+        echo "ERROR: unknown command : '$action'"
         usage
         exit 1
         ;;
 esac
-
-- 
GitLab