Moving DB from and to AWS Ec2 machine


#1

Hi everyone,
Is there a way to move my DB from meteor web app that is live on AWS EC2 Machine to some where else. I have two cases here. we use MUP to for hosting our project.
a) want to move my local db to AWS
b) want to move my AWS db to somewhere else.
what could be the possible solutions ?
thanks in advance to those who will put any sort of effort.


#2

Can you just do a mongo backup and restore?

https://docs.mongodb.com/manual/tutorial/backup-and-restore-tools/


#3

Ok will try and update you about its status.


#4

Here’s a snippet of a bash script I use to (among other things) do backup and restore from a live server using single commands. I’m also using AWS EC2 with MUP. The commands below dump to/from a local .gz file, they do not transfer your local db to the server (or vice versa), but they could probably be easily modified to do so.

$ ./run.sh dump
$ ./run.sh backup

#!/bin/bash
# ./run.sh
MY_ENV=production
# MY_ENV=staging

if [ "$MY_ENV" = 'production' ]; then
    server=example.com
    user=root
    key=aws_production.pem
else
    server=staging.example.com
    user=root
    key=aws_staging.pem
fi

database=database_name

red=`tput setaf 1`
cyan=`tput setaf 6`
nocol=`tput sgr0`

backup() {
    ssh -i "./.keys/$key" $user@$server "docker exec mongodb mongodump -d $database --archive --gzip" > dump.gz
    echo "${cyan}>>> Data backed up to : $(pwd)/dump.gz"
}


echoWhichServer() {
    [ "$MY_ENV" = 'production' ] && echo -n "${red}" || echo -n "${cyan}"
    echo ">>> Set to ${MY_ENV} server (${server})...${nocol}"
}

restore() {
    echoWhichServer
    read -p "Are you sure you want to overwrite the server data (yes)? "
    echo    # (optional) move to a new line
    if [[ $REPLY =~ ^[Yy][Ee][Ss]$ ]]
    then
        cat dump.gz | ssh -i "./.keys/$key" $user@$server "cat | docker exec -i mongodb mongorestore --archive --gzip --drop"
        echo "${cyan}>>> Data restored"
    fi
}

case "$1" in
    backup|dump)  backup; ;;
    restore) restore; ;;
esac