1. mongodb恢复数据
如果数据库需要从备份中恢复数据,使用mongodb自带的mongorestore工具来恢复数据。
mongorestore获取mongodump的输出结果,将备份的结果写入到mongodb中。
2. mongorestore工具
查看mongorestore的的选项参数:
./bin/mongorestore --help
-h [ --host ] arg host
--port arg port
-u [ --username ] arg username
-p [ --password ] arg password
-d [ --db ] arg database to use
-c [ --collection ] arg collection to use (some commands)
-c [ --collection ] arg collection to use (some commands)
--drop drop each collection before import 在导入前先清理掉老数据
<path> 备份数据目录
如:
./bin/mongorestore --host 127.0.0.1 --port 9980 --username user --password pass /home/work/backup/2011-10-24
<replica_set_name>/<hostname1><:port>,<hostname2:<port>,...
参考下面例子
3. 恢复脚本
从备份目录恢复脚本:
#! /bin/sh
# @author bj1602#163.com
# @file restore.sh
# @desc 恢复数据
set -x
hostname="127.0.0.1";
# replica set 副本集格式
hostname="repl0/192.168.0.1:9980,192.168.0.2:9980";
port="9980";
log_dir="/home/gang/backup/mongodb/2014-03-29-10";
mongo_dir="/home/gang/mongo/";
echo `date +"%F %H:%M:%S"`;
cd $mongo_dir;
./bin/mongorestore --host $hostname --port $port --drop $log_dir;
echo `date +"%F %H:%M:%S"`;