DEV Community

loading...

Dump MongoDB daily and upload the backup on AWS S3 in Ubuntu with AWS CLI

Bogdan Alexandru Militaru
Get whatever you want from me [here](https://boobo94.github.io/)
Originally published at boobo94.github.io on ・1 min read

Install AWS CLI on Ubuntu

$ sudo apt-get update
$ sudo apt-get install awscli

Enter fullscreen mode Exit fullscreen mode

AWS Credentials

To configure AWS Credentials

$ aws configure

Enter fullscreen mode Exit fullscreen mode

Then you can check ~/.aws/credentials and ~/.aws/config to see the content.

Credentials file

$ cat ~/.aws/credentials

[default]
aws_access_key_id = YOUR_ACCESS_KEY
aws_secret_access_key = YOUR_SECRET_KEY

Enter fullscreen mode Exit fullscreen mode

Config file

$ cat ~/.aws/config

[default]
output = json
region = eu-central-1

Enter fullscreen mode Exit fullscreen mode

Backup files

First, we need to create a folder and within add two files

Got to the root

$ cd

Enter fullscreen mode Exit fullscreen mode

Create a new folders bin and logs

$ mkdir bin logs

Enter fullscreen mode Exit fullscreen mode

Add a new file to backup the database to S3

$ vi bin/db-backups.sh

Enter fullscreen mode Exit fullscreen mode

And add the content

backup_name=~/db_backups-`date +%Y-%m-%d-%H%M`
mongodump --host localhost --port 27017 --authenticationDatabase admin -u ADMIN_USER -p YOUR_PASSWORD --out $backup_name
tar czf $backup_name.tar.gz $backup_name
aws s3 cp $backup_name.tar.gz s3://YOUR_PATH_HERE
rm -rf $backup_name
rm $backup_name.tar.gz

Enter fullscreen mode Exit fullscreen mode

The second file needs to upload the logs to S3

$ vi bin/log-backup.sh 

Enter fullscreen mode Exit fullscreen mode

Add the content

root_folder=/home/ubuntu
backup_name=~/log_backups-`date +%Y-%m-%d-%H%M`
tar czf $backup_name.tar.gz $root_folder/logs
aws s3 cp $backup_name.tar.gz s3://YOUR_PATH_HERE
for f in $root_folder/logs/*.log; do :> $f; done
rm $backup_name.tar.gz

Enter fullscreen mode Exit fullscreen mode

The last step add the following script in crontab

$ crontab -e

Enter fullscreen mode Exit fullscreen mode

At the end of the file add

0 0 * * * /bin/bash ~/bin/db-backups.sh >> ~/logs/db_backups.log 2>&1
0 0 * * * /bin/bash ~/bin/log-backup.sh >> ~/logs/log_backup.log 2>&1

Enter fullscreen mode Exit fullscreen mode

Discussion (0)