forked from mcci-catena/docker-iot-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongodb_backup.sh
65 lines (52 loc) · 3.32 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/bin/bash
#The Shell script will be used for taking backup and send it to S3 bucket.
# TO list all Databases in mongodb databases
DATE1=$(date +%Y%m%d%H%M)
DATE=$(date +%d-%m-%y_%H-%M)
mkdir -p /var/lib/backup/mongodb
#Full Mongodb backup
mongodump --host mongodb:27017 --authenticationDatabase admin -u "$MONGO_INITDB_ROOT_USERNAME" -p "$MONGO_INITDB_ROOT_PASSWORD" -o /var/lib/mongodb-backup/dump
showdb(){
mongo --quiet --host mongodb:27017 --eval "printjson(db.adminCommand('listDatabases'))" -u "$MONGO_INITDB_ROOT_USERNAME" -p "$MONGO_INITDB_ROOT_PASSWORD" | grep -i name | awk -F'"' '{print $4}'
}
showdb > /mongo_dbs.txt
#Backing up the databases listed.
while read -r db
do
echo "Creating backup for $db"
mongodump --host mongodb:27017 --db "$db" --authenticationDatabase admin -u "$MONGO_INITDB_ROOT_USERNAME" -p "$MONGO_INITDB_ROOT_PASSWORD" -o /var/lib/mongodb-backup/
done < "/mongo_dbs.txt"
tar czf /var/lib/backup/mongodb/"${SOURCE_NAME}"_mongodb_db_backup_"${DATE1}".tgz /var/lib/mongodb-backup/. && rsync -avr /var/lib/mongodb/ /root/mongodb_data/ && tar czf /var/lib/backup/mongodb/"${SOURCE_NAME}"_mongodb_data_backup_"${DATE1}".tgz /root/mongodb_data/.
# Moving the backup to S3 bucket
if s3cmd put -r --no-mime-magic /var/lib/backup/mongodb/ s3://"${S3_BUCKET_MONGODB}"/;
then
echo "DATE:" "$DATE" > /tmp/mongodbbackup.txt
echo " " >> /tmp/mongodbbackup.txt
echo "DESCRIPTION: ${SOURCE_NAME}_Mongodb backup" >> /tmp/mongodbbackup.txt
echo " " >> /tmp/mongodbbackup.txt
echo "STATUS: mongodb backup is Successful." >> /tmp/mongodbbackup.txt
echo " " >> /tmp/mongodbbackup.txt
echo "******* Mongodb Database Backup ****************" >> /tmp/mongodbbackup.txt
echo " " >> /tmp/mongodbbackup.txt
s3cmd ls --no-mime-magic s3://"${S3_BUCKET_MONGODB}"/ --human-readable | grep -i "${SOURCE_NAME}"_mongodb_db | cut -d' ' -f3- | tac | head -10 | sed "s,s3:\/\/${S3_BUCKET_MONGODB}\/,,g" &>> /tmp/mongodbbackup.txt
echo " " >> /tmp/mongodbbackup.txt
echo "************** Mongodb data Backup *************" >> /tmp/mongodbbackup.txt
echo " " >> /tmp/mongodbbackup.txt
s3cmd ls --no-mime-magic s3://"${S3_BUCKET_MONGODB}"/ --human-readable | grep -i "${SOURCE_NAME}"_mongodb_data | cut -d' ' -f3- | tac | head -10 | sed "s,s3:\/\/${S3_BUCKET_MONGODB}\/,,g" &>> /tmp/mongodbbackup.txt
echo " " >> /tmp/mongodbbackup.txt
echo "********************** END *********************" >> /tmp/mongodbbackup.txt
else
echo "DATE:" "$DATE" > /tmp/mongodbbackup.txt
echo " " >> /tmp/mongodbbackup.txt
echo "DESCRIPTION: ${SOURCE_NAME}_Mongodb backup" >> /tmp/mongodbbackup.txt
echo " " >> /tmp/mongodbbackup.txt
echo "STATUS: mongodb backup is Failed." >> /tmp/mongodbbackup.txt
echo " " >> /tmp/mongodbbackup.txt
echo "Something went wrong, Please check it" >> /tmp/mongodbbackup.txt
< /tmp/mongodbbackup.txt mail -s "${SOURCE_NAME}: mongodb backup" "${CRON_BACKUP_MAIL}"
fi
# Remove the old backup data in local directory to avoid excessive storage use
find /var/lib/backup/mongodb/ -type f -exec rm {} \;
find /root/mongodb_data/ -type f -exec rm {} \;
find /var/lib/mongodb-backup/ -type f -exec rm {} \;
< /tmp/mongodbbackup.txt mail -s "${SOURCE_NAME}: mongodb backup" "${CRON_BACKUP_MAIL}"