Skip to content
Open
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
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 @@
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,8 @@
ThinBackupPluginImpl.get().getExpandedBackupPath(),
restoreFromDate,
"on".equals(restoreNextBuildNumber),
"on".equals(restorePlugins));
"on".equals(restorePlugins),
"on".equals(restoreConfigHistory));

Check warning on line 118 in src/main/java/org/jvnet/hudson/plugins/thinbackup/ThinBackupMgmtLink.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 117-118 are not covered by tests
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 @@ -69,7 +72,6 @@
public static final String JOBS_DIR_NAME = "jobs";
public static final String USERS_DIR_NAME = "users";
public static final String ARCHIVE_DIR_NAME = "archive";
public static final String CONFIG_HISTORY_DIR_NAME = "config-history";
public static final String USERSCONTENTS_DIR_NAME = "userContent";
public static final String NEXT_BUILD_NUMBER_FILE_NAME = "nextBuildNumber";
public static final String PLUGINS_DIR_NAME = "plugins";
Expand All @@ -87,7 +89,7 @@
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 +110,7 @@
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 +172,7 @@

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

if (!hudsonHome.exists() || !hudsonHome.isDirectory()) {
if (!jenkinsHome.exists() || !jenkinsHome.isDirectory()) {

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

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 175 is only partially covered, 2 branches are missing
final String msg = "No Hudson directory found. Backup cannot be performed.";
LOGGER.severe(msg);
throw new FileNotFoundException(msg);
Expand All @@ -194,7 +196,15 @@
}

if (plugin.isBackupConfigHistory()) {
backupRootFolder(CONFIG_HISTORY_DIR_NAME);
final Plugin configHistoryPlugin = Jenkins.get().getPlugin("jobConfigHistory");
if (configHistoryPlugin != null && configHistoryPlugin.getWrapper().isActive()) {

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

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 200 is only partially covered, 2 branches are missing
final JobConfigHistory descriptor =
(JobConfigHistory) Jenkins.get().getDescriptor(JobConfigHistory.class);
if (descriptor != null) {

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

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 203 is only partially covered, one branch is missing
final File configuredHistoryRootDir = descriptor.getConfiguredHistoryRootDir();
backupConfigHistoryFolder(configuredHistoryRootDir.toString());
}
}
}

if (plugin.isBackupPluginArchives()) {
Expand Down Expand Up @@ -272,7 +282,7 @@
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 +296,7 @@

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 +425,8 @@
FileFilterUtils.and(getFileAgeDiffFilter(), getExcludedFilesFilter())));

try {
FileUtils.copyDirectory(hudsonHome, backupDirectory, ExistsAndReadableFileFilter.wrapperFilter(filter));
FileUtils.copyDirectory(
jenkinsHome, backupDirectory, ExistsAndReadableFileFilter.wrapperFilter(filter));

Check warning on line 429 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 428-429 are not covered by tests
} catch (IOException e) {
if (plugin.isFailFast()) {
throw e;
Expand Down Expand Up @@ -465,7 +476,7 @@
*/
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 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 backupConfigHistoryFolder(String folderName) throws IOException {
final File srcDirectory = new File(folderName);
if (srcDirectory.exists() && srcDirectory.isDirectory()) {

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

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 610 is only partially covered, 2 branches are missing
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,8 @@ 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 +181,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 +190,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 +206,11 @@ 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 @@ -22,7 +22,7 @@
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;

@WithJenkins
class TestBackupWithCloudBeesFolder {
class TestBackupWithFolder {

@TempDir
private File tmpFolder;
Expand Down
Loading