// Copyright (C) 2015, The Duplicati Team // http://www.duplicati.com, info@duplicati.com // // This library is free software; you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as // published by the Free Software Foundation; either version 2.1 of the // License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, but // WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA using System; using System.Linq; using System.Collections.Generic; using Duplicati.Server.Serialization; using System.IO; namespace Duplicati.Server.WebServer.RESTMethods { public class Backups : IRESTMethodGET, IRESTMethodPOST, IRESTMethodDocumented { public class AddOrUpdateBackupData { public Boolean IsUnencryptedOrPassphraseStored { get; set;} public Database.Schedule Schedule { get; set;} public Database.Backup Backup { get; set;} } public void GET(string key, RequestInfo info) { var schedules = Program.DataConnection.Schedules; var backups = Program.DataConnection.Backups; var all = from n in backups select new AddOrUpdateBackupData { IsUnencryptedOrPassphraseStored = Program.DataConnection.IsUnencryptedOrPassphraseStored(long.Parse(n.ID)), Backup = (Database.Backup)n, Schedule = (from x in schedules where x.Tags != null && x.Tags.Contains("ID=" + n.ID) select (Database.Schedule)x).FirstOrDefault() }; info.BodyWriter.OutputOK(all.ToArray()); } private void ImportBackup(RequestInfo info) { var output_template = "
"; //output_template = ""; 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"))); } } public static Serializable.ImportExportStructure LoadConfiguration(string filename, bool importMetadata, Func