Mongorestore, from meteor production server to local

混江龙づ霸主 提交于 2019-11-28 19:19:31

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.

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

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

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.

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`

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 <mark@shust.com>
## 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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!