opt-cc/backup/backup.sh

44 lines
1011 B
Bash
Executable File

#!/usr/bin/env bash
# execute script in its location folder
cd $(dirname $0)
# 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')
mkdir -p ${DATE}/resource
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;
else
mongoimport --db cc --collection $i --drop --file ${1}/collections/${i}.json
fi
done
if [ $(ls -lt --hide=backup.sh | wc -l) -gt 14 ]; then
$(rm "$(ls -t --hide=backup.sh | tail -1)")
fi
if [ -z "$1" ]
then
tar -zcf ${DATE}.tar.gz ${DATE}
rm -rf ${DATE}
elif [[ $1 =~ ^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$ ]]
then
rm -rf ${DATE}
fi