-
Notifications
You must be signed in to change notification settings - Fork 2
/
mongo-s3-backup.sh
executable file
·39 lines (28 loc) · 1.02 KB
/
mongo-s3-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
#!/bin/sh
# Current time
TIME=`/bin/date +%d-%m-%Y-%T`
# Backup directory
DEST=/app/tmp
# Tar file of backup directory
TAR=$DEST/$TIME.tar
# Create backup dir (-p to avoid warning if already exists)
/bin/mkdir -p $DEST
# Log
echo "Backing up $MONGO_HOST:$MONGO_PORT/$MONGO_DATABASE to s3://$AWS_MONGO_BACKUP_PATH/ on $TIME";
# Dump from mongodb host into backup directory
if [ -z ${MONGO_USERNAME+x} ] || [ -z ${MONGO_PASSWORD+x} ] || [ "$MONGO_USERNAME" = "" ] || [ "$MONGO_PASSWORD" = "" ]
then
mongodump --gzip --host $MONGO_HOST --port $MONGO_PORT -d $MONGO_DATABASE -o $DEST
else
mongodump --gzip --host $MONGO_HOST --port $MONGO_PORT --username $MONGO_USERNAME --password $MONGO_PASSWORD -d $MONGO_DATABASE -o $DEST
fi
# Create tar of backup directory
/bin/tar cvf $TAR -C $DEST .
# Upload tar to s3
aws s3 cp $TAR s3://$AWS_MONGO_BACKUP_PATH/
# Remove tar file locally
/bin/rm -f $TAR
# Remove backup directory
/bin/rm -rf $DEST
# All done
echo "Backup available at https://s3.amazonaws.com/$AWS_MONGO_BACKUP_PATH/$TIME.tar"