public static Serializable.ImportExportStructure ImportBackup(string configurationFile, bool importMetadata, Func <string> getPassword, Dictionary <string, string> advancedOptions) { // This removes the ID and DBPath from the backup configuration. Serializable.ImportExportStructure importedStructure = Backups.LoadConfiguration(configurationFile, importMetadata, getPassword); // This will create the Duplicati-server.sqlite database file if it doesn't exist. Duplicati.Server.Database.Connection connection = Program.GetDatabaseConnection(advancedOptions); 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."); } string 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); }
private void ImportBackup(RequestInfo info) { var output_template = "<html><body><script type=\"text/javascript\">var jso = 'JSO'; var rp = null; try { rp = parent['CBM']; } catch (e) {}; if (rp) { rp('MSG', jso); } else { alert; rp('MSG'); };</script></body></html>"; //output_template = "<html><body><script type=\"text/javascript\">alert('MSG');</script></body></html>"; try { var input = info.Request.Form; var cmdline = Library.Utility.Utility.ParseBool(input["cmdline"].Value, false); var import_metadata = Library.Utility.Utility.ParseBool(input["import_metadata"].Value, false); var direct = Library.Utility.Utility.ParseBool(input["direct"].Value, false); output_template = output_template.Replace("CBM", input["callback"].Value); if (cmdline) { info.Response.ContentType = "text/html"; info.BodyWriter.Write(output_template.Replace("MSG", "Import from commandline not yet implemented")); } else { var file = info.Request.Form.GetFile("config"); if (file == null) { throw new Exception("No file uploaded"); } Serializable.ImportExportStructure ipx = Backups.LoadConfiguration(file.Filename, import_metadata, () => input["passphrase"].Value); if (direct) { lock (Program.DataConnection.m_lock) { var basename = ipx.Backup.Name; var c = 0; while (c++ < 100 && Program.DataConnection.Backups.Any(x => x.Name.Equals(ipx.Backup.Name, StringComparison.OrdinalIgnoreCase))) { ipx.Backup.Name = basename + " (" + c.ToString() + ")"; } if (Program.DataConnection.Backups.Any(x => x.Name.Equals(ipx.Backup.Name, StringComparison.OrdinalIgnoreCase))) { info.BodyWriter.SetOK(); info.Response.ContentType = "text/html"; info.BodyWriter.Write(output_template.Replace("MSG", "There already exists a backup with the name: " + basename.Replace("\'", "\\'"))); } var err = Program.DataConnection.ValidateBackup(ipx.Backup, ipx.Schedule); if (!string.IsNullOrWhiteSpace(err)) { info.ReportClientError(err, System.Net.HttpStatusCode.BadRequest); return; } Program.DataConnection.AddOrUpdateBackupAndSchedule(ipx.Backup, ipx.Schedule); } info.Response.ContentType = "text/html"; info.BodyWriter.Write(output_template.Replace("MSG", "OK")); } else { using (var sw = new StringWriter()) { Serializer.SerializeJson(sw, ipx, true); output_template = output_template.Replace("'JSO'", sw.ToString()); } info.BodyWriter.Write(output_template.Replace("MSG", "Import completed, but a browser issue prevents loading the contents. Try using the direct import method instead.")); } } } catch (Exception ex) { Program.DataConnection.LogError("", "Failed to import backup", ex); info.Response.ContentType = "text/html"; info.BodyWriter.Write(output_template.Replace("MSG", ex.Message.Replace("\'", "\\'").Replace("\r", "\\r").Replace("\n", "\\n"))); } }