我正在使用 Google Cloud 进行 Meteor 托管,并编写了自定义脚本。
我在 cronjob 上运行它以备份到谷歌云存储:
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
然后要在本地恢复,我创建了另一个脚本,它从谷歌云存储下载备份,在本地存储,然后进行本地恢复:
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.'
我有这个与 Meteor 一起工作,愚蠢的简单并且效果很好:) 只需将数据库名称切换到meteor
并端口到3001
(或您拥有的任何配置)。它与流星无关,因此适用于任何 mongodb 主机/平台。