-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmongodb-backup.sh
executable file
·44 lines (35 loc) · 1.25 KB
/
mongodb-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
#!/bin/bash
# source ./send-notification.sh
# Settings
PROJECT_ID="$PROJECT_ID"
DB_HOST="$MONGO_HOST"
DB_USER="$MONGO_USER"
DB_PASS="$MONGO_PASS"
BUCKET_NAME="$BUCKET"
AUTHDB="$AUTHDB"
# Path in which to create the backup (will get cleaned later)
BACKUP_PATH="/tmp/dump/"
CURRENT_DATE=$(date +"%Y%m%d-%H%M")
# Backup filename
BACKUP_FILENAME="$CURRENT_DATE.tar.gz"
# Create the backup
if [ -z "$DB_PASS" ]; then
mongodump "$DB_HOST" -o "$BACKUP_PATH"
else
mongodump "$DB_HOST" -u "$DB_USER" -p "$DB_PASS" -o "$BACKUP_PATH" --authenticationDatabase="$AUTHDB"
fi
cd $BACKUP_PATH || exit
# if [ -z "$DB_PASS" ]; then
# mongodump "$DB_HOST" -o "$BACKUP_PATH" || send_notification "DB Backup failed on $PROJECT_ID/$DB_NAME"
# else
# mongodump "$DB_HOST" -u "$DB_USER" -p "$DB_PASS" -o "$BACKUP_PATH" --authenticationDatabase="$AUTHDB" || send_notification "DB Backup failed on $PROJECT_ID/$DB_NAME"
# fi
# cd $BACKUP_PATH || exit
# Archive and compress
tar -cvzf "$BACKUP_PATH""$BACKUP_FILENAME" ./*
# Copy to Google Cloud Storage
echo "Copying $BACKUP_PATH$BACKUP_FILENAME to gs://$BUCKET_NAME/$DB_NAME/"
/root/gsutil/gsutil cp "$BACKUP_PATH""$BACKUP_FILENAME" gs://"$BUCKET_NAME"/"$DB_NAME"/ 2>&1
echo "Copying finished"
echo "Removing backup data"
rm -rf $BACKUP_PATH*