Skip to content

Commit

Permalink
o increased robustness of zipping, improved logging
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.jenkins-ci.org/trunk/hudson/plugins/thinBackup@39663 71c3de6d-444a-0410-be80-ed276b4c234a
  • Loading branch information
msteinkogler committed Jul 20, 2011
1 parent ab2399e commit b82ee98
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
Expand Up @@ -121,8 +121,9 @@ private boolean initialize() {
private boolean initializeFromZipFile() {
boolean success = true;

ZipFile zipFile = null;
try {
final ZipFile zipFile = new ZipFile(backupSetzipFile);
zipFile = new ZipFile(backupSetzipFile);
final Enumeration<? extends ZipEntry> zipEntries = zipFile.entries();
while (zipEntries.hasMoreElements() && success) {
final ZipEntry entry = zipEntries.nextElement();
Expand All @@ -142,10 +143,19 @@ private boolean initializeFromZipFile() {
}
}
}
zipFile.close();
} catch (final IOException e) {
LOGGER.log(Level.SEVERE,
String.format("Cannot initialize BackupSet from ZIP file '%s'.", backupSetzipFile.getName()), e);
success = false;
} finally {
try {
if (zipFile != null) {
zipFile.close();
}
} catch (final IOException e) {
LOGGER.log(Level.SEVERE, String.format("Cannot close ZIP file '%s'.", backupSetzipFile.getName()), e);
success = false;
}
}

return success;
Expand Down Expand Up @@ -337,13 +347,13 @@ public BackupSet unzipTo(final File directory) throws IOException {
* @param directory
* @return a reference to the created ZIP file, the current ZIP file if the BackupSet was created from one (because no
* zipping is performed in this case), or null if this BackupSet is invalid.
* @throws IOException
*/
public File zipTo(final File directory) throws IOException {
public File zipTo(final File directory) {
File zipFile = null;

if (isValid()) {
if (!inZipFile) {
DirectoriesZipper zipper = null;
try {
if (!directory.exists()) {
final boolean success = directory.mkdirs();
Expand All @@ -354,17 +364,20 @@ public File zipTo(final File directory) throws IOException {

final String zipFileName = getBackupSetZipFileName();
zipFile = new File(directory, zipFileName);
final DirectoriesZipper zipper = new DirectoriesZipper(zipFile);
zipper = new DirectoriesZipper(zipFile);

zipper.addToZip(getFullBackup());
for (final File diffBackup : getDiffBackups()) {
zipper.addToZip(diffBackup);
}

zipper.close();
} catch (final IOException ioe) {
LOGGER.log(Level.WARNING, "Could not zip backup set.", ioe);
throw ioe;
LOGGER.log(Level.SEVERE, "Could not zip backup set.", ioe);
} finally {
try {
zipper.close();
} catch (final IOException ioe) {
LOGGER.log(Level.SEVERE, "Could not zip backup set.", ioe);
}
}
} else {
zipFile = backupSetzipFile;
Expand Down
18 changes: 14 additions & 4 deletions src/main/java/org/jvnet/hudson/plugins/thinbackup/utils/Utils.java
Expand Up @@ -326,24 +326,34 @@ public static void moveOldBackupsToZipFile(final File backupRoot, final File cur
LOGGER.fine("Moving old backups to zip files...");

final List<BackupSet> validBackupSets = Utils.getValidBackupSetsFromDirectories(backupRoot);

int numberOfZippedBackupSets = 0;
int numberOfMovedBackupSets = 0;
for (final BackupSet backupSet : validBackupSets) {
if ((!backupSet.containsDirectory(currentBackup)) && (!backupSet.isInZipFile())) {
final File zippedBackupSet = backupSet.zipTo(backupRoot);
++numberOfZippedBackupSets;
if (zippedBackupSet != null) {
LOGGER.fine(String.format("Successfully zipped backup set %s to '%s'.", backupSet,
zippedBackupSet.getAbsolutePath()));
try {
backupSet.delete();
LOGGER.fine(String.format("Deleted backup set %s.", backupSet));
LOGGER.fine(String.format("Deleted backup set %s after zipping it.", backupSet));
++numberOfMovedBackupSets;
} catch (final IOException ioe) {
LOGGER.log(Level.WARNING, String.format("Could not delete backup set %s.", backupSet));
}
}
}
}

LOGGER.fine("DONE moving old backups to zip files.");
if (numberOfMovedBackupSets == numberOfZippedBackupSets) {
LOGGER.info(String.format("DONE moving %d backup set(s) to ZIP files.", numberOfMovedBackupSets));
} else {
LOGGER
.info(String
.format(
"DONE zipping %d backup set(s). %d of those could be moved to ZIP files, the rest remain as files/directories as well.",
numberOfZippedBackupSets, numberOfMovedBackupSets));
}
}

}

0 comments on commit b82ee98

Please sign in to comment.