Browse Source

feat[litemall-core]: 支持数据库备份操作

Junling Bu 6 years ago
parent
commit
3487f5d6cb

+ 1 - 0
docker/docker-compose.yml

@@ -26,6 +26,7 @@ services:
     volumes:
       - ./litemall/storage:/storage
       - ./litemall/logs:/logs
+      - ./litemall/backup:/backup
       - /etc/localtime:/etc/localtime
     depends_on:
       - mysql57

+ 51 - 0
litemall-db/src/main/java/org/linlinjava/litemall/db/util/DbUtil.java

@@ -0,0 +1,51 @@
+package org.linlinjava.litemall.db.util;
+
+import java.io.*;
+import java.nio.charset.StandardCharsets;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+public class DbUtil {
+
+    public static void backup(File file, String user, String password, String db) {
+        try {
+            Runtime rt = Runtime.getRuntime();
+            String command = "mysqldump -u" + user + " -p" + password + " --set-charset=utf8 " + db;
+            Process child = rt.exec(command);
+            InputStream inputStream = child.getInputStream();
+            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
+            OutputStreamWriter outputStreamWriter = new OutputStreamWriter( new FileOutputStream(file), StandardCharsets.UTF_8);
+            String str;
+            while ((str = bufferedReader.readLine()) != null) {
+                outputStreamWriter.write(str + "\r\n");
+            }
+            outputStreamWriter.flush();
+            inputStream.close();
+            bufferedReader.close();
+            outputStreamWriter.close();
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+    public static void load(File file, String user, String password, String db) {
+        try {
+            Runtime rt = Runtime.getRuntime();
+            String command = "mysql -u" + user + " -p" + password + " --default-character-set=utf8 " + db;
+            Process child = rt.exec(command);
+            OutputStream outputStream = child.getOutputStream();
+            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8));
+            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8);
+            String str;
+            while ((str = bufferedReader.readLine()) != null) {
+                outputStreamWriter.write(str + "\r\n");
+            }
+            outputStreamWriter.flush();
+            outputStream.close();
+            bufferedReader.close();
+            outputStreamWriter.close();
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+}

+ 21 - 0
litemall-db/src/test/java/org/linlinjava/litemall/db/DbUtilTest.java

@@ -0,0 +1,21 @@
+package org.linlinjava.litemall.db;
+
+import org.junit.Test;
+import org.linlinjava.litemall.db.util.DbUtil;
+
+import java.io.File;
+
+public class DbUtilTest {
+    @Test
+    public void testBackup() {
+        File file = new File("test.sql");
+        DbUtil.backup(file, "litemall", "litemall123456", "litemall");
+    }
+
+//    这个测试用例会重置litemall数据库,所以比较危险,请开发者注意
+//    @Test
+    public void testLoad() {
+        File file = new File("test.sql");
+        DbUtil.load(file, "litemall", "litemall123456", "litemall");
+    }
+}