From 909767fecf791e9d29f89d89b6b995c1d874d18e Mon Sep 17 00:00:00 2001 From: Florian Hartwich Date: Sat, 15 Jul 2017 11:26:33 +0200 Subject: [PATCH] Optimize backup script --- backup/backup.sh | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/backup/backup.sh b/backup/backup.sh index 9c31851..389f322 100755 --- a/backup/backup.sh +++ b/backup/backup.sh @@ -1,4 +1,14 @@ -#!/bin/bash +#!/usr/bin/env bash + +deleteOldest() { + line_num=0 + while IFS= read -r -d $'\0'; do + if [ $line_num == 1 ]; then + $(rm -rf ${REPLY}) + fi + ((line_num++)) + done < <(find -maxdepth 1 -type d -print0 | sort -z) +} # array of available collection names col=(app_user awarding decoration rank squad user promotion player war) @@ -10,6 +20,9 @@ do then mongoexport --db cc --collection $i --out $(date '+%Y-%m-%d')/collections/${i}.json; cp -R ../api/resource $(date '+%Y-%m-%d')/ + if [ $(ls -l | grep -c ^d) -gt 5 ]; then + deleteOldest + fi else mongoimport --db cc --collection $i --drop --file ${1}/collections/${i}.json rm -rf ../api/resource