Home > Database > Mysql Tutorial > body text

mongdb备份恢复

WBOY
Release: 2016-06-07 14:56:14
Original
1534 people have browsed it

一、物理备份(拷贝文件) #!/bin/shDATE=`date+%Y%m%d`DEL_DATE=$(date-d'-30days'"+%Y%m%d")HOST=10.10.17.27PORT=27000USER=adminPASSWORD=123456DATA_DIR="/data/mongodb/navy_db/db"BACKUP_PATH="/data/mongodbbackup/$DATE"date+%Y%m%d%H%M/data/log/m

一、物理备份(拷贝文件)

#!/bin/sh
DATE=`date +%Y%m%d`
DEL_DATE=$(date -d '-30 days' "+%Y%m%d")
HOST=10.10.17.27
PORT=27000
USER=admin
PASSWORD=123456
DATA_DIR="/data/mongodb/navy_db/db"
BACKUP_PATH="/data/mongodbbackup/$DATE"

date +%Y%m%d%H%M >>/data/log/mongodb_bak.log
#第一步锁表
lock()
{
echo "db.fsyncLock()"|  mongo  --host $HOST --port $PORT -u $USER -p $PASSWORD  admin
}
execute()
{
  lock
  if [ $? -eq 0 ]
  then
    echo "mongodb lock successfully!" >>/data/log/mongodb_bak.log
  else
    echo "mongodb lock fail!" >>/data/log/mongodb_bak.log
  fi
}
execute
#第二步备份
back()
{
rsync -av $DATA_DIR $BACKUP_PATH/
}
execute()
{
  back
  if [ $? -eq 0 ]
  then
    echo "mongodb back successfully!" >>/data/log/mongodb_bak.log
  else
    echo "mongodb back fail!" >>/data/log/mongodb_bak.log
  fi
}
execute
#第三部解锁
unlock()
{
echo "db.fsyncUnlock()"|  mongo  --host $HOST --port $PORT -u $USER -p $PASSWORD  admin
}
execute()
{
  unlock
  if [ $? -eq 0 ]
  then
    echo "mongodb unlock successfully!" >>/data/log/mongodb_bak.log
  else
    echo "mongodb unlock fail!" >>/data/log/mongodb_bak.log
  fi
}
execute
#删除历史数据
rm -rf "/data/mongodbbackup/${DEL_DATE}/"
Copy after login

PS:

物理备份只能全库恢复,不能恢复单个表


恢复步骤:

1、停止需要恢复的实例
2、备份清空/data/mongodb/navy_db/db目录
3、拷贝/data/mongodbbackup/$DATE下面的所有文件和文件夹到/data/mongodb/navy_db/db目录
4、chown mongod:mongod /data/mongodb/navy_db/db/ -R
5、启动实例
Copy after login

二、mongodump备份

#!/bin/sh
DATE=`date +%Y%m%d`
DEL_DATE=$(date -d '-30 days' "+%Y%m%d")
HOST=10.10.17.27:27000
USER=admin
PASSWORD=123456
BACKUP_PATH="/backup/mongodbbackup/$DATE"
date +%Y%m%d%H%M >>/data/log/mongodb_bak.log
start()
{
mongodump -h $HOST -u $USER -p $PASSWORD   -o $BACKUP_PATH
}
execute()
{
  start
  if [ $? -eq 0 ]
  then
    echo "mongodb back successfully!" >>/data/log/mongodb_bak.log
  else
    echo "mongodb back fail!" >>/data/log/mongodb_bak.log
  fi
}
execute
rm -rf "/backup/mongodbbackup/${DEL_DATE}/"
Copy after login

恢复:

1、全库恢复

mongorestore --host 10.10.17.27 --port  27000   /backup/mongodbbackup/20150818/
Copy after login

2、单集合(表的概念)恢复

 mongorestore --host 10.10.17.27 --port  27001 --db navy_db_new --collection navy_db_table_new   /backup/mongodbbackup/20150818/navy_db/navy_db_table.bson
Copy after login

三、复制集备份

#!/bin/sh
DATE=`date +%Y%m%d`
DEL_DATE=$(date -d '-30 days' "+%Y%m%d")
HOST="dbset/10.10.17.27:27000,10.10.17.26:27000"
USER=admin
PASSWORD=123456
BACKUP_PATH="/data/mongodbbackup/$DATE"
date +%Y%m%d%H%M >>/data/log/mongodb_bak.log
start()
{
mongodump -h $HOST -u $USER -p $PASSWORD  --oplog  -o $BACKUP_PATH
}
execute()
{
  start
  if [ $? -eq 0 ]
  then
    echo "mongodb back successfully!" >>/data/log/mongodb_bak.log
  else
    echo "mongodb back fail!" >>/data/log/mongodb_bak.log
  fi
}
execute
rm -rf "/backup/mongodbbackup/${DEL_DATE}/"
Copy after login

PS:

备份复制集和备份单个mongodb实例用mongodump备份的方法差不多,只是host的写法不一样,需要指定复制集名称,还有就是加上 --oplog参数,来跟踪备份时主库上发生的所有写操作,获取一个pointin-time快照,否则备份的状态将与集 群中其他节点不匹配。在恢复时,还必须创建oplog,并指定--oplogReplay参数来应用这些操作,否则恢复的成员将不知道从何处开始同步,从 而在某个时间点上与源服务器保持一致。

恢复:

1、降复制集中要恢复的节点移除

rs.remove("10.10.17.26:27000")
Copy after login

2、运行mongorestore --oplogReplay命令

mongorestore --host 10.10.17.26 --port  27000 --oplogReplay  /data/mongodbbackup/20150820/
Copy after login

3、创建oplog

use local 
db.createCollection("oplog.rs", {"capped" : true, "size" : 10000000})
Copy after login

4、恢复oplog

mongorestore --host 10.10.17.26 --port  27000 -d local -c oplog.rs  /data/mongodbbackup/20150820/oplog.bson
Copy after login

5、将该节点加入到复制集

 rs.add("10.10.17.26:27000")
Copy after login


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!