-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.sh
executable file
·88 lines (74 loc) · 1.87 KB
/
run.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/bin/bash
set -e
#
# AWS configs
#
if [ -z "$AWS_ACCESS_KEY_ID" ]; then
echo "AWS_ACCESS_KEY_ID must be set"
exit 1
fi
if [ -z "$AWS_SECRET_ACCESS_KEY" ]; then
echo "AWS_SECRET_ACCESS_KEY must be set"
exit 1
fi
if [ -z "$S3_BUCKET" ]; then
echo "S3_BUCKET must be set"
exit 1
fi
#
# Mongo configs
#
if [ -z "$MONGO_HOST" ]; then
# default to a linked container with name "mongo"
MONGO_HOST="mongo"
fi
if [[ "$MONGO_DATABASE" ]]; then
MONGO_HOST+=" --db $MONGO_DATABASE"
fi
#
# backup configs
#
if [ -z "$DATE_FORMAT" ]; then
DATE_FORMAT="%Y%m%d_%H%M%S"
fi
if [ -z "$FILE_PREFIX" ]; then
FILE_PREFIX=""
fi
function restore {
aws s3api get-object --bucket $S3_BUCKET --key $FILE /backup/$FILE
tar -zxvf /backup/$FILE -C /backup
mongorestore --drop --host $MONGO_HOST $MONGORESTORE_FLAGS dump/
rm -rf dump/ /backup/$FILE
}
# backup
if [ "$1" == "backup" ]; then
printf "\nStarting backup...\n\n"
DATE=$(date +$DATE_FORMAT)
FILENAME=$FILE_PREFIX$DATE.tar.gz
FILE=/backup/$FILENAME
mongodump --host $MONGO_HOST $MONGODUMP_FLAGS
tar -zcvf $FILE dump/
printf "\nUploading $FILENAME...\n\n"
aws s3api put-object --bucket $S3_BUCKET --key $FILENAME --body $FILE
rm -rf dump/ $FILE
# list backups
elif [ "$1" == "list" ]; then
printf "\nRetrieving backups from $S3_BUCKET...\n\n"
aws s3api list-objects --bucket $S3_BUCKET --query 'Contents[].{Key: Key, Size: Size}' --output table
# restore
elif [ "$1" == "restore" ]; then
if [ -z "$2" ]; then
echo "Name of a dump to restore is required"
exit 1
fi
if [ "$2" == "latest" ]; then
printf "\nDetermining backup to restore...\n\n"
: ${FILE:=$(aws s3 ls s3://$S3_BUCKET | awk -F " " '{print $4}' | grep ^$FILE_PREFIX | sort -r | head -n1)}
printf "\nStarting restore of $FILE...\n"
restore
else
FILE=$2
printf "\nStarting restore of $FILE...\n\n"
restore
fi
fi