forked from istepanov/docker-mongodump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.sh
34 lines (24 loc) · 869 Bytes
/
backup.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
#!/bin/bash
set -eo pipefail
echo "Job started: $(date)"
DATE=$(date +%Y%m%d_%H%M%S)
if [[ -z "$TARGET_FOLDER" ]]; then
# dump directly to AWS S3
if [[ -z "$TARGET_S3_FOLDER" ]]; then
>&2 echo "If TARGET_FOLDER is null/unset, TARGET_S3_FOLDER must be set"
exit 1
fi
mongodump --uri "$MONGO_URI" --gzip --archive | aws s3 cp - "${TARGET_S3_FOLDER%/}/backup-$DATE.tar.gz"
echo "Mongo dump uploaded to $TARGET_S3_FOLDER"
else
# save dump locally (and optionally to AWS S3)
FILE="$TARGET_FOLDER/backup-$DATE.tar.gz"
mkdir -p "$TARGET_FOLDER"
mongodump --uri "$MONGO_URI" --gzip --archive="$FILE"
echo "Mongo dump saved to $FILE"
if [[ -n "$TARGET_S3_FOLDER" ]]; then
aws s3 cp "$FILE" "$TARGET_S3_FOLDER"
echo "$FILE uploaded to $TARGET_S3_FOLDER"
fi
fi
echo "Job finished: $(date)"