Skip to content

Commit

Permalink
o JENKINS-8955: settings for "clean up diffs" and "nr of backup sets …
Browse files Browse the repository at this point in the history
…to keep" are respected for ZIPping backupsets as well

git-svn-id: https://svn.jenkins-ci.org/trunk/hudson/plugins/thinBackup@39164 71c3de6d-444a-0410-be80-ed276b4c234a
  • Loading branch information
msteinkogler committed Mar 28, 2011
1 parent 6d363ab commit 2ac9014
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 55 deletions.
Expand Up @@ -398,7 +398,7 @@ private Date getDateFromDirectoryName(final String directoryName) {
Date result = null;

try {
result = Utils.getDateFromBackupDirectory(directoryName);
result = Utils.getDateFromBackupDirectoryName(directoryName);
} catch (final ParseException e) {
LOGGER.warning(String.format("Could not retrieve date component of directory '%s'.", directoryName));
}
Expand Down
Expand Up @@ -56,7 +56,7 @@ public class HudsonBackup {
private final BackupType backupType;
private final Date latestFullBackupDate;
private final boolean cleanupDiff;
private final int nrMaxStoredFull;
private final int nrMaxStoredFull; // stores nr of backup sets that will be kept
private final boolean moveOldBackupsToZipFile;

public HudsonBackup(final File backupRoot, final File hudsonHome, final BackupType backupType,
Expand Down Expand Up @@ -149,9 +149,9 @@ public void backup() throws IOException {
new DirectoryCleaner().removeEmptyDirectories(backupDirectory);

if (backupType == BackupType.FULL) {
removeSuperfluousBackups();
moveOldBackupsToZipFile(backupDirectory);
cleanupDiffs();
moveOldBackupsToZipFile(backupDirectory);
removeSuperfluousBackupSets();
}
}

Expand Down Expand Up @@ -282,19 +282,19 @@ private PluginList getPluginListFromLatestFull() throws IOException {
return latestFullPlugins;
}

private void removeSuperfluousBackups() throws IOException {
private void removeSuperfluousBackupSets() throws IOException {
if (nrMaxStoredFull > 0) {
LOGGER.fine("Removing superfluous backups...");
LOGGER.fine("Removing superfluous backup sets...");
final ThinBackupPluginImpl plugin = ThinBackupPluginImpl.getInstance();
final List<BackupSet> validBackupSets = Utils.getValidBackupSetsFromDirectories(new File(plugin.getBackupPath()));
final List<BackupSet> validBackupSets = Utils.getValidBackupSets(new File(plugin.getBackupPath()));
int nrOfRemovedBackups = 0;
while (validBackupSets.size() > nrMaxStoredFull) {
final BackupSet set = validBackupSets.get(0);
set.delete();
validBackupSets.remove(set);
++nrOfRemovedBackups;
}
LOGGER.fine(String.format("DONE. Removed %d superfluous backup(s).", nrOfRemovedBackups));
LOGGER.fine(String.format("DONE. Removed %d superfluous backup sets.", nrOfRemovedBackups));
}
}

Expand Down
110 changes: 66 additions & 44 deletions src/main/java/org/jvnet/hudson/plugins/thinbackup/utils/Utils.java
Expand Up @@ -83,7 +83,7 @@ public static void waitUntilIdle() {
* if the name could not be parsed
*/
public static Date getDateFromBackupDirectory(final File directory) throws ParseException {
return getDateFromBackupDirectory(directory.getName());
return getDateFromBackupDirectoryName(directory.getName());
}

/**
Expand All @@ -92,7 +92,7 @@ public static Date getDateFromBackupDirectory(final File directory) throws Parse
* @throws ParseException
* if the name could not be parsed
*/
public static Date getDateFromBackupDirectory(final String directoryName) throws ParseException {
public static Date getDateFromBackupDirectoryName(final String directoryName) throws ParseException {
Date result = null;

if (directoryName.startsWith(BackupType.FULL.toString()) || directoryName.startsWith(BackupType.DIFF.toString())) {
Expand Down Expand Up @@ -130,14 +130,27 @@ public static File getFormattedDirectory(final File parent, final BackupType bac
/**
* @param parentDir
* @param backupType
* @return an (unordered) list of backup directories of the given backup type.
* @return an unordered list of backup directories of the given backup type.
*/
public static List<File> getBackupTypeDirectories(final File parentDir, final BackupType backupType) {
IOFileFilter prefixFilter = FileFilterUtils.prefixFileFilter(backupType.toString());
prefixFilter = FileFilterUtils.andFileFilter(prefixFilter, DirectoryFileFilter.DIRECTORY);
return Arrays.asList(parentDir.listFiles((FilenameFilter) prefixFilter));
}

/**
* @param parentDir
* @return an unordered list of zipped backupsets in the given directory.
*/
public static List<File> getBackupSetZipFiles(final File parentDir) {
IOFileFilter zipFileFilter = FileFilterUtils.prefixFileFilter(BackupSet.BACKUPSET_ZIPFILE_PREFIX);
zipFileFilter = FileFilterUtils.andFileFilter(zipFileFilter,
FileFilterUtils.suffixFileFilter(BackupSet.BACKUPSET_ZIPFILE_SUFFIX));
zipFileFilter = FileFilterUtils.andFileFilter(zipFileFilter, FileFileFilter.FILE);

return Arrays.asList(parentDir.listFiles((FilenameFilter) zipFileFilter));
}

/**
* @param diffBackup
* @return the full backup referenced by the given diff backup, or null if none can be found.
Expand Down Expand Up @@ -168,7 +181,6 @@ public static File getReferencedFullBackup(final File diffBackup) {
}
}
} catch (final ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Expand Down Expand Up @@ -203,54 +215,28 @@ public static List<File> getReferencingDiffBackups(final File fullBackup) {
* from both directories and ZIP files, ordered descending by the date encoded in the backups' name.
*/
public static List<String> getBackupsAsDates(final File directory) {
IOFileFilter directoryFilter = FileFilterUtils.prefixFileFilter(BackupType.FULL.toString());
directoryFilter = FileFilterUtils.orFileFilter(directoryFilter,
FileFilterUtils.prefixFileFilter(BackupType.DIFF.toString()));
directoryFilter = FileFilterUtils.andFileFilter(directoryFilter, DirectoryFileFilter.DIRECTORY);
final String[] backups = directory.list(directoryFilter);

final List<String> backupDates = new ArrayList<String>(backups.length);
for (final String name : backups) {
final List<String> backupDates = new ArrayList<String>();

final List<BackupSet> backupSets = getValidBackupSets(directory);
for (final BackupSet backupSet : backupSets) {
final String fullName = backupSet.getFullBackupName();
try {
final Date tmp = getDateFromBackupDirectory(name);
final Date tmp = getDateFromBackupDirectoryName(fullName);
backupDates.add(DISPLAY_DATE_FORMAT.format(tmp));
} catch (final ParseException e) {
LOGGER.warning("Cannot parse directory name '" + name
+ "', therefore it will not show up in the list of available backups.");
LOGGER.warning(String.format(
"Cannot parse directory name '%s' , therefore it will not show up in the list of available backups.",
fullName));
}
}

IOFileFilter zipFileFilter = FileFilterUtils.prefixFileFilter(BackupSet.BACKUPSET_ZIPFILE_PREFIX);
zipFileFilter = FileFilterUtils.andFileFilter(zipFileFilter,
FileFilterUtils.suffixFileFilter(BackupSet.BACKUPSET_ZIPFILE_SUFFIX));
zipFileFilter = FileFilterUtils.andFileFilter(zipFileFilter, FileFileFilter.FILE);
final String[] backupSets = directory.list(zipFileFilter);
for (final String name : backupSets) {
final BackupSet set = new BackupSet(new File(directory, name));
if (set.isValid()) {
final String fullName = set.getFullBackupName();
for (final String diffName : backupSet.getDiffBackupsNames()) {
try {
final Date tmp = getDateFromBackupDirectory(fullName);
final Date tmp = getDateFromBackupDirectoryName(diffName);
backupDates.add(DISPLAY_DATE_FORMAT.format(tmp));
} catch (final ParseException e) {
LOGGER
.warning(String
.format(
"Cannot parse directory name '%s' contained in ZIP file '%s', therefore it will not show up in the list of available backups.",
fullName, name));
}

for (final String diffName : set.getDiffBackupsNames()) {
try {
final Date tmp = getDateFromBackupDirectory(diffName);
backupDates.add(DISPLAY_DATE_FORMAT.format(tmp));
} catch (final ParseException e) {
LOGGER
.warning(String
.format(
"Cannot parse directory name '%s' contained in ZIP file '%s', therefore it will not show up in the list of available backups.",
diffName, name));
}
LOGGER.warning(String.format(
"Cannot parse directory name '%s' , therefore it will not show up in the list of available backups.",
diffName));
}
}
}
Expand All @@ -277,6 +263,42 @@ public static List<BackupSet> getValidBackupSetsFromDirectories(final File direc
}
}
Collections.sort(validSets);

return validSets;
}

/**
* @param directory
* @return a list of valid (@see BackupSet#isValid) backup sets that exists as ZIP files (not as directories) in the
* given directory, ordered ascending by the backup date of the BackupSets' full backup.
*/
public static List<BackupSet> getValidBackupSetsFromZips(final File directory) {
final Collection<File> backups = Utils.getBackupSetZipFiles(directory);

final List<BackupSet> validSets = new ArrayList<BackupSet>();
for (final File backup : backups) {
final BackupSet set = new BackupSet(backup);
if (set.isValid()) {
validSets.add(set);
}
}
Collections.sort(validSets);

return validSets;
}

/**
* @param directory
* @return a list of valid (@see BackupSet#isValid) backup sets in the given directory, ordered ascending by the
* backup date of the BackupSets' full backup.
*/
public static List<BackupSet> getValidBackupSets(final File directory) {
final List<BackupSet> validSets = new ArrayList<BackupSet>();

validSets.addAll(getValidBackupSetsFromDirectories(directory));
validSets.addAll(getValidBackupSetsFromZips(directory));
Collections.sort(validSets);

return validSets;
}

Expand Down
Expand Up @@ -44,7 +44,7 @@
<f:textbox value="${it.configuration.diffBackupSchedule}" name="diffBackupSchedule"/>
</f:entry>

<f:entry title="Max number of stored full backups"
<f:entry title="Max number of backup sets"
help="/plugin/thinBackup/help/help-nrMaxStoredFull.html">
<f:textbox value="${it.configuration.nrMaxStoredFull}" name="nrMaxStoredFull"/>
</f:entry>
Expand Down
2 changes: 1 addition & 1 deletion src/main/webapp/help/help-nrMaxStoredFull.html
Expand Up @@ -24,6 +24,6 @@

<div>
<p>
Specifies how many full backups will be kept.
Specifies how many backup sets will be kept. Older backup sets exceeding this number will be deleted whenever a new FULL backup is performed.
</p>
</div>
Expand Up @@ -99,7 +99,7 @@ public void testGetReferencingDiffBackups() {
@Test
public void testGetBackups() {
final List<String> backups = Utils.getBackupsAsDates(backupDir);
Assert.assertEquals(10, backups.size());
Assert.assertEquals(9, backups.size());
}

@Test
Expand Down

0 comments on commit 2ac9014

Please sign in to comment.