diff --git a/core/src/main/java/com/orientechnologies/orient/core/storage/impl/local/paginated/OLocalPaginatedStorage.java b/core/src/main/java/com/orientechnologies/orient/core/storage/impl/local/paginated/OLocalPaginatedStorage.java index dbd898b487d..fee44ae573e 100755 --- a/core/src/main/java/com/orientechnologies/orient/core/storage/impl/local/paginated/OLocalPaginatedStorage.java +++ b/core/src/main/java/com/orientechnologies/orient/core/storage/impl/local/paginated/OLocalPaginatedStorage.java @@ -53,6 +53,7 @@ import com.orientechnologies.orient.core.storage.impl.local.paginated.wal.OWriteAheadLog; import java.io.*; +import java.nio.channels.FileChannel; import java.util.Arrays; import java.util.Date; import java.util.List; diff --git a/distributed/src/main/java/com/orientechnologies/orient/server/distributed/impl/task/OSyncDatabaseTask.java b/distributed/src/main/java/com/orientechnologies/orient/server/distributed/impl/task/OSyncDatabaseTask.java index 6d6abd39da3..ac4ba6edcf8 100755 --- a/distributed/src/main/java/com/orientechnologies/orient/server/distributed/impl/task/OSyncDatabaseTask.java +++ b/distributed/src/main/java/com/orientechnologies/orient/server/distributed/impl/task/OSyncDatabaseTask.java @@ -99,7 +99,8 @@ public Object execute(final ODistributedRequestId requestId, final OServer iServ backupFile.getParentFile().mkdirs(); backupFile.createNewFile(); - final FileOutputStream fileOutputStream = new FileOutputStream(backupFile); + final File resultedBackup = backupFile; + final FileOutputStream fileOutputStream = new FileOutputStream(resultedBackup); final File completedFile = new File(backupFile.getAbsolutePath() + ".completed"); if (completedFile.exists()) @@ -142,6 +143,7 @@ public void onMessage(String iText) { } catch (Exception e) { OLogManager.instance().error(this, "Cannot execute backup of database '%s' for deploy database", e, databaseName); + resultedBackup.delete(); } finally { try { fileOutputStream.close(); diff --git a/server/src/main/java/com/orientechnologies/orient/server/handler/OAutomaticBackup.java b/server/src/main/java/com/orientechnologies/orient/server/handler/OAutomaticBackup.java index a1e72deb98a..26cb47f39a1 100755 --- a/server/src/main/java/com/orientechnologies/orient/server/handler/OAutomaticBackup.java +++ b/server/src/main/java/com/orientechnologies/orient/server/handler/OAutomaticBackup.java @@ -35,6 +35,7 @@ import com.orientechnologies.orient.core.exception.OConfigurationException; import com.orientechnologies.orient.core.metadata.security.OSecurityNull; import com.orientechnologies.orient.core.record.impl.ODocument; +import com.orientechnologies.orient.core.storage.impl.local.paginated.wal.OLogSequenceNumber; import com.orientechnologies.orient.server.OServer; import com.orientechnologies.orient.server.config.OServerParameterConfiguration; import com.orientechnologies.orient.server.plugin.OServerPluginAbstract; @@ -78,11 +79,11 @@ public enum MODE { private MODE mode = MODE.FULL_BACKUP; private String exportOptions; - private String targetDirectory = "backup"; - private String targetFileName; + private String targetDirectory = "backup"; + private String targetFileName; private Set includeDatabases = new HashSet(); private Set excludeDatabases = new HashSet(); - private OServer serverInstance; + private OServer serverInstance; @Override public void config(final OServer iServer, final OServerParameterConfiguration[] iParams) { @@ -128,7 +129,7 @@ else if (param.name.equalsIgnoreCase("exportOptions")) // LOAD CFG FROM JSON FILE. THIS FILE, IF SPECIFIED, OVERWRITE DEFAULT AND XML SETTINGS configure(); - if(enabled) { + if (enabled) { if (delay <= 0) throw new OConfigurationException("Cannot find mandatory parameter 'delay'"); if (!targetDirectory.endsWith("/")) @@ -143,8 +144,8 @@ else if (param.name.equalsIgnoreCase("exportOptions")) filePath.mkdirs(); OLogManager.instance() - .info(this, "Automatic Backup plugin installed and active: delay=%dms, firstTime=%s, targetDirectory=%s", delay, firstTime, - targetDirectory); + .info(this, "Automatic Backup plugin installed and active: delay=%dms, firstTime=%s, targetDirectory=%s", delay, + firstTime, targetDirectory); final TimerTask timerTask = new TimerTask() { @Override @@ -214,7 +215,8 @@ public void run() { } catch (Exception e) { - OLogManager.instance().error(this, "Error on backup of database '" + dbURL + "' to directory: " + targetDirectory, e); + OLogManager.instance() + .error(this, "Error on backup of database '" + dbURL + "' to directory: " + targetDirectory, e); errors++; } finally { @@ -232,9 +234,8 @@ public void run() { else Orient.instance().scheduleTask(timerTask, firstTime, delay); - }else { - OLogManager.instance() - .info(this, "Automatic Backup plugin is disabled"); + } else { + OLogManager.instance().info(this, "Automatic Backup plugin is disabled"); } } @@ -348,17 +349,29 @@ protected void incrementalBackupDatabase(final String dbURL, String iPath, final protected void fullBackupDatabase(final String dbURL, final String iPath, final ODatabaseDocumentInternal db) throws IOException { OLogManager.instance().info(this, "AutomaticBackup: executing full backup of database '%s' to %s", dbURL, iPath); - final FileOutputStream fileOutputStream = new FileOutputStream(iPath); + final File backupFile = new File(iPath); try { - db.backup(fileOutputStream, null, null, new OCommandOutputListener() { - @Override - public void onMessage(String iText) { - OLogManager.instance().info(this, iText); - } - }, compressionLevel, bufferSize); - } finally { - fileOutputStream.close(); + final FileOutputStream fileOutputStream = new FileOutputStream(backupFile); + try { + db.backup(fileOutputStream, null, null, new OCommandOutputListener() { + @Override + public void onMessage(String iText) { + OLogManager.instance().info(this, iText); + } + }, compressionLevel, bufferSize); + } finally { + fileOutputStream.close(); + } + } catch (IOException e) { + OLogManager.instance().errorNoDb(this, "Error during backup, backup file %s will be deleted", e, backupFile); + backupFile.delete(); + throw e; + } catch (RuntimeException e) { + OLogManager.instance().errorNoDb(this, "Error during backup, backup file %s will be deleted", e, backupFile); + backupFile.delete(); + throw e; } + } protected void exportDatabase(final String dbURL, final String iPath, final ODatabaseDocumentInternal db) throws IOException { diff --git a/tools/src/main/java/com/orientechnologies/orient/console/OConsoleDatabaseApp.java b/tools/src/main/java/com/orientechnologies/orient/console/OConsoleDatabaseApp.java index 18bd861fc44..09c2e42b183 100755 --- a/tools/src/main/java/com/orientechnologies/orient/console/OConsoleDatabaseApp.java +++ b/tools/src/main/java/com/orientechnologies/orient/console/OConsoleDatabaseApp.java @@ -106,7 +106,7 @@ public class OConsoleDatabaseApp extends OrientConsole implements OCommandOutput private int lastPercentStep; private String currentDatabaseUserName; private String currentDatabaseUserPassword; - private int maxMultiValueEntries = 10; + private int maxMultiValueEntries = 10; public OConsoleDatabaseApp(final String[] args) { super(args); @@ -2296,6 +2296,12 @@ else if (parName.equalsIgnoreCase("compressionLevel")) if (f.exists()) f.delete(); throw e; + } catch (IOException e) { + fos.close(); + File f = new File(fileName); + if (f.exists()) + f.delete(); + throw e; } } } catch (ODatabaseExportException e) {