Skip to content

Commit

Permalink
o JENKINS-9171: add option so that build results may be backed up or …
Browse files Browse the repository at this point in the history
…not.

git-svn-id: https://svn.jenkins-ci.org/trunk/hudson/plugins/thinBackup@39165 71c3de6d-444a-0410-be80-ed276b4c234a
  • Loading branch information
msteinkogler committed Mar 28, 2011
1 parent 2ac9014 commit 39c9afb
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 23 deletions.
Expand Up @@ -108,7 +108,8 @@ public void doSaveSettings(final StaplerRequest res, final StaplerResponse rsp,
@QueryParameter("diffBackupSchedule") final String diffBackupSchedule,
@QueryParameter("nrMaxStoredFull") final String nrMaxStoredFull,
@QueryParameter("moveOldBackupsToZipFile") final boolean moveOldBackupsToZipFile,
@QueryParameter("cleanupDiff") final boolean cleanupDiff) throws IOException {
@QueryParameter("cleanupDiff") final boolean cleanupDiff,
@QueryParameter("backupBuildResults") final boolean backupBuildResults) throws IOException {
Hudson.getInstance().checkPermission(Hudson.ADMINISTER);

final ThinBackupPluginImpl plugin = ThinBackupPluginImpl.getInstance();
Expand All @@ -118,6 +119,7 @@ public void doSaveSettings(final StaplerRequest res, final StaplerResponse rsp,
plugin.setNrMaxStoredFull(nrMaxStoredFull);
plugin.setCleanupDiff(cleanupDiff);
plugin.setMoveOldBackupsToZipFile(moveOldBackupsToZipFile);
plugin.setBackupBuildResults(backupBuildResults);
plugin.save();
LOGGER.fine("Saving backup settings done.");
rsp.sendRedirect(res.getContextPath() + "/thinBackup");
Expand Down
Expand Up @@ -91,12 +91,12 @@ protected void backupNow(final BackupType type) {
LOGGER.fine("Wait until executors are idle to perform backup.");
Utils.waitUntilIdle();
new HudsonBackup(new File(backupPath), Hudson.getInstance().getRootDir(), type, maxStoredFull, cleanupDiff,
plugin.isMoveOldBackupsToZipFile()).backup();
plugin.isMoveOldBackupsToZipFile(), plugin.isBackupBuildResults()).backup();
} else {
LOGGER.warning("ThinBackup is not configured yet: No backup path set.");
}
} catch (final IOException e) {
String msg = MessageFormat
final String msg = MessageFormat
.format(
"Cannot perform a backup. Please be sure jenkins/hudson has write privileges in the configured backup path '{0}'.",
backupPath);
Expand Down
Expand Up @@ -39,6 +39,7 @@ public class ThinBackupPluginImpl extends Plugin {
private String nrMaxStoredFull;
private boolean cleanupDiff;
private boolean moveOldBackupsToZipFile;
private boolean backupBuildResults = true;

public ThinBackupPluginImpl() {
instance = this;
Expand Down Expand Up @@ -95,14 +96,22 @@ public boolean isCleanupDiff() {
return cleanupDiff;
}

public void setMoveOldBackupsToZipFile(boolean moveOldBackupsToZipFile) {
public void setMoveOldBackupsToZipFile(final boolean moveOldBackupsToZipFile) {
this.moveOldBackupsToZipFile = moveOldBackupsToZipFile;
}

public boolean isMoveOldBackupsToZipFile() {
return moveOldBackupsToZipFile;
}

public void setBackupBuildResults(final boolean backupBuildResults) {
this.backupBuildResults = backupBuildResults;
}

public boolean isBackupBuildResults() {
return backupBuildResults;
}

public FormValidation doCheckBackupPath(final StaplerRequest res, final StaplerResponse rsp,
@QueryParameter("value") final String value) {
if ((value == null) || value.isEmpty()) {
Expand Down
Expand Up @@ -58,15 +58,18 @@ public class HudsonBackup {
private final boolean cleanupDiff;
private final int nrMaxStoredFull; // stores nr of backup sets that will be kept
private final boolean moveOldBackupsToZipFile;
private final boolean backupBuildResults;

public HudsonBackup(final File backupRoot, final File hudsonHome, final BackupType backupType,
final int nrMaxStoredFull, final boolean cleanupDiff, final boolean moveOldBackupsToZipFile) {
final int nrMaxStoredFull, final boolean cleanupDiff, final boolean moveOldBackupsToZipFile,
final boolean backupBuildResults) {
hudson = Hudson.getInstance();

hudsonDirectory = hudsonHome;
this.cleanupDiff = cleanupDiff;
this.moveOldBackupsToZipFile = moveOldBackupsToZipFile;
this.nrMaxStoredFull = nrMaxStoredFull;
this.backupBuildResults = backupBuildResults;

this.backupRoot = backupRoot;
if (!backupRoot.exists()) {
Expand All @@ -93,13 +96,15 @@ public HudsonBackup(final File backupRoot, final File hudsonHome, final BackupTy
* date for which the backup directory should use
*/
HudsonBackup(final File backupRoot, final File hudsonHome, final BackupType backupType, final int nrMaxStoredFull,
final boolean cleanupDiff, final boolean moveOldBackupsToZipFile, final Date date) {
final boolean cleanupDiff, final boolean moveOldBackupsToZipFile, final boolean backupBuildResults,
final Date date) {
hudson = Hudson.getInstance();

hudsonDirectory = hudsonHome;
this.cleanupDiff = cleanupDiff;
this.moveOldBackupsToZipFile = moveOldBackupsToZipFile;
this.nrMaxStoredFull = nrMaxStoredFull;
this.backupBuildResults = backupBuildResults;

this.backupRoot = backupRoot;
if (!backupRoot.exists()) {
Expand Down Expand Up @@ -199,18 +204,20 @@ private void backupJobConfigFor(final String jobName, final File jobsDirectory,

private void backupBuildsFor(final String jobName, final File jobsDirectory, final File jobsBackupDirectory)
throws IOException {
final File buildsDir = new File(new File(jobsDirectory, jobName), BUILDS_DIR_NAME);
if (buildsDir.exists() && buildsDir.isDirectory()) {
final Collection<String> builds = Arrays.asList(buildsDir.list());
if (builds != null) {
for (final String build : builds) {
final File srcDir = new File(buildsDir, build);
if (!isSymLinkFile(srcDir)) {
final File destDir = new File(new File(new File(jobsBackupDirectory, jobName), BUILDS_DIR_NAME), build);
IOFileFilter buildFilter = FileFilterUtils.andFileFilter(FileFileFilter.FILE, getDiffFilter());
buildFilter = FileFilterUtils.andFileFilter(buildFilter,
FileFilterUtils.notFileFilter(FileFilterUtils.suffixFileFilter(".zip")));
FileUtils.copyDirectory(srcDir, destDir, buildFilter);
if (backupBuildResults) {
final File buildsDir = new File(new File(jobsDirectory, jobName), BUILDS_DIR_NAME);
if (buildsDir.exists() && buildsDir.isDirectory()) {
final Collection<String> builds = Arrays.asList(buildsDir.list());
if (builds != null) {
for (final String build : builds) {
final File srcDir = new File(buildsDir, build);
if (!isSymLinkFile(srcDir)) {
final File destDir = new File(new File(new File(jobsBackupDirectory, jobName), BUILDS_DIR_NAME), build);
IOFileFilter buildFilter = FileFilterUtils.andFileFilter(FileFileFilter.FILE, getDiffFilter());
buildFilter = FileFilterUtils.andFileFilter(buildFilter,
FileFilterUtils.notFileFilter(FileFilterUtils.suffixFileFilter(".zip")));
FileUtils.copyDirectory(srcDir, destDir, buildFilter);
}
}
}
}
Expand Down
Expand Up @@ -59,6 +59,12 @@
checked="${it.configuration.moveOldBackupsToZipFile}"
name="moveOldBackupsToZipFile"
/>

<f:optionalBlock title="Backup build results"
help="/plugin/thinBackup/help/help-backupBuildResults.html"
checked="${it.configuration.backupBuildResults}"
name="backupBuildResults"
/>
</f:section>

<!-- Save! -->
Expand Down
29 changes: 29 additions & 0 deletions src/main/webapp/help/help-backupBuildResults.html
@@ -0,0 +1,29 @@
<!--
The MIT License
Copyright (c) 2011, Borland (a Micro Focus Company), Matthias Steinkogler, Thomas Fuerer
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->

<div>
<p>
If set, build results will be backed up as well. This is potentially a lot of data, so think carefully about it.
</p>
</div>
2 changes: 1 addition & 1 deletion src/main/webapp/help/help-cleanupDiff.html
Expand Up @@ -24,6 +24,6 @@

<div>
<p>
If set, all old DIFF backups will be deleted whenever a new FULL backup is created.
If set, all old DIFF backups will be deleted whenever a new FULL backup has been created.
</p>
</div>
2 changes: 1 addition & 1 deletion src/main/webapp/help/help-nrMaxStoredFull.html
Expand Up @@ -24,6 +24,6 @@

<div>
<p>
Specifies how many backup sets will be kept. Older backup sets exceeding this number will be deleted whenever a new FULL backup is performed.
Specifies how many backup sets will be kept. Older backup sets exceeding this number will be deleted whenever a new FULL backup has been performed.
</p>
</div>
Expand Up @@ -34,7 +34,7 @@ public void testBackup() throws Exception {
final Calendar cal = Calendar.getInstance();
cal.set(Calendar.MINUTE, cal.get(Calendar.MINUTE - 10));

new HudsonBackup(backupDir, root, BackupType.FULL, -1, false, false, cal.getTime()).backup();
new HudsonBackup(backupDir, root, BackupType.FULL, -1, false, false, true, cal.getTime()).backup();

String[] list = backupDir.list();
Assert.assertEquals(1, list.length);
Expand All @@ -51,12 +51,31 @@ public void testBackup() throws Exception {
Assert.assertEquals(4, list.length);
}

@Test
public void testBackupWithoutBuildResults() throws Exception {
final Calendar cal = Calendar.getInstance();
cal.set(Calendar.MINUTE, cal.get(Calendar.MINUTE - 10));

new HudsonBackup(backupDir, root, BackupType.FULL, -1, false, false, false, cal.getTime()).backup();

String[] list = backupDir.list();
Assert.assertEquals(1, list.length);
final File backup = new File(backupDir, list[0]);
list = backup.list();
Assert.assertEquals(6, list.length);

final File job = new File(new File(backup, "jobs"), "test");
list = job.list();
Assert.assertEquals(1, list.length);
Assert.assertEquals("config.xml", list[0]);
}

@Test
public void testHudsonDiffBackup() throws Exception {
final Calendar cal = Calendar.getInstance();
cal.set(Calendar.MINUTE, cal.get(Calendar.MINUTE - 10));

new HudsonBackup(backupDir, root, BackupType.FULL, -1, false, false, cal.getTime()).backup();
new HudsonBackup(backupDir, root, BackupType.FULL, -1, false, false, true, cal.getTime()).backup();

// fake modification
backupDir.listFiles((FileFilter) FileFilterUtils.prefixFileFilter(BackupType.FULL.toString()))[0]
Expand All @@ -66,7 +85,7 @@ public void testHudsonDiffBackup() throws Exception {
globalConfigFile.setLastModified(System.currentTimeMillis() - 60000 * 120);
}

new HudsonBackup(backupDir, root, BackupType.DIFF, -1, false, false).backup();
new HudsonBackup(backupDir, root, BackupType.DIFF, -1, false, false, true).backup();
final File lastDiffBackup = backupDir.listFiles((FileFilter) FileFilterUtils.prefixFileFilter(BackupType.DIFF
.toString()))[0];
Assert.assertEquals(1, lastDiffBackup.list().length);
Expand Down

0 comments on commit 39c9afb

Please sign in to comment.