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(); } }
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://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...
摘要:就是說,恢復后,備份后添加修改的數據都會被刪除,慎用實例 Centos Mongodb離線安裝&配置遠程連接&數據遷移 筆者的之前的centos服務器滿了,這次準備遷移數據.目的是,擴容更大的磁盤分區,避免一次又一次的掛載新的磁盤.由于機器無法聯網本次為離線安裝 下載Mongodb網址為https://www.mongodb.com/dr/fastdl.mongodb.org/lin...
摘要:一的導入與導出導出工具概念中的工具可以把一個導出成格式或格式的文件。可以通過參數指定導出的數據項,也可以根據指定的條件導出數據。恢復工具概念是從備份中恢復數據的工具,它主要用來獲取的輸出結果,并將備份的數據插入到運行的中。 一、Mongodb的導入與導出 1.1、導出工具:mongoexport 概念: mongoDB中的mongoexport工具可以把一個collection導出成J...
摘要:導入導出可以操作的是本地的服務器也可以是遠程的服務器所以都有如下通用選項主機端口用戶名密碼導出庫名表名列名查詢條件導出的文件名導出格式便于和傳統數據庫交換數據導出庫下面的表從哪里導出導出的文檔數導出庫下 導入/導出可以操作的是本地的mongodb服務器,也可以是遠程的服務器所以,都有如下通用選項: -h host 主機 --port port 端口 -u username 用...
閱讀 821·2019-08-30 14:05
閱讀 1712·2019-08-30 11:08
閱讀 3216·2019-08-29 15:41
閱讀 3591·2019-08-23 18:31
閱讀 1510·2019-08-23 18:29
閱讀 546·2019-08-23 14:51
閱讀 2103·2019-08-23 13:53
閱讀 2126·2019-08-23 13:02