opt-cc/backup/backup.sh

40 lines
1009 B
Bash
Executable File

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