Skip to content

Commit

Permalink
- Fix for (GEOS-7851) Backup and Restore may fail if java.io.tmpdir …
Browse files Browse the repository at this point in the history
…is not defined
  • Loading branch information
afabiani committed Nov 14, 2016
1 parent 69cbf8d commit 304f07c
Show file tree
Hide file tree
Showing 8 changed files with 205 additions and 74 deletions.
Expand Up @@ -76,6 +76,8 @@ public class Backup extends JobExecutionListenerSupport

public static final String PARAM_INPUT_FILE_PATH = "input.file.path";

public static final String PARAM_CLEANUP_TEMP = "BK_CLEANUP_TEMP";

public static final String PARAM_DRY_RUN_MODE = "BK_DRY_RUN";

public static final String PARAM_BEST_EFFORT_MODE = "BK_BEST_EFFORT";
Expand Down Expand Up @@ -348,7 +350,7 @@ public BackupExecutionAdapter runBackupAsync(final Resource archiveFile,
FileUtils.touch(archiveFile.file());

// Write flat files into a temporary folder
Resource tmpDir = BackupUtils.tmpDir();
Resource tmpDir = BackupUtils.geoServerTmpDir(getGeoServerDataDirectory());

// Fill Job Parameters
JobParametersBuilder paramsBuilder = new JobParametersBuilder();
Expand Down Expand Up @@ -414,7 +416,7 @@ public BackupExecutionAdapter runBackupAsync(final Resource archiveFile,
public RestoreExecutionAdapter runRestoreAsync(final Resource archiveFile, final Filter filter,
final Hints params) throws IOException {
// Extract archive into a temporary folder
Resource tmpDir = BackupUtils.tmpDir();
Resource tmpDir = BackupUtils.geoServerTmpDir(getGeoServerDataDirectory());
BackupUtils.extractTo(archiveFile, tmpDir);

// Fill Job Parameters
Expand Down Expand Up @@ -601,6 +603,7 @@ private void parseParams(final Hints params, JobParametersBuilder paramsBuilder)
final Set<String> key = ((Hints.OptionKey) param.getKey()).getOptions();
for (String k : key) {
switch (k) {
case PARAM_CLEANUP_TEMP:
case PARAM_DRY_RUN_MODE:
case PARAM_BEST_EFFORT_MODE:
if (paramsBuilder.toJobParameters().getString(k) == null) {
Expand Down
Expand Up @@ -8,6 +8,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.Map.Entry;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.geoserver.backuprestore.Backup;
Expand Down Expand Up @@ -115,6 +116,20 @@ public void afterJob(JobExecution jobExecution) {
Resource sourceFolder = Resources
.fromURL(jobParameters.getString(Backup.PARAM_OUTPUT_FILE_PATH));
BackupUtils.compressTo(sourceFolder, backupExecution.getArchiveFile());

// Cleanup Temporary Resources
String cleanUpTempFolders = jobParameters.getString(Backup.PARAM_CLEANUP_TEMP);
if (cleanUpTempFolders != null && Boolean.parseBoolean(cleanUpTempFolders) && sourceFolder != null) {
if (Resources.exists(sourceFolder)) {
try {
if (!sourceFolder.delete()) {
LOGGER.warning("It was not possible to cleanup Temporary Resources. Please double check that Resources inside the Temp GeoServer Data Directory have been removed.");
}
} catch (Exception e) {
LOGGER.log(Level.WARNING, "It was not possible to cleanup Temporary Resources. Please double check that Resources inside the Temp GeoServer Data Directory have been removed.", e);
}
}
}
}
}
} catch (NoSuchJobExecutionException | IOException e) {
Expand Down
Expand Up @@ -7,6 +7,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.Map.Entry;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.geoserver.backuprestore.Backup;
Expand All @@ -16,11 +17,13 @@
import org.geoserver.catalog.event.CatalogListener;
import org.geoserver.catalog.impl.CatalogImpl;
import org.geoserver.platform.resource.Resource;
import org.geoserver.platform.resource.Resources;
import org.geotools.util.logging.Logging;
import org.opengis.filter.Filter;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobExecutionListener;
import org.springframework.batch.core.JobParameters;

/**
* Implements a Spring Batch {@link JobExecutionListener}.
Expand Down Expand Up @@ -130,6 +133,24 @@ public void afterJob(JobExecution jobExecution) {
}

backupFacade.getGeoServer().reset();

JobParameters jobParameters = restoreExecution.getJobParameters();
Resource tempFolder = Resources
.fromURL(jobParameters.getString(Backup.PARAM_INPUT_FILE_PATH));

// Cleanup Temporary Resources
String cleanUpTempFolders = jobParameters.getString(Backup.PARAM_CLEANUP_TEMP);
if (cleanUpTempFolders != null && Boolean.parseBoolean(cleanUpTempFolders) && tempFolder != null) {
if (Resources.exists(tempFolder)) {
try {
if (!tempFolder.delete()) {
LOGGER.warning("It was not possible to cleanup Temporary Resources. Please double check that Resources inside the Temp GeoServer Data Directory have been removed.");
}
} catch (Exception e) {
LOGGER.log(Level.WARNING, "It was not possible to cleanup Temporary Resources. Please double check that Resources inside the Temp GeoServer Data Directory have been removed.", e);
}
}
}
}
}
// Collect errors
Expand Down
Expand Up @@ -7,6 +7,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
Expand All @@ -20,11 +21,13 @@
import org.apache.commons.vfs2.FileType;
import org.apache.commons.vfs2.VFS;
import org.codehaus.plexus.util.FileUtils;
import org.geoserver.config.GeoServerDataDirectory;
import org.geoserver.platform.resource.Files;
import org.geoserver.platform.resource.Paths;
import org.geoserver.platform.resource.Resource;
import org.geoserver.platform.resource.Resource.Type;
import org.geoserver.platform.resource.Resources;
import org.geoserver.util.IOUtils;
import org.geotools.util.logging.Logging;

/**
Expand All @@ -39,8 +42,62 @@ public class BackupUtils {

private static final Logger LOGGER = Logging.getLogger(BackupUtils.class);

/**
* Returns a random temp folder Resource inside the System Temp Directory.
*
* @return
* @throws IOException
*/
public static Resource tmpDir() throws IOException {
Resource root = Resources.fromPath(System.getProperty("java.io.tmpdir", "."));
String tempPath = null;
try {
tempPath = System.getProperty("java.io.tmpdir");
if (tempPath == null) {
tempPath = IOUtils.createTempDirectory("backuputils").getAbsolutePath();
LOGGER.warning(
"It was not possible to create a temporary folder into the System 'java.io.tmpdir'. Falling back to default TEMP ["
+ tempPath + "].");
}
} catch (Exception e) {
tempPath = null;
LOGGER.log(Level.SEVERE,
"It was not possible to create a temporary folder! In order to fix the problem, please check the System 'java.io.tmpdir' point to a valid folder.",
e);
throw new IOException(
"It was not possible to create a temporary folder! In order to fix the problem, please check the System 'java.io.tmpdir' point to a valid folder.",
e);
}

if (tempPath == null) {
LOGGER.log(Level.SEVERE,
"It was not possible to create or find a suitable temporary folder. 'tempPath' is NULL! In order to fix the problem, please check the System 'java.io.tmpdir' point to a valid folder.");
throw new IOException(
"It was not possible to create or find a suitable temporary folder. 'tempPath' is NULL! In order to fix the problem, please check the System 'java.io.tmpdir' point to a valid folder.");
}

return createRandomResource(tempPath);
}

/**
* Returns a random temp folder Resource inside the GeoServer Temp Directory.
*
* @param geoServerDataDirectory
* @return
* @throws IOException
*/
public static Resource geoServerTmpDir(GeoServerDataDirectory geoServerDataDirectory) throws IOException {
String tempPath = geoServerDataDirectory.findOrCreateDir("temp").getAbsolutePath();

return createRandomResource(tempPath);
}

/**
* @param tempPath
* @return
* @throws IOException
*/
private static Resource createRandomResource(String tempPath) throws IOException {
Resource root = Resources.fromPath(tempPath);
Resource directory = Resources.createRandom("tmp", "", root);

do {
Expand All @@ -51,7 +108,7 @@ public static Resource tmpDir() throws IOException {

return Files.asResource(directory.dir());
}

/**
* Extracts the archive file {@code archiveFile} to {@code targetFolder}; both shall previously exist.
*
Expand Down Expand Up @@ -226,4 +283,5 @@ public static Resource dir(Resource baseDir, String subDir) {
return Resources.fromPath(
Resources.directory(targetPath, !Resources.exists(targetPath)).getAbsolutePath());
}

}

0 comments on commit 304f07c

Please sign in to comment.