Mongorestore, from meteor production server to local
Asked Answered
P

6

23

I've found plenty of good instructions on how to use mongodump and mongorestore, to back up my meteor production server and restore the backup if need be:

meteor mongo --url myApp.meteor.com

mongodump -u client -h production-db-b2.meteor.io:27017 -d myApp_meteor_com -out dump/2014_10_21 -p [password from meteor mongo --url]

mongorestore -u client -h production-db-b2.meteor.io:27017 -d myApp_meteor_com dump/2014_10_21_v2/myApp_meteor_com -p [password from meteor mongo --url]

What I haven't found is an explanation of is how to restore a backup-dump to my local meteor app. I have a mongodump output in my app folder. I'm not sure if I can use mongorestore or if there's something else I'm supposed to be doing.

Pratique answered 21/10, 2013 at 21:59 Comment(0)
S
39

The easiest way that I found:

  1. cd in your project and execute meteor command
  2. in another terminal:

mongorestore -h 127.0.0.1 --port 3001 -d meteor dump/meteor

change 127.0.0.1 if your localhost has different ip address and 3001 to a port you have mongodb on (it is usually 3001 or 3002, so try both), dump/meteor is the path to a dump you created previously.

Also the easiest way to export local db:

  1. cd in your project and execute meteor command
  2. In another terminal:

mongodump -h 127.0.0.1 --port 3001 -d meteor

again, change localhost ip and port if needed. . As a result, the dump/meteor folder with db files will be created in the folder you cd before running mongodump.

Good luck.

Stroll answered 21/1, 2015 at 11:43 Comment(2)
Just to add, if you don't have mongo installed on your mac, you can do this: brew install mongoLorileelorilyn
Check out the port number, Your local meteor application is using — it is located in file .meteor/local/db/METEOR-PORT.Sula
P
6

To accomplish the opposite, sending local app data to production app, I wrote this little shell script. It has been useful while I am developing locally and just getting the demo synced for the client to view. Note it has --drop at the end which will overwrite your production database, use with care!

It takes care of the client, pw, and server data from meteor mongo --url ... which expires after 1 minute and is really annoying to try to copy-paste within that time.

#!/usr/bin/env bash

mongodump -h 127.0.0.1:3001 -d meteor -o ~/www/APPNAME/server/dump
IN=`meteor mongo --url APPNAME.meteor.com`
client=`echo $IN | awk -F'mongodb://' '{print $2}' | awk -F':' '{print $1}'`
echo $client
pw=`echo $IN | awk -F':' '{print $3}' | awk -F'@' '{print $1}'`
echo $pw
serv=`echo $IN | awk -F'@' '{print $2}' | awk -F'/' '{print $1}'`
echo $serv
mongorestore -u $client -h $serv -d APPNAME_meteor_com dump/meteor -p $pw --drop
Perilymph answered 14/5, 2014 at 4:38 Comment(0)
F
5

This is what I do:

I. Create a mongo dump in the server

    DATE=$(date +%m%d%y_%H.%M);
    mongodump --host localhost -d APPNAME -o /tmp/APPNAME_$DATE 
    tar -cjvvf /tmp/APPNAME_$DATE.tar.bz2 /tmp/APPNAME_$DATE

II. Download the dump in the development machine and unpack in /tmp


    scp root@$HOST:/tmp/APPNAME_$DATE.tar.bz2 /tmp/
    cp /tmp/APPNAME_$DATE.tar.bz2 .
    mkdir -p /tmp/APPNAME_$DATE 
    cd /tmp/APPNAME_$DATE 
    tar -xjvf /tmp/APPNAME_$DATE.tar.bz2

III. Update local meteor development database


    mongorestore --db meteor  -h localhost --port 8082 --drop /tmp/APPNAME_$DATE/tmp/APPNAME_$DATE/APPNAME

Fons answered 22/10, 2013 at 7:54 Comment(0)
K
4

You can use mongorestore.

It's pretty much the same as what you already did.

In your first line: meteor mongo --url myApp.meteor.com just remove the last part to so the line will read: meteor mongo --url. When executed on your local machine you will get the information for the local instance of your meteor app. From that point you can just use mongorestore to restore your local db the way you already did remotely.

I use to do a meteor reset prior to a mongorestore, just to be sure that my db is empty, but I don't know if it's actual necessary.

Note that the app should be running when doing this.

Kinslow answered 22/10, 2013 at 5:47 Comment(0)
H
1

I ended up writing a script to download the meteor database. Check it out at https://github.com/AlexeyMK/meteor-download

Usage (in your app's root):

curl https://raw.github.com/AlexeyMK/meteor-download/master/download.sh > download.sh        
./download.sh yourapp.meteor.com`
Hersey answered 13/11, 2013 at 1:29 Comment(0)
H
0

I'm using Google Cloud for Meteor hosting, and wrote custom scripts.

I have this running on a cronjob to backup to google cloud storage:

https://github.com/markoshust/mongo-docker-backup-gcloud/blob/master/mongobackup.sh

#!/bin/bash
MONGO_DB=dbname
MONGO_HOST=127.0.0.1
HOST_DIR=/home/YOURNAME
BACKUP_DIR=/mongobackup
BUCKET=gs://BUCKET_NAME
DATE=`date +%Y-%m-%d:%H:%M:%S`

/usr/bin/docker run --rm \
  -v $HOST_DIR/$BACKUP_DIR:$BACKUP_DIR \
  markoshust/mongoclient \
  mongodump --host $MONGO_HOST --db $MONGO_DB --out $BACKUP_DIR

sudo mkdir -p $HOST_DIR/$BACKUP_DIR/$MONGO_DB/$DATE
sudo mv $HOST_DIR/$BACKUP_DIR/$MONGO_DB/* $HOST_DIR/$BACKUP_DIR/$MONGO_DB/$DATE

$HOST_DIR/gsutil/gsutil rsync -r $HOST_DIR/$BACKUP_DIR $BUCKET

sudo /bin/rm -rf $HOST_DIR/$BACKUP_DIR

Then to restore locally, I created another script which downloads the backup from google cloud storage, stores locally, then does a local restore:

https://github.com/markoshust/mongorestore.sh/blob/master/.mongorestore.sh

#!/bin/bash
## This script syncs a mongodb backup from a Google Cloud Storage bucket and
## mongorestore's it to a local db.
##
## Author: Mark Shust <[email protected]>
## Version: 1.1.0

BUCKET=my-bucket-name
FOLDER=folder-name/$1
BACKUP_DIR=./.backups/
DB_NAME=localdb
DB_HOST=localhost
DB_PORT=27017

if [ -z $1 ]; then
  echo 'Please specify a subdirectory to sync from...'
  exit 0
fi

mkdir -p $BACKUP_DIR

if [ ! -d $BACKUP_DIR ]; then
  gsutil -m cp -r gs://$BUCKET/$FOLDER $BACKUP_DIR
fi

mongorestore --db $DB_NAME -h $DB_HOST --port $DB_PORT --drop $BACKUP_DIR/$1/

echo 'Database restore complete.'

I have this working with Meteor, stupid simple and works great :) Just switch db name to meteor and port to 3001 (or whatever config you have). It's meteor-agnostic so works with any mongodb host/platform.

Heimlich answered 19/5, 2016 at 17:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.