-
Notifications
You must be signed in to change notification settings - Fork 0
/
MongoDB-S3-Backup
73 lines (38 loc) · 1.35 KB
/
MongoDB-S3-Backup
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
66
67
68
69
1. Install S3cmd
sudo apt-get install s3cmd.
2. Configure S3cmd
s3cmd --configure
3. Test S3cmd
s3cmd ls
4. Backup Script
Create a new shell file named as mongo_backup.sh and write the following code in it.
#!/bin/bash
#Force file syncronization and lock writes
mongo admin --eval "printjson(db.fsyncLock())"
MONGODUMP_PATH="/usr/bin/mongodump"
MONGO_DATABASE="Your_db_name" #replace with your database name
TIMESTAMP=`date +%F-%H%M`
S3_BUCKET_NAME="Your_Bucket_Name" #replace with your bucket name on Amazon S3
S3_BUCKET_PATH="mongodb-backups"
# Create backup
$MONGODUMP_PATH -d $MONGO_DATABASE
# Add timestamp to backup
mv dump mongodb-$HOSTNAME-$TIMESTAMP
tar cf mongodb-$HOSTNAME-$TIMESTAMP.tar mongodb-$HOSTNAME-$TIMESTAMP
# Upload to S3
s3cmd put mongodb-$HOSTNAME-$TIMESTAMP.tar s3://$S3_BUCKET_NAME/$S3_BUCKET_PATH/mongodb-$HOSTNAME-$TIMESTAMP.tar
#Unlock database writes
mongo admin --eval "printjson(db.fsyncUnlock())"
#Delete local files
rm -rf mongodb-*
5. Verify the script
bash mongo_backup.sh
6. Automatic backup using Cron
We now want to automate this process of taking the backup of MongoDB to S3.
sudo su
crontab -e
Add the line at the bottom of the the cron file.
It will automatically backup at every day 11:00 P.M
* 23 * * * /home/ubuntu/mongo_backup.sh
#Restore Command for mongoDB
mongorestore /path/<dumpfolder>