Always run backup cleanup even on non-leader nodes since it can change
any moment (outage, maintenance, etc...).

Related-To: JCDA-4782
Related-To: PROD-36108
Change-Id: Ifd6928ec2d17eebd2c042ef00c8fcaad96163436
diff --git a/zookeeper/files/backup/zookeeper-backup-client-runner.sh b/zookeeper/files/backup/zookeeper-backup-client-runner.sh
index 64e5f83..7ea7dfc 100644
--- a/zookeeper/files/backup/zookeeper-backup-client-runner.sh
+++ b/zookeeper/files/backup/zookeeper-backup-client-runner.sh
@@ -2,6 +2,32 @@
 #!/bin/bash
 # Script to backup zookeeper schema and create snapshot of keyspaces
 
+
+function clean_old_backups() {
+if [ $SKIPCLEANUP = false ] ; then
+    {%- if backup.client.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
+    else
+        echo "There are less full backups than required, not deleting anything."
+    fi
+    {%- endif %}
+else
+    echo "----------------------------"
+    echo "-s parameter passed. Cleanup was not triggered"
+fi
+}
+
 SKIPCLEANUP=false
 while getopts "sf" opt; do
   case $opt in
@@ -70,6 +96,7 @@
     if [ $RC -gt 0 ] && [ ! -s "$TMPDIR/$TMPLOG" ]; then
         printf "Not a zookeper leader. This script does backup just on zookeper leader.\n"
         [ "$TMPDIR" != "/" ] && rm -rf "$TMPDIR"
+        clean_old_backups
         exit 0
     else
         # Include the timestamp in the filename
@@ -138,26 +165,5 @@
 
 # Cleanup
 # ---------
-if [ $SKIPCLEANUP = false ] ; then
-    {%- if backup.client.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
-    else
-        echo "There are less full backups than required, not deleting anything."
-    fi
-    {%- endif %}
-else
-    echo "----------------------------"
-    echo "-s parameter passed. Cleanup was not triggered"
-fi
+clean_old_backups
 # Fin.