Note: the version just supports macOS and linux.
100% testing coverage, please feel free to use.
new MongoDump.Builder() .runtime(mockRuntime) .uri("mongodb://127.0.0.1:27017/gt_ut") .archive("dbname.archive") .commandPath("/usr/local/bin/mongodump") .build() .execute();Source Code MongoDump.java
package learningops.mongo; import org.apache.commons.io.IOUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.InputStream; import static com.google.common.base.Preconditions.checkNotNull; /** * @author learningops * @date 22/03/2018 */ public class MongoDump { private static final Logger LOG = LoggerFactory.getLogger(MongoDump.class); private String uri; private String archive; private String commandPath; private Runtime runtime; public static class Builder { private String uri; private String archive; private String commandPath; private Runtime runtime; public Builder archive(String archive) { this.archive = archive; return this; } public Builder runtime(Runtime runtime) { this.runtime = runtime; return this; } public Builder commandPath(String commandPath) { this.commandPath = commandPath; return this; } public Builder uri(String uri) { this.uri = uri; return this; } public MongoDump build() { MongoDump result = new MongoDump(); result.uri = checkNotNull(uri, "uri was null."); result.commandPath = checkNotNull(commandPath, "commandPath was null."); result.archive = checkNotNull(archive, "archive was null."); Runtime rt = runtime; if (rt == null) { rt = Runtime.getRuntime(); } result.runtime = rt; return result; } } public String execute() { try { String command = String.format("%s --archive=%s --uri=%s", commandPath, archive, uri); LOG.debug("command: {}", command); Process runtimeProcess = runtime.exec(new String[]{"/bin/sh", "-c", command}); int exitValue = runtimeProcess.waitFor(); if (exitValue != 0) { InputStream error = runtimeProcess.getErrorStream(); String errorMessage = IOUtils.toString(error, "UTF-8"); throw new MongoDumpException(errorMessage); } InputStream message = runtimeProcess.getInputStream(); return IOUtils.toString(message, "UTF-8"); } catch (MongoDumpException e) { throw e; } catch (Exception e) { throw new RuntimeException(e); } } }MongoDumpException.java
package learningops.mongo; /** * @author learningops * @date 22/03/2018 */ public class MongoDumpException extends RuntimeException { public MongoDumpException(String message) { super(message); } }Unit Testing
package learningops.mongo; import org.apache.commons.io.IOUtils; import org.testng.annotations.Test; import java.io.InputStream; import static org.mockito.Matchers.anyString; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import static org.testng.Assert.assertEquals; /** * @author learningops * @date 22/03/2018 */ public class MongoDumpTest { @Test public void test() throws Exception { Runtime mockRuntime = mock(Runtime.class); Process mockProcess = mock(Process.class); when(mockProcess.getInputStream()).thenReturn(IOUtils.toInputStream("success message", "UTF-8")); when(mockRuntime.exec(new String[]{anyString()})).thenReturn(mockProcess); when(mockProcess.waitFor()).thenReturn(0); String result = new MongoDump.Builder() .runtime(mockRuntime) .uri("mongodb://127.0.0.1:27017/gt_ut") .archive("dbname.archive") .commandPath("/usr/local/bin/mongodump") .build() .execute(); assertEquals(result, "success message"); } @Test(expectedExceptions = {MongoDumpException.class}, expectedExceptionsMessageRegExp = "error message") public void unknownTermination() throws Exception { Runtime mockRuntime = mock(Runtime.class); Process mockProcess = mock(Process.class); when(mockRuntime.exec(new String[]{anyString()})).thenReturn(mockProcess); when(mockProcess.waitFor()).thenReturn(1); InputStream errorStream = IOUtils.toInputStream("error message", "UTF-8"); when(mockProcess.getErrorStream()).thenReturn(errorStream); new MongoDump.Builder() .runtime(mockRuntime) .uri("mongodb://127.0.0.1:27017/dbname") .archive("dbname.archive") .commandPath("/usr/local/bin/mongodump") .build() .execute(); } @Test(expectedExceptions = {RuntimeException.class}) public void unknownException() throws Exception { Runtime mockRuntime = mock(Runtime.class); Process mockProcess = mock(Process.class); when(mockRuntime.exec(new String[]{anyString()})).thenReturn(mockProcess); InputStream errorStream = IOUtils.toInputStream("error message", "UTF-8"); when(mockProcess.getErrorStream()).thenReturn(errorStream); new MongoDump.Builder() .runtime(mockRuntime) .uri("mongodb://127.0.0.1:27017/dbname") .archive("dbname.archive") .commandPath("/usr/local/bin/mongodump") .build() .execute(); } @Test public void buildWithDefaultRuntime() { new MongoDump.Builder() .uri("mongodb://127.0.0.1:27017/dbname") .archive("dbname.archive") .commandPath("/usr/local/bin/mongodump") .build(); } }
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://m.specialneedsforspecialkids.com/yun/19223.html
Note: the version just supports macOS and linux. 100% testing coverage, please feel free to use. Usage new MongoDump.Builder() .runtime(mockRuntime) .uri(mongodb://127...
There are a bunch of different methods you can use to back up your MongoDB data, but if you want to avoid downtime and/or potential performance degradation, the most common advice seems to be that you...
摘要:就是說,恢復(fù)后,備份后添加修改的數(shù)據(jù)都會(huì)被刪除,慎用實(shí)例 Centos Mongodb離線安裝&配置遠(yuǎn)程連接&數(shù)據(jù)遷移 筆者的之前的centos服務(wù)器滿了,這次準(zhǔn)備遷移數(shù)據(jù).目的是,擴(kuò)容更大的磁盤分區(qū),避免一次又一次的掛載新的磁盤.由于機(jī)器無法聯(lián)網(wǎng)本次為離線安裝 下載Mongodb網(wǎng)址為https://www.mongodb.com/dr/fastdl.mongodb.org/lin...
摘要:一的導(dǎo)入與導(dǎo)出導(dǎo)出工具概念中的工具可以把一個(gè)導(dǎo)出成格式或格式的文件。可以通過參數(shù)指定導(dǎo)出的數(shù)據(jù)項(xiàng),也可以根據(jù)指定的條件導(dǎo)出數(shù)據(jù)?;謴?fù)工具概念是從備份中恢復(fù)數(shù)據(jù)的工具,它主要用來獲取的輸出結(jié)果,并將備份的數(shù)據(jù)插入到運(yùn)行的中。 一、Mongodb的導(dǎo)入與導(dǎo)出 1.1、導(dǎo)出工具:mongoexport 概念: mongoDB中的mongoexport工具可以把一個(gè)collection導(dǎo)出成J...
摘要:導(dǎo)入導(dǎo)出可以操作的是本地的服務(wù)器也可以是遠(yuǎn)程的服務(wù)器所以都有如下通用選項(xiàng)主機(jī)端口用戶名密碼導(dǎo)出庫名表名列名查詢條件導(dǎo)出的文件名導(dǎo)出格式便于和傳統(tǒng)數(shù)據(jù)庫交換數(shù)據(jù)導(dǎo)出庫下面的表從哪里導(dǎo)出導(dǎo)出的文檔數(shù)導(dǎo)出庫下 導(dǎo)入/導(dǎo)出可以操作的是本地的mongodb服務(wù)器,也可以是遠(yuǎn)程的服務(wù)器所以,都有如下通用選項(xiàng): -h host 主機(jī) --port port 端口 -u username 用...
閱讀 831·2019-08-30 14:05
閱讀 1721·2019-08-30 11:08
閱讀 3223·2019-08-29 15:41
閱讀 3599·2019-08-23 18:31
閱讀 1519·2019-08-23 18:29
閱讀 552·2019-08-23 14:51
閱讀 2112·2019-08-23 13:53
閱讀 2132·2019-08-23 13:02