// Copyright (C) 2026, The Duplicati Team // https://duplicati.com, hello@duplicati.com // // 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. using System; using System.Collections.Generic; using System.Linq; using Duplicati.Server.Database; using Duplicati.Server.Serialization; using Duplicati.Server.Serialization.Interface; namespace Duplicati.Library.RestAPI; public static class BackupImportExportHandler { public static void RemovePasswords(IBackup backup) { backup.RemoveSensitiveInformation(); } public static byte[] ExportToJSON(Connection connection, IBackup backup, string passphrase) => ExportToJSON(connection.PrepareBackupForExport(backup), passphrase); public static byte[] ExportToJSON(Server.Serializable.ImportExportStructure ipx, string passphrase) { using var ms = new System.IO.MemoryStream(); using (var sw = new System.IO.StreamWriter(ms, leaveOpen: true)) Serializer.SerializeJson(sw, ipx, true); if (string.IsNullOrWhiteSpace(passphrase)) return ms.ToArray(); ms.Position = 0; using (var ms2 = new System.IO.MemoryStream()) using (var m = new Duplicati.Library.Encryption.AESEncryption(passphrase, new Dictionary())) { m.Encrypt(ms, ms2); return ms2.ToArray(); } } public static Server.Serializable.ImportExportStructure ImportBackup(Connection connection, string configurationFile, bool importMetadata, Func getPassword) { // This removes the ID and DBPath from the backup configuration. Server.Serializable.ImportExportStructure importedStructure = LoadConfiguration(configurationFile, importMetadata, getPassword); if (connection.Backups.Any(x => x.Name.Equals(importedStructure.Backup.Name, StringComparison.OrdinalIgnoreCase))) throw new InvalidOperationException($"A backup with the name {importedStructure.Backup.Name} already exists."); var error = connection.ValidateBackup(importedStructure.Backup, importedStructure.Schedule); if (!string.IsNullOrWhiteSpace(error)) throw new InvalidOperationException(error); // This creates a new ID and DBPath. connection.AddOrUpdateBackupAndSchedule(importedStructure.Backup, importedStructure.Schedule); return importedStructure; } public static Server.Serializable.ImportExportStructure LoadConfiguration(string filename, bool importMetadata, Func getPassword) { Server.Serializable.ImportExportStructure ipx; var buf = new byte[3]; using (var fs = System.IO.File.OpenRead(filename)) { Utility.Utility.ForceStreamRead(fs, buf, buf.Length); fs.Position = 0; if (buf[0] == 'A' && buf[1] == 'E' && buf[2] == 'S') { using (var m = new Encryption.AESEncryption(getPassword(), new Dictionary())) using (var m2 = m.Decrypt(fs)) using (var sr = new System.IO.StreamReader(m2)) ipx = Serializer.Deserialize(sr); } else { using (var sr = new System.IO.StreamReader(fs)) ipx = Serializer.Deserialize(sr); } } if (ipx.Backup == null) throw new Exception("No backup found in document"); if (ipx.Backup.Metadata == null) ipx.Backup.Metadata = new Dictionary(); if (!importMetadata) ipx.Backup.Metadata.Clear(); ipx.Backup.ID = null; ipx.Backup.DBPath = null; ipx.Backup.ConnectionStringID = -1; if (ipx.Schedule != null) ipx.Schedule.ID = -1; if (ipx.Backup.AdditionalTargetURLs != null) ipx.Backup.AdditionalTargetURLs = ipx.Backup.AdditionalTargetURLs.Select(x => { x.BackupID = -1; x.ConnectionStringID = -1; x.TargetUrlKey = Guid.NewGuid().ToString(); return x; }).ToArray(); return ipx; } }