opt-cc/backup/backup.sh

53 lines
1.1 KiB
Bash
Raw Normal View History

2017-07-15 11:26:33 +02:00
#!/usr/bin/env bash
2017-07-15 12:03:38 +02:00
# execute script in its location folder
cd $(dirname $0)
2017-07-15 11:26:33 +02:00
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)
}
2017-05-27 23:35:31 +02:00
# array of available collection names
2017-08-27 15:38:12 +02:00
col=(app_user awarding decoration rank squad user promotion player war campaign)
2017-05-27 23:35:31 +02:00
2017-08-27 15:33:04 +02:00
if [ -z "$1" ]
then
DATE=$(date '+%Y-%m-%d')
2017-08-27 15:50:28 +02:00
mkdir -p ${DATE}/resource
2017-08-27 15:57:37 +02:00
cp -R ../api/resource/ ${DATE}/
2017-08-27 15:33:04 +02:00
else
DATE=${1}
tar -xzf ${DATE}.tar.gz
rm -rf ../api/resource
cp -Rv ${DATE}/resource ../api/
fi
2017-05-27 23:35:31 +02:00
for i in "${col[@]}"
do
# provide date for restore process, if data import is needed
if [ -z "$1" ]
2017-05-27 23:35:31 +02:00
then
2017-08-26 18:35:00 +02:00
mongoexport --db cc --collection $i --out ${DATE}/collections/${i}.json;
2017-07-15 11:26:33 +02:00
if [ $(ls -l | grep -c ^d) -gt 5 ]; then
deleteOldest
fi
2017-05-27 23:35:31 +02:00
else
mongoimport --db cc --collection $i --drop --file ${1}/collections/${i}.json
2017-05-27 23:35:31 +02:00
fi
done
2017-08-27 15:33:04 +02:00
if [ -z "$1" ]
then
tar -zcf ${DATE}.tar.gz ${DATE}
fi
2017-08-27 15:35:07 +02:00
rm -rf ${DATE}
2017-08-27 15:33:04 +02:00