Exemple #1
0
        public static byte[] ExportToJSON(IBackup backup, string passphrase)
        {
            Serializable.ImportExportStructure ipx = Program.DataConnection.PrepareBackupForExport(backup);

            byte[] data;
            using (MemoryStream ms = new System.IO.MemoryStream())
            {
                using (StreamWriter sw = new System.IO.StreamWriter(ms))
                {
                    Serializer.SerializeJson(sw, ipx, true);

                    if (!string.IsNullOrWhiteSpace(passphrase))
                    {
                        ms.Position = 0;
                        using (MemoryStream ms2 = new System.IO.MemoryStream())
                        {
                            using (Library.Encryption.AESEncryption m = new Duplicati.Library.Encryption.AESEncryption(passphrase, new Dictionary <string, string>()))
                            {
                                m.Encrypt(ms, ms2);
                                data = ms2.ToArray();
                            }
                        }
                    }
                    else
                    {
                        data = ms.ToArray();
                    }
                }
            }

            return(data);
        }
Exemple #2
0
        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);
        }
Exemple #3
0
        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")));
            }
        }