Skip to content
Open
Show file tree
Hide file tree
Changes from 6 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
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@
<groupId>io.jenkins.plugins</groupId>
<artifactId>ionicons-api</artifactId>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>jobConfigHistory</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.jenkins</groupId>
<artifactId>configuration-as-code</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ public void doRestore(
final StaplerResponse2 rsp,
@QueryParameter("restoreBackupFrom") final String restoreBackupFrom,
@QueryParameter("restoreNextBuildNumber") final String restoreNextBuildNumber,
@QueryParameter("restorePlugins") final String restorePlugins)
@QueryParameter("restorePlugins") final String restorePlugins,
@QueryParameter("restoreConfigHistory") final String restoreConfigHistory)
throws IOException {
LOGGER.info("Starting restore operation.");

Expand All @@ -113,7 +114,9 @@ public void doRestore(
ThinBackupPluginImpl.get().getExpandedBackupPath(),
restoreFromDate,
"on".equals(restoreNextBuildNumber),
"on".equals(restorePlugins));
"on".equals(restorePlugins),
"on".equals(restoreConfigHistory)
);
hudsonRestore.restore();

LOGGER.info("Restore finished.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@

import com.google.common.base.Throwables;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Plugin;
import hudson.PluginWrapper;
import hudson.model.ItemGroup;
import hudson.model.Job;
import hudson.model.Run;
import hudson.model.TopLevelItem;
import hudson.plugins.jobConfigHistory.JobConfigHistory;
import hudson.util.RunList;
import java.io.File;
import java.io.FileNotFoundException;
Expand All @@ -31,6 +33,7 @@
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -87,7 +90,7 @@ public class HudsonBackup {
public static final String COMPLETED_BACKUP_FILE = "backup-completed.info";

private final ThinBackupPluginImpl plugin;
private final File hudsonHome;
private final File jenkinsHome;
private final File backupRoot;
private final File backupDirectory;
private final BackupType backupType;
Expand All @@ -108,7 +111,7 @@ public HudsonBackup(
ItemGroup<TopLevelItem> hudson) {
this.hudson = hudson;
this.plugin = plugin;
this.hudsonHome = plugin.getJenkinsHome();
this.jenkinsHome = plugin.getJenkinsHome();

final String excludedFilesRegex = plugin.getExcludedFilesRegex();
if ((excludedFilesRegex != null) && !excludedFilesRegex.trim().isEmpty()) {
Expand Down Expand Up @@ -170,7 +173,7 @@ public void backup() throws IOException {

LOGGER.fine(MessageFormat.format("Performing {0} backup.", backupType));

if (!hudsonHome.exists() || !hudsonHome.isDirectory()) {
if (!jenkinsHome.exists() || !jenkinsHome.isDirectory()) {
final String msg = "No Hudson directory found. Backup cannot be performed.";
LOGGER.severe(msg);
throw new FileNotFoundException(msg);
Expand All @@ -194,7 +197,15 @@ public void backup() throws IOException {
}

if (plugin.isBackupConfigHistory()) {
backupRootFolder(CONFIG_HISTORY_DIR_NAME);
final Plugin configHistoryPlugin = Jenkins.get().getPlugin("jobConfigHistory");
if (configHistoryPlugin != null && configHistoryPlugin.getWrapper().isActive()) {
final JobConfigHistory descriptor =
(JobConfigHistory) Jenkins.get().getDescriptor(JobConfigHistory.class);
if (descriptor != null) {
final File configuredHistoryRootDir = descriptor.getConfiguredHistoryRootDir();
backupConfigHistoryFolder(configuredHistoryRootDir.toString());
}
}
}

if (plugin.isBackupPluginArchives()) {
Expand Down Expand Up @@ -272,7 +283,7 @@ private void backupGlobalXmls() throws IOException {
getExcludedFilesFilter());
try {
FileUtils.copyDirectory(
hudsonHome, backupDirectory, ExistsAndReadableFileFilter.wrapperFilter(suffixFileFilter));
jenkinsHome, backupDirectory, ExistsAndReadableFileFilter.wrapperFilter(suffixFileFilter));
} catch (IOException e) {
if (plugin.isFailFast()) {
throw e;
Expand All @@ -286,7 +297,7 @@ private void backupGlobalXmls() throws IOException {

private void backupJobs() throws IOException {
LOGGER.fine("Backing up job specific configuration files...");
final File jobsDirectory = new File(hudsonHome.getAbsolutePath(), JOBS_DIR_NAME);
final File jobsDirectory = new File(jenkinsHome.getAbsolutePath(), JOBS_DIR_NAME);
final File jobsBackupDirectory = new File(backupDirectory.getAbsolutePath(), JOBS_DIR_NAME);

backupJobsDirectory(jobsDirectory, jobsBackupDirectory);
Expand Down Expand Up @@ -415,7 +426,7 @@ private void backupAdditionalFiles() throws IOException {
FileFilterUtils.and(getFileAgeDiffFilter(), getExcludedFilesFilter())));

try {
FileUtils.copyDirectory(hudsonHome, backupDirectory, ExistsAndReadableFileFilter.wrapperFilter(filter));
FileUtils.copyDirectory(jenkinsHome, backupDirectory, ExistsAndReadableFileFilter.wrapperFilter(filter));
} catch (IOException e) {
if (plugin.isFailFast()) {
throw e;
Expand Down Expand Up @@ -465,7 +476,7 @@ private File createBackupDirectory(File jobBackupdirectory, File jobDirectory, F
*/
private List<File> findAllConfigurations(File dir) throws UncheckedIOException {
Collection<File> listFiles =
FileUtils.listFiles(dir, FileFilterUtils.nameFileFilter(CONFIG_XML), TrueFileFilter.INSTANCE);
FileUtils.listFiles(dir, FileFilterUtils.nameFileFilter(CONFIG_XML), TrueFileFilter.TRUE);

List<File> confs = new ArrayList<>();
for (File file : listFiles) {
Expand Down Expand Up @@ -583,7 +594,7 @@ private void backupBuildArchive(final File buildSrcDir, final File buildDestDir)

private void backupRootFolder(String folderName) throws IOException {
try {
backupRootFolder(folderName, TrueFileFilter.INSTANCE);
backupRootFolder(folderName, TrueFileFilter.TRUE);
} catch (IOException e) {
if (plugin.isFailFast()) {
throw e;
Expand All @@ -594,8 +605,24 @@ private void backupRootFolder(String folderName) throws IOException {
}
}

private void backupConfigHistoryFolder(String folderName) throws IOException {
final File srcDirectory = new File(folderName);
if (srcDirectory.exists() && srcDirectory.isDirectory()) {
LOGGER.log(Level.FINE, "Backing up {0}...", folderName);
Path pathAbsolute = Paths.get(folderName);
Path pathBase = Paths.get(jenkinsHome.getAbsolutePath());
Path pathRelative = pathBase.relativize(pathAbsolute);
final File destDirectory = new File(backupDirectory.getAbsolutePath(), String.valueOf(pathRelative));
IOFileFilter filter =
FileFilterUtils.and(TrueFileFilter.TRUE, getFileAgeDiffFilter(), getExcludedFilesFilter());
filter = FileFilterUtils.or(filter, DirectoryFileFilter.DIRECTORY);
FileUtils.copyDirectory(srcDirectory, destDirectory, ExistsAndReadableFileFilter.wrapperFilter(filter));
LOGGER.log(Level.FINE, "DONE backing up {0}.", folderName);
}
}

private void backupRootFolder(String folderName, IOFileFilter fileFilter) throws IOException {
final File srcDirectory = new File(hudsonHome.getAbsolutePath(), folderName);
final File srcDirectory = new File(jenkinsHome.getAbsolutePath(), folderName);
if (srcDirectory.exists() && srcDirectory.isDirectory()) {
LOGGER.log(Level.FINE, "Backing up {0}...", folderName);
final File destDirectory = new File(backupDirectory.getAbsolutePath(), folderName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,24 +62,27 @@ public class HudsonRestore {
private static final Logger LOGGER = Logger.getLogger("hudson.plugins.thinbackup");

private final String backupPath;
private final File hudsonHome;
private final File jenkinsHome;
private final Date restoreFromDate;
private final boolean restoreNextBuildNumber;
private final boolean restorePlugins;
private final boolean restoreConfigHistory;
private final Map<String, List<Plugin>> availablePluginLocations;

public HudsonRestore(
final File hudsonConfigurationPath,
final String backupPath,
final Date restoreFromDate,
final boolean restoreNextBuildNumber,
final boolean restorePlugins) {
this.hudsonHome = hudsonConfigurationPath;
final boolean restorePlugins,
final boolean restoreConfigHistory) {
this.jenkinsHome = hudsonConfigurationPath;
this.backupPath = backupPath;
this.restoreFromDate = restoreFromDate;
this.restoreNextBuildNumber = restoreNextBuildNumber;
this.restorePlugins = restorePlugins;
this.availablePluginLocations = new HashMap<>();
this.restoreConfigHistory = restoreConfigHistory;
}

public void restore() {
Expand Down Expand Up @@ -168,6 +171,7 @@ private boolean restoreFromZipFile() throws IOException {

private void restore(final File toRestore) throws IOException {
final IOFileFilter nextBuildNumberFileFilter = FileFilterUtils.nameFileFilter("nextBuildNumber");
final IOFileFilter configHistoryFileFilter = FileFilterUtils.notFileFilter(FileFilterUtils.prefixFileFilter("config-history"));
final IOFileFilter noBackupCompletedFile =
FileFilterUtils.notFileFilter(FileFilterUtils.nameFileFilter(COMPLETED_BACKUP_FILE));
IOFileFilter restoreNextBuildNumberFilter;
Expand All @@ -176,7 +180,7 @@ private void restore(final File toRestore) throws IOException {
restoreNextBuildNumberFilter = noBackupCompletedFile;

final Collection<File> restore =
FileUtils.listFiles(toRestore, nextBuildNumberFileFilter, TrueFileFilter.INSTANCE);
FileUtils.listFiles(toRestore, nextBuildNumberFileFilter, TrueFileFilter.TRUE);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually this is the same, only the newer syntax.

final Map<String, Integer> nextBuildNumbers = new HashMap<>();
for (final File file : restore) {
try (BufferedReader reader = new BufferedReader(new FileReader(file, StandardCharsets.UTF_8))) {
Expand All @@ -185,7 +189,7 @@ private void restore(final File toRestore) throws IOException {
}

final Collection<File> current =
FileUtils.listFiles(hudsonHome, nextBuildNumberFileFilter, TrueFileFilter.INSTANCE);
FileUtils.listFiles(jenkinsHome, nextBuildNumberFileFilter, TrueFileFilter.TRUE);
for (final File file : current) {
try (BufferedReader reader = new BufferedReader(new FileReader(file, StandardCharsets.UTF_8))) {
final int currentBuildNumber = Integer.parseInt(reader.readLine());
Expand All @@ -201,7 +205,12 @@ private void restore(final File toRestore) throws IOException {
FileFilterUtils.notFileFilter(nextBuildNumberFileFilter), noBackupCompletedFile);
}

FileUtils.copyDirectory(toRestore, this.hudsonHome, restoreNextBuildNumberFilter, true);
if (!restoreConfigHistory) {
restoreNextBuildNumberFilter = FileFilterUtils.and(
restoreNextBuildNumberFilter, configHistoryFileFilter);
}

FileUtils.copyDirectory(toRestore, this.jenkinsHome, restoreNextBuildNumberFilter, true);

if (restorePlugins) {
restorePlugins(toRestore);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@
<f:checkbox name="restorePlugins"/>
</f:entry>

<f:entry title="${%restore_config_history}" help="/plugin/thinBackup/help/help-restoreConfigHistory.html">
<f:checkbox name="restoreConfigHistory" default="true"/>
</f:entry>

</f:section>

<f:bottomButtonBar>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ restore_configuration = Restore Configuration
restore_next_build_number = Restore next build number file (if found in backup)
restore_options = Restore options
restore_plugins = Restore plugins
restore_config_history = Restore config-history (if found in backup)
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
restore_backup_from = Backup wiederherstellen von:
restore_configuration = Restore Konfiguration
restore_next_build_number = N�chste Build Nummer Datein wiederherstellen (falls im Backup vorhanden)
restore_next_build_number = N�chste Build Nummer Dateien wiederherstellen (falls im Backup vorhanden)
restore_options = Restore Optionen
restore_plugins = Plugins wiederherstellen
restore_config_history = Config-history wiederherstellen (falls im Backup vorhanden)
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@

<div>
<p>
If this option is enabled, the directory <em>config-history</em> will also be backed up.
If this option is enabled, the directory which is configured within the config-history plugin will also be backed up (only if plugin is installed).
</p>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@

<div>
<p>
Falls diese Option aktiviert ist, wird das Verzeichnis <em>config-history</em> zum Backup hinzugefügt.
Falls diese Option aktiviert ist, wird das Verzeichnis, welches im config-history konfiguriert ist, zum Backup hinzugefügt (nur wenn das Plugin installiert ist).
</p>
</div>
30 changes: 30 additions & 0 deletions src/main/webapp/help/help-restoreConfigHistory.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!--
The MIT License

Copyright (c) 2024 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>
If this option is enabled, the config-history gets restored.<br/>
This will only work, if the config-history plugin is installed, otherwise it cannot read the config folder.
</p>
</div>
30 changes: 30 additions & 0 deletions src/main/webapp/help/help-restoreConfigHistory_de.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!--
The MIT License

Copyright (c) 2024 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, wird die config-history aus dem Backup wiederhergestellt.<br/>
Es wird nur funktionieren, falls das config-history Plugin installiert ist, sonst kann der Konfigurationsordner nicht ausgelesen werden.
</p>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ void testRestoreFromFolder(JenkinsRule r) throws Exception {
assertEquals(0, jobList.length);

// now do the restore without build number
HudsonRestore hudsonRestore = new HudsonRestore(rootDir, backupDir.getAbsolutePath(), date, false, false);
HudsonRestore hudsonRestore = new HudsonRestore(rootDir, backupDir.getAbsolutePath(), date, false, false, false);
hudsonRestore.restore();

// verify jobs are back
Expand All @@ -83,7 +83,7 @@ void testRestoreFromFolder(JenkinsRule r) throws Exception {
assertFalse(new File(test2rootDir, "nextBuildNumber").exists());

// restore from backup INCLUDING build number
hudsonRestore = new HudsonRestore(rootDir, backupDir.getAbsolutePath(), date, true, false);
hudsonRestore = new HudsonRestore(rootDir, backupDir.getAbsolutePath(), date, true, false, false);
hudsonRestore.restore();

assertTrue(new File(test2rootDir, "nextBuildNumber").exists());
Expand Down Expand Up @@ -146,7 +146,7 @@ void testRestoreFromZip(JenkinsRule r) throws Exception {

// now do the restore without build number
HudsonRestore hudsonRestore =
new HudsonRestore(rootDir, backupDir.getAbsolutePath(), restoreFromDate, false, false);
new HudsonRestore(rootDir, backupDir.getAbsolutePath(), restoreFromDate, false, false, false);
hudsonRestore.restore();

// verify jobs are back
Expand All @@ -157,7 +157,7 @@ void testRestoreFromZip(JenkinsRule r) throws Exception {
assertFalse(new File(test2rootDir, "nextBuildNumber").exists());

// restore from backup INCLUDING build number
hudsonRestore = new HudsonRestore(rootDir, backupDir.getAbsolutePath(), restoreFromDate, true, false);
hudsonRestore = new HudsonRestore(rootDir, backupDir.getAbsolutePath(), restoreFromDate, true, false, false);
hudsonRestore.restore();

assertTrue(new File(test2rootDir, "nextBuildNumber").exists());
Expand All @@ -166,7 +166,7 @@ void testRestoreFromZip(JenkinsRule r) throws Exception {
@Test
void testLogsForRestoringWithoutBackupPath(JenkinsRule r) {
try (LogRecorder l = new LogRecorder().capture(3).record("hudson.plugins.thinbackup", Level.SEVERE)) {
final HudsonRestore hudsonRestore = new HudsonRestore(null, null, null, false, false);
final HudsonRestore hudsonRestore = new HudsonRestore(null, null, null, false, false, false);
hudsonRestore.restore();
assertThat(
l, recorded(Level.SEVERE, containsString("Backup path not specified for restoration. Aborting.")));
Expand All @@ -176,7 +176,7 @@ void testLogsForRestoringWithoutBackupPath(JenkinsRule r) {
@Test
void testLogsForRestoringWithoutRestoreFromDate(JenkinsRule r) {
try (LogRecorder l = new LogRecorder().capture(3).record("hudson.plugins.thinbackup", Level.SEVERE)) {
final HudsonRestore hudsonRestore = new HudsonRestore(null, "/var/backup", null, false, false);
final HudsonRestore hudsonRestore = new HudsonRestore(null, "/var/backup", null, false, false, false);
hudsonRestore.restore();
assertThat(
l,
Expand Down