From 7e756a55ea959d6d4681776f19f6376bad3fdf70 Mon Sep 17 00:00:00 2001
From: Antoine Barbare <antoine.barbare@etu.utc.fr>
Date: Sun, 18 Feb 2018 14:49:47 +0100
Subject: [PATCH] Modify mongo script to compress result and be compliant with
 backup rotation

---
 pica-backup/mongo-run.sh | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)

diff --git a/pica-backup/mongo-run.sh b/pica-backup/mongo-run.sh
index 934dfb9d..5485d2f5 100755
--- a/pica-backup/mongo-run.sh
+++ b/pica-backup/mongo-run.sh
@@ -9,10 +9,10 @@ BACKUP_FOLDER=${BACKUP_FOLDER:-"/backup/"}
 
 if [[ -z "${MONGO_USER}" && -z "${MONGO_PASS}" ]]; then
   BACKUP_CMD="mongodump -d ${MONGO_DB} -h ${MONGO_HOST}:${MONGO_PORT} -o $BACKUP_FOLDER"'${BACKUP_NAME}'
-  RESTORE_CMD="mongorestore -d ${MONGO_DB} -h ${MONGO_HOST}:${MONGO_PORT} $BACKUP_FOLDER"'${BACKUP_NAME}'
+  RESTORE_CMD="mongorestore -d ${MONGO_DB} -h ${MONGO_HOST}:${MONGO_PORT} "
 else
   BACKUP_CMD="mongodump -d ${MONGO_DB} -h ${MONGO_HOST}:${MONGO_PORT} -u ${MONGO_USER} -p ${MONGO_PASS} -o $BACKUP_FOLDER"'${BACKUP_NAME}'
-  RESTORE_CMD="mongorestore -d ${MONGO_DB} -h ${MONGO_HOST}:${MONGO_PORT} -u ${MONGO_USER} -p ${MONGO_PASS} $BACKUP_FOLDER"'${BACKUP_NAME}'
+  RESTORE_CMD="mongorestore -d ${MONGO_DB} -h ${MONGO_HOST}:${MONGO_PORT} -u ${MONGO_USER} -p ${MONGO_PASS} "
 fi
 
 ##########################
@@ -30,6 +30,10 @@ BACKUP_NAME=\$(date +\%Y.\%m.\%d.\%H\%M\%S)
 
 echo "=> ${MONGO_SERVICE_NAME}: Backup started: \${BACKUP_NAME}"
 if ${BACKUP_CMD} ;then
+    MONGO_BACKUP=$BACKUP_FOLDER\${BACKUP_NAME}
+    echo " => Compress files \$MONGO_BACKUP.tar.gz"
+    tar -czvf \$MONGO_BACKUP.tar.gz \$MONGO_BACKUP && \
+    rm -rf \$MONGO_BACKUP && \
     echo "${MONGO_SERVICE_NAME}:  Backup succeeded"
 else
     echo "${MONGO_SERVICE_NAME}: Backup failed"
@@ -51,11 +55,15 @@ cat <<EOF >> /$restore_script_name
 #!/bin/bash
 
 echo "=> ${MONGO_SERVICE_NAME}: Restore database from \$1"
-if ${RESTORE_CMD} ;then
-    echo "${MONGO_SERVICE_NAME}: Restore succeeded"
+echo "  => Uncompress save \$1"
+tar -xzvf \$1
+output="\$(echo \$1 | awk -F'.tar.gz' '{print \$1}')"
+if mongorestore -d wekan -h wekan-db2:27017 \$output;then
+      echo "wekan: Restore succeeded"
 else
-    echo "${MONGO_SERVICE_NAME}: Restore failed"
+      echo "wekan: Restore failed"
 fi
+rm -Rf \$output
 EOF
 chmod +x /$restore_script_name
 
-- 
GitLab