#!/usr/bin/env bash # execute script in its location folder cd $(dirname $0) 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 campaign) if [ -z "$1" ] then DATE=$(date '+%Y-%m-%d') cp -R ../api/resource ${DATE}/ else DATE=${1} tar -xzf ${DATE}.tar.gz rm -rf ../api/resource cp -Rv ${DATE}/resource ../api/ fi for i in "${col[@]}" do # provide date for restore process, if data import is needed if [ -z "$1" ] then mongoexport --db cc --collection $i --out ${DATE}/collections/${i}.json; if [ $(ls -l | grep -c ^d) -gt 5 ]; then deleteOldest fi else mongoimport --db cc --collection $i --drop --file ${1}/collections/${i}.json fi done if [ -z "$1" ] then tar -zcf ${DATE}.tar.gz ${DATE} fi rm -rf ${DATE}