下面的脚本只需更改变量$mongodb相关参数即可,如有更好的更便捷的方法请赐教。
#!/bin/perl ################################# ### author: www.ttlsa.com ### ### QQ群: 39514058 ### ### E-mail: [email protected] ### ################################# use strict; use File::Path; use POSIX; use MongoDB; use Data::Dumper; my $mongodump='/usr/local/mongodb/bin/mongodump'; my $mongodb={ 'admin_1'=>{ #定义组别 'email'=>'[email protected]', #定义备份出错时邮件通知地址,此处未包含该功能。 'server'=>[ #定义mongodb相关信息 { 'replset'=>1, #是否复制集 'sharding'=>0, #是否分片 'setname'=>"TTLSA_COM", #复制集名称 'mongodsvr'=>["10.1.11.155:27017","10.1.11.156:27017","10.1.11.157:27017"], #mongod服务器IP和端口号 'configsvr'=>[], #config server服务器IP和端口号 'mongossvr'=>[], #mongos server服务器IP和端口号 'backupdir'=>"/data/backup/mongodb_ttlsa_com", #备份目录 'user'=>"", #用户名 'passwd'=>"", #密码 'interval'=>15, #备份轮滚周期 }, { 'replset'=>1, 'sharding'=>1, 'setname'=>"shard1", 'mongodsvr'=>["10.1.22.21:27029","10.1.22.22:27029","10.1.22.23:27029"], 'configsvr'=>["10.1.22.21:27028","10.1.22.22:27028","10.1.22.23:27028"], 'mongossvr'=>["10.1.22.21:27027","10.1.22.22:27027","10.1.22.23:27027"], 'backupdir'=>"/data/backup/mongodb_shard1", 'user'=>"", 'passwd'=>"", 'interval'=>15, }, { 'replset'=>0, 'sharding'=>0, 'setname'=>"shard1", 'mongodsvr'=>["10.1.27.22:30000","10.1.27.22:30001","10.1.20.16"], 'configsvr'=>[], 'mongossvr'=>[], 'backupdir'=>"/data/backup/mongodb_standalon", 'user'=>"root", 'passwd'=>"www.ttlsa.com", 'interval'=>15, } ]}, #'admin_2'=>{} }; while(my($group,$value)=each(%$mongodb)){ foreach my $node (@{$value->{'server'}}){ my $tmp_stdout = tmpnam(); my $tmp_stderr = tmpnam(); if($node->{'replset'} && !$node->{'sharding'}){ my $dir = $node->{'backupdir'}; mkpath $dir unless -e $dir; my $return=backup_rotate($dir,$node->{'interval'}); my $hosts = $node->{'setname'}.'/'.join(',', @{$node->{'mongodsvr'}}); if(($node->{'user'} ne '') && ($node->{'passwd'} ne '')){ my $retval=system("$mongodump -vvvvv -h $hosts -u $node->{'user'} -p $node->{'passwd'} --oplog -o $dir/dump.1 >$tmp_stdout 2>$tmp_stderr"); }else{ my $retval=system("$mongodump -vvvvv -h $hosts --oplog -o $dir/dump.1 >$tmp_stdout 2>$tmp_stderr"); } }elsif($node->{'sharding'}){ my $mongod_hosts=$node->{'setname'}.'/'.join(',', @{$node->{'mongodsvr'}}); my $mongos_hosts=$node->{'setname'}.'/'.join(',', @{$node->{'mongossvr'}}); my $mongod_dir = $node->{'backupdir'}.'/'.'mongodsvr'; my $config_dir = $node->{'backupdir'}.'/'.'configsvr'; mkpath $mongod_dir unless -e $mongod_dir; mkpath $config_dir unless -e $config_dir; my $return=backup_rotate($mongod_dir,$node->{'interval'}); my $return=backup_rotate($config_dir,$node->{'interval'}); if(($node->{'user'} ne '') && ($node->{'passwd'} ne '')){ my $conn = MongoDB::Connection->new("host" => "mongodb://$mongos_hosts","db_name" => "admin", "username" => "$node->{'user'}", "password" => "$node->{'passwd'}"); my $db = $conn->get_database('config'); my $coll=$db->get_collection('settings'); my $ret=$coll->update({ _id => "balancer" }, { '$set' => { 'stopped'=> 'true' } },{'false'},{'true'}); my $retval=system("$mongodump -h $mongos_hosts -u $node->{'user'} -p $node->{'passwd'} --db config -o $config_dir/dump.1 >$tmp_stdout 2>$tmp_stderr"); my $retval=system("$mongodump -h $mongod_hosts -u $node->{'user'} -p $node->{'passwd'} --oplog -o $mongod_dir/dump.1 >$tmp_stdout 2>$tmp_stderr"); my $ret=$coll->update({ _id => "balancer" }, { '$set' => { 'stopped'=> 'false' } },{'false'},{'true'}); }else{ my $conn = MongoDB::Connection->new("host" => "mongodb://$mongos_hosts"); my $db = $conn->get_database('config'); my $coll=$db->get_collection('settings'); my $ret=$coll->update({ _id => "balancer" }, { '$set' => { 'stopped'=> 'true' } },{'false'},{'true'}); my $retval=system("$mongodump -h $mongos_hosts --db config -o $config_dir/dump.1 >$tmp_stdout 2>$tmp_stderr"); my $retval=system("$mongodump -h $mongod_hosts --oplog -o $mongod_dir/dump.1 >$tmp_stdout 2>$tmp_stderr"); my $ret=$coll->update({ _id => "balancer" }, { '$set' => { 'stopped'=> 'false' } },{'false'},{'true'}); } }else{ foreach my $each (@{$node->{'mongodsvr'}}){ my $dir = $node->{'backupdir'}.'/'.$each; mkpath $dir unless -e $dir; my $return=backup_rotate($dir,$node->{'interval'}); if(($node->{'user'} ne '') && ($node->{'passwd'} ne '')){ my $retval=system("$mongodump -h $each -u $node->{'user'} -p $node->{'passwd'} -o $dir/dump.1 >$tmp_stdout 2>$tmp_stderr"); }else{ my $retval=system("$mongodump -h $each -o $dir/dump.1 >$tmp_stdout 2>$tmp_stderr"); } } } } } sub backup_rotate { my $backup_dir=shift; my $retral=shift; my @aa=sort{$b<=>$a} 1..$retral; if(chdir "/$backup_dir"){ for my $num (@aa){ my $old=$num+1; if(-e "dump.$num"){ if(-e "dump.$old"){ system("rm -rf dump.$old && mv dump.$num dump.$old"); }else{ system("mv dump.$num dump.$old"); } } } }else{ return "Failed to change dir $backup_dir : $!"; } }转至: http://www.ttlsa.com/mongodb/mongodb-backup-and-recovery-_lower_-ttlsa-tutorial-series-mongodb/