AWS practical guide

Periodically back up your MongoDB on Amazon S3

Schedule task on your Windows machine running MongoDB to back up to S3

back up mongodb to s3
Concept: Periodic upload of a MongoDB dump to Amazon S3 bucket
  1. Set your backup schedule using Windows Task scheduler to run a PowerShell script
  2. Write up a PowerShell script. This script will need to first import AWS Tools for Powershell, then do a MongoDB dump, compress it, then upload the compressed backup to Amazon S3

Preparation

Create S3 bucket in AWS management console
Create an S3 bucket through the AWS Management Console
create new IAM user with programmatic access
Attach existing policy to iam user
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": "s3:PutObject",
"Resource": "arn:aws:s3:::mymongodbbackupdemo/*"
}
]
}
iam user access key
  1. Ensure that you have PowerShell. If you are running a Windows machine, you are probably fine as PowerShell is included by default, that is, unless you are operating a dinosaur version of Windows. If you are running Linux, then follow through this instructions from Microsoft. Remember to follow it through to the end and don’t forget to set the execution policy. Otherwise your PowerShell script won’t execute at all.
  2. Install AWS Tools for PowerShell. If you are on Windows, then follow through this instructions from AWS. For Linux, go here.

Piece them together

Import-Module “C:\Program Files (x86)\AWS Tools\PowerShell\AWSPowerShell\AWSPowerShell.psd1”
$database = “MyMongoDBName”
$mongohost = “localhost”
$backupPath = ‘C:\Data\MongoRemBackup’
$timestamp = get-date -format yyyyMMddHHmmss
$archivePath = ‘C:\Data\MongoRemBackup\mymongo-’+$timestamp+’.gz’
$s3Bucket = ‘mymongodbbackupdemo
$region = ‘ap-southeast-1’
$accessKey = ‘[Just for demonstration purpose]’
$secretKey = ‘[Just for demonstration purpose]’
& “C:\Program Files\MongoDB\Server\3.2\bin\mongodump.exe” — db $database — archive=”$archivePath” — gzipWrite-S3Object -BucketName $s3Bucket -File $archivePath -Key $archiveFileName -Region $region -AccessKey $accessKey -SecretKey $secretKey
SCHTASKS /CREATE /SC DAILY /TN "MongoDB Backup and Upload to S3" /TR "C:\Scripts\mongoDbS3Backup.ps1" /ST 01:00

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Adi Simon

I.T. Solutions Architecture, Cloud Architecture, Data Architecture