Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(config): move current backup set to zip file by flag #148

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public class ThinBackupPluginImpl extends GlobalConfiguration {
private static final int VERY_HIGH_TIMEOUT = 12 * 60;
private boolean cleanupDiff = false;
private boolean moveOldBackupsToZipFile = false;
private boolean moveCurrentBackupToZipFile = false;
private boolean backupBuildResults = true;
private boolean backupBuildArchive = false;
private boolean backupPluginArchives = false;
Expand Down Expand Up @@ -100,6 +101,7 @@ public File getJenkinsHome() {

@Override
public boolean configure(StaplerRequest req, JSONObject json) throws FormException {
LOGGER.warning(json.toString());

try (BulkChange bc = new BulkChange(this)) {
req.bindJSON(this, json);
Expand Down Expand Up @@ -203,6 +205,16 @@ public boolean isMoveOldBackupsToZipFile() {
return moveOldBackupsToZipFile;
}

@DataBoundSetter
public void setMoveCurrentBackupToZipFile(final boolean moveCurrentBackupToZipFile) {
this.moveCurrentBackupToZipFile = moveCurrentBackupToZipFile;
save();
}

public boolean isMoveCurrentBackupToZipFile() {
return moveCurrentBackupToZipFile;
}

@DataBoundSetter
public void setBackupBuildResults(final boolean backupBuildResults) {
this.backupBuildResults = backupBuildResults;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -685,74 +685,77 @@

private void moveOldBackupsToZipFile(final File currentBackup) {
if (plugin.isMoveOldBackupsToZipFile()) {
final ZipperThread zipperThread = new ZipperThread(backupRoot, currentBackup);
final ZipperThread zipperThread =
new ZipperThread(backupRoot, currentBackup, plugin.isMoveCurrentBackupToZipFile());
zipperThread.start();
}
}

private IOFileFilter getFileAgeDiffFilter() {
IOFileFilter result = FileFilterUtils.trueFileFilter();

if (backupType == BackupType.DIFF) {
result = FileFilterUtils.ageFileFilter(latestFullBackupDate, false);
}

return result;
}

private IOFileFilter getExcludedFilesFilter() {
IOFileFilter result = FileFilterUtils.trueFileFilter();

if (excludedFilesRegexPattern != null) {
result = FileFilterUtils.notFileFilter(new RegexFileFilter(excludedFilesRegexPattern));
}

return result;
}

private Date getLatestFullBackupDate() {
final List<File> fullBackups = Utils.getBackupTypeDirectories(backupRoot, BackupType.FULL);
if ((fullBackups == null) || (fullBackups.isEmpty())) {
return null;
}

Date result = new Date(0);
for (final File fullBackup : fullBackups) {
final Date tmp = Utils.getDateFromBackupDirectory(fullBackup);
if (tmp != null) {
if (tmp.after(result)) {
result = tmp;
}
} else {
LOGGER.log(
Level.INFO,
"Cannot parse directory name ' {0} ', thus ignoring it when getting latest backup date.",
fullBackup.getName());
}
}

return result;
}

/**
* Zipping the old backups is done in a thread so the rest of Hudson/Jenkins is
* not blocked.
*/
public static class ZipperThread extends Thread {
private static final Logger LOGGER = Logger.getLogger("hudson.plugins.thinbackup");

private final File backupRoot;
private final File currentBackup;
private final boolean moveCurrentBackupToZipFile;

public ZipperThread(final File backupRoot, final File currentBackup) {
public ZipperThread(final File backupRoot, final File currentBackup, final boolean moveCurrentBackupToZipFile) {
this.backupRoot = backupRoot;
this.currentBackup = currentBackup;
this.moveCurrentBackupToZipFile = moveCurrentBackupToZipFile;
}

@Override
public void run() {
LOGGER.fine("Starting zipper thread...");
Utils.moveOldBackupsToZipFile(backupRoot, currentBackup);
Utils.moveOldBackupsToZipFile(backupRoot, currentBackup, moveCurrentBackupToZipFile);

Check warning on line 758 in src/main/java/org/jvnet/hudson/plugins/thinbackup/backup/HudsonBackup.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 688-758 are not covered by tests
LOGGER.fine("DONE zipping.");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -388,14 +388,16 @@
* @param currentBackup
* specified which backup should be omitted from being moved. If null, all backups are moved to ZIP files.
*/
public static void moveOldBackupsToZipFile(final File backupRoot, final File currentBackup) {
public static void moveOldBackupsToZipFile(
final File backupRoot, final File currentBackup, final boolean moveCurrentBackupToZipFile) {
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())) {
if ((!backupSet.containsDirectory(currentBackup) || moveCurrentBackupToZipFile)

Check warning on line 399 in src/main/java/org/jvnet/hudson/plugins/thinbackup/utils/Utils.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 399 is only partially covered, 3 branches are missing
&& (!backupSet.isInZipFile())) {

Check warning on line 400 in src/main/java/org/jvnet/hudson/plugins/thinbackup/utils/Utils.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 400 is only partially covered, one branch is missing
final File zippedBackupSet = backupSet.zipTo(backupRoot);
++numberOfZippedBackupSets;
if (zippedBackupSet != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,13 @@
<f:checkbox/>
</f:entry>

<f:entry title="${%move}" field="moveOldBackupsToZipFile">
<f:checkbox/>
</f:entry>
<f:optionalBlock title="${%move}"
field="moveOldBackupsToZipFile"
inline="true">
<f:entry title="${%move_current}" field="moveCurrentBackupToZipFile">
<f:checkbox/>
</f:entry>
</f:optionalBlock>

<f:entry title="${%fail_fast}" field="failFast">
<f:checkbox/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ force_quite_minutes = Force Jenkins to quiet mode after specified minutes
full_backup_schedule = Backup schedule for full backups
max_backup_full = Max number of backup sets
move = Move old backups to ZIP files
move_current = Move current backup to ZIP file
thin_backup_configuration=ThinBackup Configuration
wait_for_idle = Wait until Jenkins is idle to perform a backup
fail_fast = Stop the backup as soon as an exception occurs in the file handling
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ force_quite_minutes = Zwinge Jenkins in den quiet mode nach x Minuten
full_backup_schedule = Plan für vollständiges Backup
max_backup_full = Maximale Anzahl an Backup Sätzen
move = Alte Backups in ZIP Dateien verschieben
move_current = Aktuelle Backup in ZIP Datei verschieben
thin_backup_configuration = ThinBackup Konfiguration
wait_for_idle = Warte bis Jenkins idle ist um ein Backup durchzuführen
fail_fast = Stoppe das Backup, sobald eine Exception in der Behandlung der Dateien auftritt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<!--
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 this is checked, then whenever a new full backup will be moved to ZIP file.
ZIP file will contain one backup set, i.e. one full backup and any diff backups referencing it.
The filename will identify the timeframe where the backups are included (i.e. the timestamp of the full backup and the timestamp of the latest diff backup).
</p>
<p>
<strong>NOTE:</strong><br/>
The setting "<em>Max number of backup sets</em>" applies to backup ZIP files created by thinBackup as well.
Also note that in case "<em>Clean up differential backups</em>" is checked it is performed before zipping is run (i.e. there will be no diff backups in the ZIP files).
</p>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<!--
The MIT License

Copyright (c) 2023, Stefan Spieker

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>
Falls diese Option aktiviert ist, werden jedem neuen Full Backup in ein ZIP file verschoben.
ZIP-Datei wird ein Vollbackup und alle differenziellen Backups die darauf referenzieren enthalten.
Der Dateiname wird das Zeitintervall identifizieren aus dem Backups enthalten sind. Also den Zeitstempel des Vollbackups und des letzten inkrementellen Backups.
</p>
<p>
<strong>HINWEIS:</strong><br/>
Die Einstellung <em>Maximale Anzahl an Backup Sätzen</em> gilt ebenfalls für ZIP-Dateien, die von thinBackup erstellt wurden.
Im Falle dass <em>Differentielle Backups bereinigen</em> aktiviert ist, werden, werden vor dem zippen die differenziellen Backups gelöscht.
Es werden also keine differenziellen Backups in der ZIP-Datei vorhanden sein.
</p>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public void should_support_configuration_as_code() {

assertFalse(thinBackupPluginConfig.isCleanupDiff());
assertFalse(thinBackupPluginConfig.isMoveOldBackupsToZipFile());
assertFalse(thinBackupPluginConfig.isMoveCurrentBackupToZipFile());
assertFalse(thinBackupPluginConfig.isBackupBuildArchive());
assertFalse(thinBackupPluginConfig.isBackupPluginArchives());
assertFalse(thinBackupPluginConfig.isBackupUserContents());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public void testRestoreFromZip() throws IOException, InterruptedException, Parse
Assertions.assertEquals(1, backupsAsDates.size());

// create zips with older backups
Utils.moveOldBackupsToZipFile(backupDir, null);
Utils.moveOldBackupsToZipFile(backupDir, null, false);

// check that backupset is present
File[] files = backupDir.listFiles();
Expand Down
1 change: 1 addition & 0 deletions src/test/resources/configuration-as-code.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ unclassified:
forceQuietModeTimeout: 120
fullBackupSchedule: "0 12 * * 1"
moveOldBackupsToZipFile: false
moveCurrentBackupToZipFile: false
nrMaxStoredFull: -1
waitForIdle: true
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<forceQuietModeTimeout>1</forceQuietModeTimeout>
<cleanupDiff>false</cleanupDiff>
<moveOldBackupsToZipFile>true</moveOldBackupsToZipFile>
<moveCurrentBackupToZipFile>false</moveCurrentBackupToZipFile>
<backupBuildResults>true</backupBuildResults>
<backupBuildArchive>false</backupBuildArchive>
<backupPluginArchives>false</backupPluginArchives>
Expand Down