-
Notifications
You must be signed in to change notification settings - Fork 4
/
backup.sh
60 lines (41 loc) · 1.18 KB
/
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
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
#!/bin/sh
set -e
echo "Backup starting..."
. /root/container_env.sh
export BOTO_PATH=/root/config.boto
MONGODB_HOST=${MONGODB_HOST:-localhost}
MONGODB_PORT=${MONGODB_PORT:-27017}
BACKUP_DIR=${BACKUP_DIR:-/backup}
# Store the current date in YYYY-mm-DD-HHMMSS
DATE=$(date -u "+%F-%H%M%S")
ARCHIVE_NAME="backup-$DATE.tar.gz"
CMD_AUTH_PART=""
mkdir -p $BACKUP_DIR
if [[ ! -z $MONGODB_USER ]] && [[ ! -z $MONGODB_PASSWORD ]]
then
CMD_AUTH_PART="--username=\"$MONGODB_USER\" --password=\"$MONGODB_PASSWORD\" "
fi
if [[ ! -z $MONGODB_DB ]]
then
CMD_DB_PART="--db=\"$MONGODB_DB\" "
fi
if [[ $MONGODB_OPLOG = "true" ]]
then
CMD_OPLOG_PART="--oplog --forceTableScan "
fi
CMD_INCLUDE=""
CMD_EXCLUDE=""
if [[ ! -z $COLLECTION ]]
then
CMD_INCLUDE="--collection $COLLECTION "
fi
if [[ ! -z $EXCLUDE_COLLECTION ]]
then
CMD_EXCLUDE="--excludeCollection $EXCLUDE_COLLECTION "
fi
CMD="mongodump --host=\"$MONGODB_HOST\" --port=\"$MONGODB_PORT\" $CMD_AUTH_PART$CMD_DB_PART$CMD_OPLOG_PART$CMD_INCLUDE$CMD_EXCLUDE--gzip --archive=$BACKUP_DIR/$ARCHIVE_NAME"
echo "Running command: $CMD"
eval "$CMD"
gsutil cp $BACKUP_DIR/$ARCHIVE_NAME "gs://$GCS_BUCKET"
rm $BACKUP_DIR/$ARCHIVE_NAME
echo "Backup done!"