Exemplo n.º 1
0
        public static void Main(string[] args)
        {
            if (args.Length != 3)
            {
                throw new ArgumentException($"Incorrect number of input arguments.  {ConfigurationImporter.usageString}");
            }

            string configurationFile = args[0];
            Dictionary <string, string> importOptions = Duplicati.Library.Utility.CommandLineParser.ExtractOptions(args.Skip(1).ToList());

            if (!importOptions.TryGetValue("import-metadata", out string importMetadataString))
            {
                throw new ArgumentException($"Invalid import-metadata argument.  {ConfigurationImporter.usageString}");
            }
            bool importMetadata = Duplicati.Library.Utility.Utility.ParseBool(importMetadataString, false);

            if (!importOptions.TryGetValue("server-datafolder", out string serverDatafolder))
            {
                throw new ArgumentException($"Invalid server-datafolder argument.  {ConfigurationImporter.usageString}");
            }

            Dictionary <string, string> advancedOptions = new Dictionary <string, string>
            {
                { "server-datafolder", serverDatafolder }
            };

            ImportExportStructure importedStructure = Backups.ImportBackup(configurationFile, importMetadata, () => ConfigurationImporter.ReadPassword($"Password for {configurationFile}: "), advancedOptions);

            Console.WriteLine($"Imported \"{importedStructure.Backup.Name}\" with ID {importedStructure.Backup.ID} and local database at {importedStructure.Backup.DBPath}.");
        }
Exemplo n.º 2
0
        public void RoundTrip()
        {
            Dictionary <string, string> metadata = new Dictionary <string, string> {
                { "SourceFilesCount", "1" }
            };
            Dictionary <string, string> advancedOptions = new Dictionary <string, string> {
                { "server-datafolder", this.serverDatafolder }
            };

            using (Program.DataConnection = Program.GetDatabaseConnection(advancedOptions))
            {
                // Unencrypted file, don't import metadata.
                string unencryptedWithoutMetadata = Path.Combine(this.serverDatafolder, Path.GetRandomFileName());
                File.WriteAllBytes(unencryptedWithoutMetadata, Server.WebServer.RESTMethods.Backup.ExportToJSON(this.CreateBackup("unencrypted without metadata", "user", "password", metadata), null));
                Backups.ImportBackup(unencryptedWithoutMetadata, false, () => null, advancedOptions);
                Assert.AreEqual(1, Program.DataConnection.Backups.Length);
                Assert.AreEqual(0, Program.DataConnection.Backups[0].Metadata.Count);

                // Unencrypted file, import metadata.
                string unencryptedWithMetadata = Path.Combine(this.serverDatafolder, Path.GetRandomFileName());
                File.WriteAllBytes(unencryptedWithMetadata, Server.WebServer.RESTMethods.Backup.ExportToJSON(this.CreateBackup("unencrypted with metadata", "user", "password", metadata), null));
                Backups.ImportBackup(unencryptedWithMetadata, true, () => null, advancedOptions);
                Assert.AreEqual(2, Program.DataConnection.Backups.Length);
                Assert.AreEqual(metadata.Count, Program.DataConnection.Backups[1].Metadata.Count);

                // Encrypted file, don't import metadata.
                string encryptedWithoutMetadata = Path.Combine(this.serverDatafolder, Path.GetRandomFileName());
                string passphrase = "abcde";
                File.WriteAllBytes(encryptedWithoutMetadata, Server.WebServer.RESTMethods.Backup.ExportToJSON(this.CreateBackup("encrypted without metadata", "user", "password", metadata), passphrase));
                Backups.ImportBackup(encryptedWithoutMetadata, false, () => passphrase, advancedOptions);
                Assert.AreEqual(3, Program.DataConnection.Backups.Length);
                Assert.AreEqual(0, Program.DataConnection.Backups[2].Metadata.Count);

                // Encrypted file, import metadata.
                string encryptedWithMetadata = Path.Combine(this.serverDatafolder, Path.GetRandomFileName());
                File.WriteAllBytes(encryptedWithMetadata, Server.WebServer.RESTMethods.Backup.ExportToJSON(this.CreateBackup("encrypted with metadata", "user", "password", metadata), passphrase));
                Backups.ImportBackup(encryptedWithMetadata, true, () => passphrase, advancedOptions);
                Assert.AreEqual(4, Program.DataConnection.Backups.Length);
                Assert.AreEqual(metadata.Count, Program.DataConnection.Backups[3].Metadata.Count);

                // Encrypted file, incorrect passphrase.
                Assert.Throws(Is.InstanceOf <Exception>(), () => Backups.ImportBackup(encryptedWithMetadata, true, () => passphrase + " ", advancedOptions));
            }
        }