线上系统为防止服务器异常,导致数据丢失!!通常会将数库做定时异地备份。
Windows server 2016在安装bin目录下可以使用两种方式:mongodump/mongorestore数据库的备份/恢复、mongoexport/mongoimport表的备份/恢复。
一、备份
mongodump.exe -h IP:端口 -u 用户名 -p 密码 -d 数据库 -o 备份地址 --authenticationDatabase admin
错误: Failed: error connecting to db server: server returned error on SASL authentication step: Authentication failed.
解决:–authenticationDatabase admin
提示:复杂密码需要加斜杠作为转意符。
备份示例:
mongodump.exe -h 127.0.0.1:27001 -u root -p 123456 -d test_db -o D:\dbback\ --authenticationDatabase admin
二、恢复
mongorestore.exe -h IP:端口 -u 用户名 -p 密码 -d 数据库 备份地址\数据库 --authenticationDatabase admin
恢复示例:
mongorestore.exe -h 127.0.0.1:27001 -u root -p 123456 -d test_db d:\dbback\test_db --authenticationDatabase admin
三、脚本
@echo off
set host=127.0.0.1
set port=27001
set user=xzl123
set password=123456
set dbname=test_db
set back_date=%Date:~0,4%%Date:~5,2%%Date:~8,2%%time:~0,2%%Time:~3,2%%Time:~6,2%
set backfileurl=D:\dbback\mongo\%dbname%_%back_date%\
"D:\DevelopmentSoftware\mongodb4\bin\mongodump.exe" -h %host%:%port% -u %user% -p %password% -d %dbname% -o %backfileurl% --authenticationDatabase admin
forfiles /p D:\dbback\mongo /s /m * /d -5 /c "cmd /c del @file /f"
exit
四、定时计划
添加定时任务计划:控制面板>>搜索任务计划>>创建任务>>跟着导航走完设置,保存即可。