Fix config formatting
[gitter.git] / scripts / upgrade-data.sh
blob781f4c0f30a03e49995a2ca0ca2d9df163ff2d1f
1 #!/bin/bash
2 set -e
3 set -x
4 MONGO_HOST=$1
5 MONGO_DB=$2
7 if [ -z "$MONGO_HOST" ]; then
8 MONGO_HOST=localhost
9 fi;
11 if [ -z "$MONGO_DB" ]; then
12 MONGO_DB=gitter
13 fi;
15 MONGO_URL=$MONGO_HOST/$MONGO_DB
17 BACKUP_DONE=""
19 SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
21 MD5=$(which md5||which md5sum)
24 find $SCRIPT_DIR/dataupgrades -type f -name "*.sh" -print0 | while read -d $'\0' file
26 md5=$($MD5 "$file")
29 if [[ $(mongo $MONGO_URL --quiet --eval "print(db.dataUpgrades.find({ \"script\": \"$file\", \"md5\": \"$md5\"}).length())") -eq 0 ]]; then
30 if [[ "${SKIP_BACKUP}" != "yes" ]]; then
32 if [ -z "$BACKUP_DONE" ]; then
33 BACKUP_DIR=mongo-backup-$(date "+%Y-%m-%d.%H-%M-%S")
34 mkdir -p "$BACKUP_DIR"
35 mongodump --host $MONGO_HOST --oplog --out "mongo-backup-$(date "+%Y-%m-%d.%H-%M-%S")"
36 BACKUP_DONE="DONE"
41 echo executing "$file"
42 "$file" $MONGO_URL
45 result=$?
47 mongo $MONGO_URL --quiet --eval "db.dataUpgradesExecutions.insert({ \"script\": \"$file\", \"md5\": \"$md5\", executedAt: new Date(), result: \"$result\"})"
49 if [ $result -eq 0 ]; then
50 mongo $MONGO_URL --quiet --eval "db.dataUpgrades.update({ \"script\": \"$file\" }, { \"script\": \"$file\", \"md5\": \"$md5\", executedAt: new Date() }, { \"upsert\": true } )"
51 else
52 exit $result
53 fi;
54 fi;
56 done