Refactor and fix server backup script

Fixes: MKUP-297
Fixes: PROD-34217

Change-Id: Ic1a05da0d4cdad6fe0861e76d5e6da330baa60cd
diff --git a/ceph/files/backup/ceph-backup-client-runner-call.sh b/ceph/files/backup/ceph-backup-client-runner-call.sh
index 81e49cf..3a909d9 100644
--- a/ceph/files/backup/ceph-backup-client-runner-call.sh
+++ b/ceph/files/backup/ceph-backup-client-runner-call.sh
@@ -97,10 +97,10 @@
     {%- else %}
     echo "----------------------------"
     echo "Cleanup. Keeping only $KEEP full backups"
-    NUMBER_OF_FULL=`find $BACKUPDIR -maxdepth 1 -mindepth 1 -type d -print| wc -l`
+    NUMBER_OF_FULL=$(find $BACKUPDIR/$HOSTNAME -maxdepth 1 -mindepth 1 -print | wc -l)
     FULL_TO_DELETE=$(( $NUMBER_OF_FULL - $KEEP ))
     if [ $FULL_TO_DELETE -gt 0 ] ; then
-        cd $BACKUPDIR
+        cd $BACKUPDIR/$HOSTNAME
         ls -t | tail -n -$FULL_TO_DELETE | xargs -d '\n' rm -rf
     else
         echo "There are less full backups than required, not deleting anything."
diff --git a/ceph/files/backup/ceph-backup-server-runner.sh b/ceph/files/backup/ceph-backup-server-runner.sh
index 200897c..a164bcc 100644
--- a/ceph/files/backup/ceph-backup-server-runner.sh
+++ b/ceph/files/backup/ceph-backup-server-runner.sh
@@ -6,31 +6,35 @@
 
     BACKUPDIR="{{ backup.backup_dir }}/full"
     KEEP={{ backup.server.full_backups_to_keep }}
-    {%- if backup.server.backup_times is not defined %}
-    HOURSFULLBACKUPLIFE={{ backup.server.hours_before_full }} # Lifetime of the latest full backup in hours
-    if [ $HOURSFULLBACKUPLIFE -gt 24 ]; then
-        FULLBACKUPLIFE=$(( 24 * 60 * 60 ))
-    else
-        FULLBACKUPLIFE=$(( $HOURSFULLBACKUPLIFE * 60 * 60 ))
-    fi
-    {%- endif %}
 
 # Cleanup
 # ---------
-{%- if backup.server.backup_times is not defined %}
-echo "----------------------------"
-echo "Cleanup. Keeping only $KEEP full backups"
-AGE=$(($FULLBACKUPLIFE * $KEEP / 60))
-find $BACKUPDIR -maxdepth 1 -type d -mmin +$AGE -execdir echo "removing: "$BACKUPDIR/{} \; -execdir rm -rf $BACKUPDIR/{} \;
-{%- else %}
-echo "----------------------------"
-echo "Cleanup. Keeping only $KEEP full backups"
-NUMBER_OF_FULL=`find $BACKUPDIR -maxdepth 1 -mindepth 1 -type d -print| wc -l`
-FULL_TO_DELETE=$(( $NUMBER_OF_FULL - $KEEP ))
-if [ $FULL_TO_DELETE -gt 0 ] ; then
-    cd $BACKUPDIR
-    ls -t | tail -n -$FULL_TO_DELETE | xargs -d '\n' rm -rf
+echo "Cleanup started. Keeping only $KEEP full backups for every subdir."
+echo "============================"
+BACKUP_SUBDIRS=$(find /srv/volumes/backup/ceph/full -maxdepth 1 -mindepth 1 -type d)
+if [ {% raw %}${#BACKUP_SUBDIRS}{% endraw %} -gt 0 ] ; then
+    for SUBDIR in $BACKUP_SUBDIRS
+    do
+        echo "Cleaning $SUBDIR"
+        NUMBER_OF_FULL=$(find $SUBDIR -maxdepth 1 -mindepth 1 -print | wc -l)
+        FULL_TO_DELETE=$(( $NUMBER_OF_FULL - $KEEP ))
+        if [ $FULL_TO_DELETE -gt 0 ] ; then
+            cd $SUBDIR
+            TO_DELETE=$(ls -t | tail -n -$FULL_TO_DELETE | xargs -d '\n')
+            echo "Folowing backups will be deleted: $TO_DELETE"
+            ls -t | tail -n -$FULL_TO_DELETE | xargs -d '\n' rm -rf
+        else
+            echo "There are less full backups than required, not deleting anything."
+        fi
+        echo "----------------------------"
+    done
 else
-    echo "There are less full backups than required, not deleting anything."
+    NUMBER_OF_FULL=$(find $BACKUPDIR -maxdepth 1 -mindepth 1 -print| wc -l)
+    FULL_TO_DELETE=$(( $NUMBER_OF_FULL - $KEEP ))
+    if [ $FULL_TO_DELETE -gt 0 ] ; then
+        cd $BACKUPDIR
+        ls -t | tail -n -$FULL_TO_DELETE | xargs -d '\n' rm -rf
+    else
+        echo "There are less full backups than required, not deleting anything."
+    fi
 fi
-{%- endif %}
\ No newline at end of file