-
Notifications
You must be signed in to change notification settings - Fork 3
/
backup.sh
45 lines (37 loc) · 973 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
35
36
37
38
39
40
41
42
43
44
45
#!/bin/bash
source /env.sh
OPTS="--gzip"
DATE=$(date +%Y.%m.%d.%H.%M)
OUTPUT=/backup/$MONGO_HOST-$DATE.archive.gz
echo "MONGO_HOST: $MONGO_HOST"
if [ ! -z "$MONGO_USER" ]
then
OPTS="$OPTS --username $MONGO_USER"
echo "MONGO_USER: $MONGO_USER"
fi
if [ ! -z "$MONGO_PASS" ]
then
OPTS="$OPTS --password $MONGO_PASS"
echo "MONGO_PASS: ********"
fi
if [ ! -z "$MONGO_DB" ]
then
OPTS="$OPTS --db $MONGO_DB"
echo "MONGO_DB: $MONGO_DB"
fi
if [ ! -z "$MONGO_AUTH_DB" ]
then
OPTS="$OPTS --authenticationDatabase $MONGO_AUTH_DB"
echo "MONGO_AUTH_DB: $MONGO_AUTH_DB"
fi
echo "=> Backup started at $DATE"
mongodump --forceTableScan $OPTS --host "$MONGO_HOST" --port "$MONGO_PORT" --archive="$OUTPUT"
if [ -n "$MAX_BACKUPS" ]; then
while [ "$(find /backup -maxdepth 1 | wc -l)" -gt "$MAX_BACKUPS" ]
do
TARGET=$(find /backup -maxdepth 1 | sort | head -n 1)
rm -rf "$TARGET"
echo "Backup $TARGET is deleted"
done
fi
echo "=> Backup done: $OUTPUT"