Percona MongoDB3.2版本默认支持WiredTiger引擎的在线热备份,解决了官方版只能通过mongodump逻辑备份这一缺陷。
使用说明:要在当前dbpath中对数据库进行热备份,请在admin数据库上以管理员身份运行createBackup命令,并指定备份目录。
自动备份脚本
perl -MCPAN -e “install MongoDB”
#!/usr/bin/perl
use MongoDB;
use File::Path;
use POSIX qw(strftime);
my $mc = MongoDB::MongoClient->new(
host => "mongodb://localhost:37019/",
username => "admin",
password => "123456",
);
my $db = $mc->get_database("admin");
$year = strftime "%Y",localtime;
$month = strftime "%m",localtime;
$time = strftime "%Y-%m-%d-%H-%M-%S", localtime;
$BAKDB = "cfgdb";
$BAKDIR = "/data/bak/hcy/$year/$month/$BAKDB\_$time";
my $user = getpwnam "mongodb" or die "bad user";
my $group = getgrnam "mongodb" or die "bad group";
mkpath($BAKDIR) or die "目录已存在. $!";
chown $user, $group, $BAKDIR;
my $cmd = [
createBackup => 1,
backupDir => $BAKDIR
];
$db->run_command($cmd);
if($! == 0){
print "backup is success.
";
}else{
print "backup is failure.
";
}
注:
MongoDB参数设置
directoryperdb = true
wiredTigerDirectoryForIndexes = true
这两个必须注销掉,否则备份失败。
恢复很简单,把备份目录里的数据文件直接拷贝到你的dbpath下,然后启动MongoDB即可。
参考文献:
https://www.percona.com/doc/percona-server-for-mongodb/LATEST/hot-backup.html#hot-backup
本文转自hcymysql51CTO博客,原文链接: http://blog.51cto.com/hcymysql/1931613,如需转载请自行联系原作者