This is an automated email from the ASF dual-hosted git repository.

JackieTien97 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/iotdb.git


The following commit(s) were added to refs/heads/master by this push:
     new 493b3867f28 Fix offline backup skipping data directories with 
whitespace around separators
493b3867f28 is described below

commit 493b3867f281ec57841047168ba820256d72659e
Author: DaZuiZui <[email protected]>
AuthorDate: Wed Dec 2 18:09:52 2026 +0800

    Fix offline backup skipping data directories with whitespace around 
separators
---
 .../iotdb/tool/backup/IoTDBDataBackTool.java       | 145 +++++++------
 .../iotdb/tool/backup/IoTDBDataBackToolTest.java   | 237 +++++++++++++++++++++
 .../org/apache/iotdb/db/conf/IoTDBDescriptor.java  |   2 +-
 3 files changed, 317 insertions(+), 67 deletions(-)

diff --git 
a/iotdb-client/cli/src/main/java/org/apache/iotdb/tool/backup/IoTDBDataBackTool.java
 
b/iotdb-client/cli/src/main/java/org/apache/iotdb/tool/backup/IoTDBDataBackTool.java
index b128a614f4c..f095bc96b4c 100644
--- 
a/iotdb-client/cli/src/main/java/org/apache/iotdb/tool/backup/IoTDBDataBackTool.java
+++ 
b/iotdb-client/cli/src/main/java/org/apache/iotdb/tool/backup/IoTDBDataBackTool.java
@@ -22,6 +22,8 @@ package org.apache.iotdb.tool.backup;
 import org.apache.iotdb.cli.i18n.CliMessages;
 import org.apache.iotdb.commons.conf.CommonConfig;
 import org.apache.iotdb.commons.conf.IoTDBConstant;
+import org.apache.iotdb.commons.conf.TrimProperties;
+import org.apache.iotdb.db.conf.IoTDBConfig;
 import org.apache.iotdb.db.conf.IoTDBDescriptor;
 import org.apache.iotdb.tool.data.AbstractDataTool;
 
@@ -31,6 +33,7 @@ import org.slf4j.LoggerFactory;
 import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
@@ -62,7 +65,6 @@ public class IoTDBDataBackTool {
   static String targetDirParam = "";
   static String targetDataDirParam = "";
   static String targetWalDirParam = "";
-  static String remoteDnDataDir = "";
 
   static AtomicInteger fileCount = new AtomicInteger(0);
   static AtomicInteger targetFileCount = new AtomicInteger(0);
@@ -71,9 +73,7 @@ public class IoTDBDataBackTool {
 
   private static final Logger LOGGER = 
LoggerFactory.getLogger(AbstractDataTool.class);
 
-  private static final IoTDBDescriptor ioTDBDescriptor = 
IoTDBDescriptor.getInstance();
   static String sourcePath = System.getProperty(IoTDBConstant.IOTDB_HOME, 
null);
-  static boolean IS_OBJECT_STORAGE = false;
   static String DEFAULT_DN_DATA_DIRS =
       "data"
           + File.separator
@@ -126,9 +126,9 @@ public class IoTDBDataBackTool {
       } else if (args[i].equalsIgnoreCase("-targetdir") && i + 1 < 
args.length) {
         targetDirParam = args[i + 1];
       } else if (args[i].equalsIgnoreCase("-targetdatadir") && i + 1 < 
args.length) {
-        targetDataDirParam = args[i + 1];
+        targetDataDirParam = normalizeDirectoryList(args[i + 1]);
       } else if (args[i].equalsIgnoreCase("-targetwaldir") && i + 1 < 
args.length) {
-        targetWalDirParam = args[i + 1];
+        targetWalDirParam = normalizeDirectoryList(args[i + 1]);
       }
     }
   }
@@ -205,7 +205,7 @@ public class IoTDBDataBackTool {
         if (!targetDir.exists()) {
           targetDir.mkdirs();
         }
-        countConfigNodeFile(targetDirString.toString(), copyMap, 
cnMapProperties);
+        countConfigNodeFile(properties, targetDirString.toString(), copyMap, 
cnMapProperties);
         countNodeBack(targetDirString.toString(), copyMap);
         for (Map.Entry<String, String> entry : copyMap.entrySet()) {
           countFiles(entry.getKey());
@@ -220,7 +220,8 @@ public class IoTDBDataBackTool {
                 + CommonConfig.SYSTEM_CONFIG_NAME,
             cnMapProperties);
       } else if (nodeTypeParam.equalsIgnoreCase("datanode")) {
-        countDataNodeFile(targetDirString.toString(), copyMap, dnDataDirsMap, 
dnMapProperties);
+        countDataNodeFile(
+            properties, targetDirString.toString(), copyMap, dnDataDirsMap, 
dnMapProperties);
         countNodeBack(targetDirString.toString(), copyMap);
         checkQuickMode(dnDataDirsMap);
         if (!targetDir.exists()) {
@@ -242,8 +243,9 @@ public class IoTDBDataBackTool {
                 + CommonConfig.SYSTEM_CONFIG_NAME,
             dnMapProperties);
       } else if (nodeTypeParam.equalsIgnoreCase("all") || 
nodeTypeParam.isEmpty()) {
-        countConfigNodeFile(targetDirString.toString(), copyMap, 
cnMapProperties);
-        countDataNodeFile(targetDirString.toString(), copyMap, dnDataDirsMap, 
dnMapProperties);
+        countConfigNodeFile(properties, targetDirString.toString(), copyMap, 
cnMapProperties);
+        countDataNodeFile(
+            properties, targetDirString.toString(), copyMap, dnDataDirsMap, 
dnMapProperties);
         countNodeBack(targetDirString.toString(), copyMap);
         checkQuickMode(dnDataDirsMap);
         if (!targetDir.exists()) {
@@ -283,7 +285,7 @@ public class IoTDBDataBackTool {
       }
 
       if (nodeTypeParam.equalsIgnoreCase("confignode")) {
-        countConfigNodeFile(targetDirString.toString(), copyMap, 
cnMapProperties);
+        countConfigNodeFile(properties, targetDirString.toString(), copyMap, 
cnMapProperties);
         countNodeBack(targetDirString.toString(), copyMap);
         for (Map.Entry<String, String> entry : copyMap.entrySet()) {
           countFiles(entry.getKey());
@@ -299,7 +301,8 @@ public class IoTDBDataBackTool {
             cnMapProperties);
       } else if (nodeTypeParam.equalsIgnoreCase("datanode")) {
         countNodeBack(targetDirString.toString(), copyMap);
-        countDataNodeFile(targetDirString.toString(), copyMap, dnDataDirsMap, 
dnMapProperties);
+        countDataNodeFile(
+            properties, targetDirString.toString(), copyMap, dnDataDirsMap, 
dnMapProperties);
 
         for (Map.Entry<String, String> entry : copyMap.entrySet()) {
           countFiles(entry.getKey());
@@ -319,8 +322,9 @@ public class IoTDBDataBackTool {
             dnMapProperties);
       } else if (nodeTypeParam.equalsIgnoreCase("all") || 
nodeTypeParam.isEmpty()) {
         countNodeBack(targetDirString.toString(), copyMap);
-        countConfigNodeFile(targetDirString.toString(), copyMap, 
cnMapProperties);
-        countDataNodeFile(targetDirString.toString(), copyMap, dnDataDirsMap, 
dnMapProperties);
+        countConfigNodeFile(properties, targetDirString.toString(), copyMap, 
cnMapProperties);
+        countDataNodeFile(
+            properties, targetDirString.toString(), copyMap, dnDataDirsMap, 
dnMapProperties);
         for (Map.Entry<String, String> entry : copyMap.entrySet()) {
           countFiles(entry.getKey());
         }
@@ -450,9 +454,6 @@ public class IoTDBDataBackTool {
 
   private static void countNodeBack(String targetDirString, Map<String, 
String> copyMap) {
     File sourceDir = new File(sourcePath);
-    Properties properties = getProperties(CommonConfig.SYSTEM_CONFIG_NAME);
-    initDataNodeProperties(properties);
-    initConfigNodeProperties(properties);
 
     copyMap.put(
         sourceDir.getAbsolutePath() + File.separatorChar + ".env",
@@ -496,28 +497,24 @@ public class IoTDBDataBackTool {
   }
 
   private static void countDataNodeFile(
+      Properties dataProperties,
       String targetDirString,
       Map<String, String> copyMap,
       Map<String, String> dnDataDirsMap,
       Map<String, String> dnMapProperties) {
-    Properties dataProperties = getProperties(CommonConfig.SYSTEM_CONFIG_NAME);
-    initDataNodeProperties(dataProperties);
-
     String dnSystemDir = dataProperties.getProperty("dn_system_dir");
     String dnConsensusDir = dataProperties.getProperty("dn_consensus_dir");
 
     String dnTracingDir = dataProperties.getProperty("dn_tracing_dir");
 
     String dnWalDirs = dataProperties.getProperty("dn_wal_dirs");
-    String dnDataDirs = dataProperties.getProperty("dn_data_dirs");
-    String dnDataDirAll = dnDataDirs;
-    dnDataDirs = isObjectStorage(dnDataDirs);
-    remoteDnDataDir = dnDataDirAll.replaceAll(dnDataDirs, "");
+    String dnDataDirs = 
resolveDataDirectories(dataProperties.getProperty("dn_data_dirs"));
+    String remoteDnDataDir = getObjectStorageSuffix(dnDataDirs);
+    dnDataDirs = dnDataDirs.substring(0, dnDataDirs.length() - 
remoteDnDataDir.length());
     dnSystemDir = pathHandler(dnSystemDir);
     dnConsensusDir = pathHandler(dnConsensusDir);
     dnTracingDir = pathHandler(dnTracingDir);
     dnWalDirs = pathHandler(dnWalDirs);
-    dnDataDirs = pathHandler(dnDataDirs);
     String bakDnSystemDir = targetDirString + File.separatorChar + 
DEFAULT_DN_SYSTEM_DIR;
     String bakDnConsensusDir = targetDirString + File.separatorChar + 
DEFAULT_DN_CONSENSUS_DIR;
     String bakDnTracingDir = targetDirString + File.separatorChar + 
DEFAULT_DN_TRACING_DIR;
@@ -531,7 +528,6 @@ public class IoTDBDataBackTool {
     } else {
       targetWalDirParam = getCreateDnDataPathString(dnWalDirs, 
targetWalDirParam, "wal");
     }
-    String targetDnDataDirs;
     if (targetDataDirParam.isEmpty()) {
       targetDataDirParam =
           targetDirParam
@@ -545,10 +541,14 @@ public class IoTDBDataBackTool {
     if (!vaildParam(dnDataDirs, dnWalDirs)) {
       System.exit(0);
     }
-    targetDnDataDirs = getCreateDnDataPathString(dnDataDirs, 
targetDataDirParam, "data");
+    String targetDnDataDirs = remoteDnDataDir;
+    if (!dnDataDirs.isEmpty()) {
+      String localTargets = getCreateDnDataPathString(dnDataDirs, 
targetDataDirParam, "data");
+      dnDataDirsMap.putAll(getCreatePathMapping(dnDataDirs, localTargets, 
"data"));
+      targetDnDataDirs = localTargets + remoteDnDataDir;
+    }
     Map<String, String> dnSystemDirMap =
         getCreatePathMapping(Objects.requireNonNull(dnSystemDir), 
bakDnSystemDir, "system");
-    dnDataDirsMap.putAll(getCreatePathMapping(dnDataDirs, targetDnDataDirs, 
"data"));
     Map<String, String> dnConsensusDirMap =
         getCreatePathMapping(
             Objects.requireNonNull(dnConsensusDir), bakDnConsensusDir, 
"consensus");
@@ -561,46 +561,56 @@ public class IoTDBDataBackTool {
     copyMap.putAll(dnTracingDirMap);
 
     dnMapProperties.put("dn_system_dir", bakDnSystemDir);
-    dnMapProperties.put("dn_data_dirs", targetDnDataDirs + remoteDnDataDir);
+    dnMapProperties.put("dn_data_dirs", targetDnDataDirs);
     dnMapProperties.put("dn_wal_dirs", targetWalDirParam);
     dnMapProperties.put("dn_tracing_dir", bakDnTracingDir);
     dnMapProperties.put("dn_consensus_dir", bakDnConsensusDir);
   }
 
-  private static String isObjectStorage(String dnDataDirs) {
-    StringBuilder tmpDnDataDirs = new StringBuilder();
-    String[] patternDirs = dnDataDirs.split(";");
-    for (int i = 0; i < patternDirs.length; i++) {
-      String patternDir = patternDirs[i];
-      String[] subPatternDirs = patternDir.split(",");
-      for (int c = 0; c < subPatternDirs.length; c++) {
-        if (c == subPatternDirs.length - 1 && i == patternDirs.length - 1) {
-          if (subPatternDirs[c].equals("OBJECT_STORAGE")) {
-            IS_OBJECT_STORAGE = true;
-          } else {
-            tmpDnDataDirs.append(subPatternDirs[c]);
-          }
-        } else {
-          tmpDnDataDirs.append(subPatternDirs[c]);
-          if (subPatternDirs.length > 1 && c < subPatternDirs.length - 1) {
-            tmpDnDataDirs.append(",");
-          } else if (patternDirs.length > 1 && i < patternDirs.length - 1) {
-            tmpDnDataDirs.append(";");
-          }
+  private static String normalizeDirectoryList(String directories) {
+    String[] tiers = directories.trim().split(";");
+    for (int i = 0; i < tiers.length; i++) {
+      String[] dirs = tiers[i].trim().split(",");
+      for (int j = 0; j < dirs.length; j++) {
+        dirs[j] = dirs[j].trim();
+      }
+      tiers[i] = String.join(",", dirs);
+    }
+    return String.join(";", tiers);
+  }
+
+  private static String resolveDataDirectories(String dataDirs) {
+    String[][] tiers = IoTDBDescriptor.parseDataDirs(dataDirs);
+    String[] resolvedTiers = new String[tiers.length];
+    for (int i = 0; i < tiers.length; i++) {
+      String[] dirs = tiers[i];
+      for (int j = 0; j < dirs.length; j++) {
+        if (i == tiers.length - 1
+            && j == dirs.length - 1
+            && IoTDBConstant.OBJECT_STORAGE_DIR.equals(dirs[j])) {
+          break;
         }
+        // Resolve before joining so an empty local entry retains its place in 
the tier.
+        dirs[j] = IoTDBConfig.addDataHomeDir(dirs[j]);
       }
+      resolvedTiers[i] = String.join(",", dirs);
     }
-    if (IS_OBJECT_STORAGE) {
-      return tmpDnDataDirs.toString().substring(0, 
tmpDnDataDirs.toString().length() - 1);
+    return String.join(IoTDBConstant.TIER_SEPARATOR, resolvedTiers);
+  }
+
+  private static String getObjectStorageSuffix(String dataDirs) {
+    int separator = Math.max(dataDirs.lastIndexOf(';'), 
dataDirs.lastIndexOf(','));
+    if (IoTDBConstant.OBJECT_STORAGE_DIR.equals(dataDirs.substring(separator + 
1))) {
+      return dataDirs.substring(Math.max(0, separator));
     }
-    return tmpDnDataDirs.toString();
+    return "";
   }
 
   private static void countConfigNodeFile(
-      String targetDirString, Map<String, String> copyMap, Map<String, String> 
cnMapProperties) {
-    Properties configProperties = 
getProperties(CommonConfig.SYSTEM_CONFIG_NAME);
-    initConfigNodeProperties(configProperties);
-
+      Properties configProperties,
+      String targetDirString,
+      Map<String, String> copyMap,
+      Map<String, String> cnMapProperties) {
     String bakCnSystemDir = targetDirString + File.separatorChar + 
DEFAULT_CN_SYSTEM_DIR;
     String bakCnConsensusDir = targetDirString + File.separatorChar + 
DEFAULT_CN_CONSENSUS_DIR;
 
@@ -896,18 +906,21 @@ public class IoTDBDataBackTool {
     }
   }
 
-  private static Properties getProperties(String configName) {
-    URL url = ioTDBDescriptor.getPropsUrl(configName);
-    Properties properties = new Properties();
-    if (url != null) {
-      try (InputStream inputStream = url.openStream()) {
-        LOGGER.info(CliMessages.START_READ_CONFIG, url);
-        properties.load(new InputStreamReader(inputStream, 
StandardCharsets.UTF_8));
-      } catch (Exception e) {
-        LOGGER.error(CliMessages.READ_CONFIG_ERROR, url, e);
-      }
+  private static Properties getProperties(String configName) throws 
IOException {
+    URL url = IoTDBDescriptor.getPropsUrl(configName);
+    if (url == null) {
+      LOGGER.error(CliMessages.READ_CONFIG_ERROR, configName);
+      throw new FileNotFoundException(configName);
+    }
+    try (InputStream inputStream = url.openStream()) {
+      LOGGER.info(CliMessages.START_READ_CONFIG, url);
+      Properties properties = new TrimProperties();
+      properties.load(new InputStreamReader(inputStream, 
StandardCharsets.UTF_8));
+      return properties;
+    } catch (IOException | IllegalArgumentException e) {
+      LOGGER.error(CliMessages.READ_CONFIG_ERROR, url, e);
+      throw e;
     }
-    return properties;
   }
 
   private static boolean filesAreEqual(Path file1, Path file2) throws 
IOException {
diff --git 
a/iotdb-client/cli/src/test/java/org/apache/iotdb/tool/backup/IoTDBDataBackToolTest.java
 
b/iotdb-client/cli/src/test/java/org/apache/iotdb/tool/backup/IoTDBDataBackToolTest.java
new file mode 100644
index 00000000000..8a1c3d2e283
--- /dev/null
+++ 
b/iotdb-client/cli/src/test/java/org/apache/iotdb/tool/backup/IoTDBDataBackToolTest.java
@@ -0,0 +1,237 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iotdb.tool.backup;
+
+import org.apache.iotdb.commons.conf.CommonConfig;
+import org.apache.iotdb.commons.conf.IoTDBConstant;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.Reader;
+import java.io.Writer;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.Arrays;
+import java.util.Properties;
+import java.util.concurrent.TimeUnit;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertTrue;
+
+public class IoTDBDataBackToolTest {
+
+  private static final Path SEQUENCE_FILE = Path.of("sequence", 
"sample.tsfile");
+
+  @Rule public TemporaryFolder temporaryFolder = new TemporaryFolder();
+
+  private Path home;
+  private Path backup;
+  private Path log;
+
+  @Before
+  public void setUp() throws IOException {
+    home = temporaryFolder.newFolder("home").toPath();
+    backup = temporaryFolder.getRoot().toPath().resolve("backup");
+    log = temporaryFolder.getRoot().toPath().resolve("backup-output.log");
+    Files.createDirectory(home.resolve("conf"));
+    Files.createDirectory(home.resolve("logs"));
+  }
+
+  @Test
+  public void testBackupAllDataDirectoriesWithWhitespace() throws Exception {
+    Path second = temporaryFolder.getRoot().toPath().resolve("disk two");
+    writeSample(home.resolve("first"), "first");
+    writeSample(second, "second");
+    writeSample(home.resolve("OBJECT_STORAGE"), "third");
+
+    Properties properties = new Properties();
+    properties.setProperty("dn_data_dirs", "first, " + second + " ,; 
OBJECT_STORAGE");
+    Properties restored = runBackup(properties);
+
+    Path data = backup.resolve("data").resolve("datanode").resolve("data");
+    String restoredDataDirs = restored.getProperty("dn_data_dirs");
+    assertEquals(
+        data.resolve("data1") + "," + data.resolve("data2") + ";" + 
data.resolve("data3"),
+        restoredDataDirs);
+    String[] dirs = restoredDataDirs.split("[,;]");
+    assertEquals("first", Files.readString(Path.of(dirs[0], "sample")));
+    assertEquals("second", Files.readString(Path.of(dirs[1], "sample")));
+    assertEquals("third", Files.readString(Path.of(dirs[2], "sample")));
+  }
+
+  @Test
+  public void testBackupExplicitlyEmptyDataDirectory() throws Exception {
+    writeSequenceFile(home, "home");
+    Properties properties = new Properties();
+    properties.setProperty("dn_data_dirs", "");
+
+    Properties restored = runBackup(properties);
+
+    assertRestoredSequenceFile(home, restored.getProperty("dn_data_dirs"));
+  }
+
+  @Test
+  public void testBackupEmptyDataDirectoryAtEndOfTier() throws Exception {
+    Path hot = temporaryFolder.newFolder("hot").toPath();
+    Path cold = temporaryFolder.newFolder("cold").toPath();
+    writeSequenceFile(hot, "hot");
+    writeSequenceFile(home, "home");
+    writeSequenceFile(cold, "cold");
+    Properties properties = new Properties();
+    properties.setProperty("dn_data_dirs", hot + ", ;" + cold);
+
+    Properties restored = runBackup(properties);
+
+    String[] tiers = restored.getProperty("dn_data_dirs").split(";", -1);
+    assertEquals(2, tiers.length);
+    String[] firstTier = tiers[0].split(",", -1);
+    assertEquals(2, firstTier.length);
+    assertRestoredSequenceFile(hot, firstTier[0]);
+    assertRestoredSequenceFile(home, firstTier[1]);
+    assertRestoredSequenceFile(cold, tiers[1]);
+  }
+
+  @Test
+  public void testExplicitBackupDirectoriesWithObjectStorage() throws 
Exception {
+    writeSample(home.resolve("hot"), "hot");
+    writeSample(home.resolve("cold"), "cold");
+    writeSample(home.resolve("wal1"), "wal1");
+    writeSample(home.resolve("wal2"), "wal2");
+    writeSample(home.resolve("system"), "system");
+    Properties properties = new Properties();
+    properties.setProperty("dn_data_dirs", "hot ; cold ; " + 
IoTDBConstant.OBJECT_STORAGE_DIR);
+    properties.setProperty("dn_wal_dirs", "wal1,wal2  ");
+    properties.setProperty("dn_system_dir", "system  ");
+
+    Path hot = backup.resolve("hot tier");
+    Path cold = backup.resolve("cold tier");
+    Path wal1 = backup.resolve("first wal");
+    Path wal2 = backup.resolve("second wal");
+    Properties restored =
+        runBackup(
+            properties, "-targetdatadir", hot + " ; " + cold, "-targetwaldir", 
wal1 + " , " + wal2);
+
+    assertEquals(
+        hot + ";" + cold + ";" + IoTDBConstant.OBJECT_STORAGE_DIR,
+        restored.getProperty("dn_data_dirs"));
+    assertEquals(wal1 + "," + wal2, restored.getProperty("dn_wal_dirs"));
+    assertEquals("hot", Files.readString(hot.resolve("sample")));
+    assertEquals("cold", Files.readString(cold.resolve("sample")));
+    assertEquals("wal1", Files.readString(wal1.resolve("sample")));
+    assertEquals("wal2", Files.readString(wal2.resolve("sample")));
+    assertEquals(
+        "system", 
Files.readString(Path.of(restored.getProperty("dn_system_dir"), "sample")));
+  }
+
+  @Test
+  public void testMissingConfigurationStopsBackup() throws Exception {
+    assertBackupFailsBeforeCreatingTarget();
+  }
+
+  @Test
+  public void testMalformedConfigurationStopsBackup() throws Exception {
+    Path external = temporaryFolder.newFolder("external").toPath();
+    writeSequenceFile(external, "external data");
+    Properties properties = new Properties();
+    properties.setProperty("dn_data_dirs", external.toString());
+    try (Writer writer =
+        
Files.newBufferedWriter(home.resolve("conf").resolve(CommonConfig.SYSTEM_CONFIG_NAME)))
 {
+      writer.write("broken=\\uZZZZ\n");
+      properties.store(writer, null);
+    }
+
+    assertBackupFailsBeforeCreatingTarget();
+  }
+
+  private void assertBackupFailsBeforeCreatingTarget() throws Exception {
+    int exitCode = runBackupProcess();
+    assertNotEquals(Files.readString(log), 0, exitCode);
+    assertFalse(Files.exists(backup));
+  }
+
+  private static void writeSample(Path directory, String value) throws 
IOException {
+    Files.writeString(Files.createDirectory(directory).resolve("sample"), 
value);
+  }
+
+  private static void writeSequenceFile(Path dataDir, String value) throws 
IOException {
+    Path file = dataDir.resolve(SEQUENCE_FILE);
+    Files.createDirectories(file.getParent());
+    Files.writeString(file, value);
+  }
+
+  private void assertRestoredSequenceFile(Path sourceDir, String restoredDir) 
throws IOException {
+    Path targetDir = Path.of(restoredDir);
+    assertTrue(restoredDir, targetDir.startsWith(backup));
+    assertArrayEquals(
+        Files.readAllBytes(sourceDir.resolve(SEQUENCE_FILE)),
+        Files.readAllBytes(targetDir.resolve(SEQUENCE_FILE)));
+  }
+
+  private Properties runBackup(Properties properties, String... targetOptions) 
throws Exception {
+    Path config = 
home.resolve("conf").resolve(CommonConfig.SYSTEM_CONFIG_NAME);
+    try (Writer writer = Files.newBufferedWriter(config)) {
+      properties.store(writer, null);
+    }
+
+    int exitCode = runBackupProcess(targetOptions);
+    assertEquals(Files.readString(log), 0, exitCode);
+    Properties restored = new Properties();
+    Path restoredConfig = backup.resolve("conf").resolve(config.getFileName());
+    try (Reader reader = Files.newBufferedReader(restoredConfig)) {
+      restored.load(reader);
+    }
+    return restored;
+  }
+
+  private int runBackupProcess(String... targetOptions) throws Exception {
+    String java = File.separatorChar == '\\' ? "java.exe" : "java";
+    ProcessBuilder builder =
+        new ProcessBuilder(
+                Path.of(System.getProperty("java.home"), "bin", 
java).toString(),
+                "-D" + IoTDBConstant.IOTDB_HOME + "=" + home,
+                "-cp",
+                System.getProperty(
+                    "surefire.test.class.path", 
System.getProperty("java.class.path")),
+                IoTDBDataBackTool.class.getName(),
+                "-node",
+                "datanode",
+                "-targetdir",
+                backup.toString())
+            .directory(temporaryFolder.getRoot())
+            .redirectErrorStream(true)
+            .redirectOutput(log.toFile());
+    builder.command().addAll(Arrays.asList(targetOptions));
+    builder.environment().put(IoTDBConstant.IOTDB_HOME, home.toString());
+    Process process = builder.start();
+    try {
+      assertTrue(process.waitFor(60, TimeUnit.SECONDS));
+      return process.exitValue();
+    } finally {
+      process.destroyForcibly();
+    }
+  }
+}
diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/conf/IoTDBDescriptor.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/conf/IoTDBDescriptor.java
index 04fe3381b75..347636a6728 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/conf/IoTDBDescriptor.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/conf/IoTDBDescriptor.java
@@ -2186,7 +2186,7 @@ public class IoTDBDescriptor {
     }
   }
 
-  private String[][] parseDataDirs(String dataDirs) {
+  public static String[][] parseDataDirs(String dataDirs) {
     String[] tiers = dataDirs.split(IoTDBConstant.TIER_SEPARATOR);
     String[][] tierDataDirs = new String[tiers.length][];
     for (int i = 0; i < tiers.length; ++i) {

Reply via email to