forked from firstandthird/mongobackup-s3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup
executable file
·49 lines (38 loc) · 1.16 KB
/
backup
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
#!/bin/bash
if [ -z "$AWS_ACCESS_KEY_ID" ]; then
echo "AWS_ACCESS_KEY_ID must be set"
fi
if [ -z "$AWS_SECRET_ACCESS_KEY" ]; then
echo "AWS_SECRET_ACCESS_KEY must be set"
fi
if [ -z "$S3BUCKET" ]; then
echo "S3BUCKET must be set"
fi
if [ -z "$DATEFORMAT" ]; then
DATEFORMAT='%Y%m%d_%H%M%S'
fi
if [ -z "$FILEPREFIX" ]; then
FILEPREFIX=''
fi
if [ "$1" == "backup" ]; then
echo "Starting backup..."
DATE=$(date +$DATEFORMAT)
FILENAME=$FILEPREFIX$DATE.tar.gz
FILE=/backup/backup-$FILENAME
mongodump --quiet -h $MONGO_PORT_27017_TCP_ADDR -p $MONGO_PORT_27017_TCP_PORT
tar -zcvf $FILE dump/
aws s3api put-object --bucket $S3BUCKET --key $FILENAME --body $FILE
echo "Cleaning up..."
rm -rf dump/ $FILE
elif [ "$1" == "list" ]; then
echo "Listing backups..."
aws s3api list-objects --bucket $S3BUCKET --query 'Contents[].{Key: Key, Size: Size}' --output table
else
echo "Starting restore"
FILE=$1
aws s3api get-object --bucket $S3BUCKET --key $FILE /backup/$FILE
tar -zxvf /backup/$FILE -C /backup
mongorestore -h $MONGO_PORT_27017_TCP_ADDR -p $MONGO_PORT_27017_TCP_PORT dump/
echo "Cleaning up..."
rm -rf dump/ /backup/$FILE
fi