Exemplo n.º 1
0
        public async Task ShouldExportAndImportConfigurations()
        {
            using (var exportStream = new MemoryStream())
            {
                int countOfConfigurations;

                using (var store = NewStore())
                {
                    for (int i = 0; i < 100; i++)
                    {
                        await store.AsyncFilesCommands.Configuration.SetKeyAsync("items/" + i, new RavenJObject
                        {
                            {
                                "test", "value"
                            },
                            {
                                "test-array", new RavenJArray
                                {
                                    "item-1", "item-2", "item-3"
                                }
                            }
                        });
                    }

                    countOfConfigurations = (await store.AsyncFilesCommands.Configuration.GetKeyNamesAsync(0, 200)).Length;

                    var exportOptions = new SmugglerExportOptions <FilesConnectionStringOptions>
                    {
                        From = new FilesConnectionStringOptions
                        {
                            Url = store.Url,
                            DefaultFileSystem = store.DefaultFileSystem
                        },
                        ToStream = exportStream
                    };

                    await new SmugglerFilesApi().ExportData(exportOptions);
                }

                using (var import = NewStore(1))
                {
                    exportStream.Position = 0;

                    var importOptions = new SmugglerImportOptions <FilesConnectionStringOptions>
                    {
                        FromStream = exportStream,
                        To         = new FilesConnectionStringOptions()
                        {
                            Url = import.Url,
                            DefaultFileSystem = import.DefaultFileSystem
                        }
                    };

                    await new SmugglerFilesApi().ImportData(importOptions);

                    Assert.Equal(countOfConfigurations, (await import.AsyncFilesCommands.Configuration.GetKeyNamesAsync(0, 200)).Length);

                    for (int i = 0; i < 100; i++)
                    {
                        Assert.NotNull(await import.AsyncFilesCommands.Configuration.GetKeyAsync <RavenJObject>("items/" + i));
                    }
                }
            }
        }
Exemplo n.º 2
0
        public async Task Foo()
        {
            {
                #region smuggler_api_1

                SmugglerFilesApi smugglerApi = new SmugglerFilesApi();

                var exportOptions = new SmugglerExportOptions <FilesConnectionStringOptions>
                {
                    ToFile = "dump.ravenfs",
                    From   = new FilesConnectionStringOptions
                    {
                        Url = "http://localhost:8080",
                        DefaultFileSystem = "NorthwindFS"
                    }
                };

                var exportResult = await smugglerApi.ExportData(exportOptions);

                #endregion
            }

            {
                #region smuggler_api_2

                SmugglerFilesApi smugglerApi = new SmugglerFilesApi();

                var importOptions = new SmugglerImportOptions <FilesConnectionStringOptions>
                {
                    FromFile = "dump.ravenfs",
                    To       = new FilesConnectionStringOptions
                    {
                        Url = "http://localhost:8080",
                        DefaultFileSystem = "NewNorthwindFS"
                    }
                };

                await smugglerApi.ImportData(importOptions);

                #endregion
            }

            {
                #region smuggler_api_3
                // export files
                // from NorthwindFS file system
                // found on http://localhost:8080
                // and import them to NewNorthwindFS
                // found on the same server
                SmugglerFilesApi smugglerApi = new SmugglerFilesApi();

                var betweenOptions = new SmugglerBetweenOptions <FilesConnectionStringOptions>
                {
                    From = new FilesConnectionStringOptions
                    {
                        Url = "http://localhost:8080",
                        DefaultFileSystem = "NorthwindFS"
                    },
                    To = new FilesConnectionStringOptions
                    {
                        Url = "http://localhost:8080",
                        DefaultFileSystem = "NewNorthwindFS"
                    }
                };

                await smugglerApi.Between(betweenOptions);

                #endregion
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Export counter data to specified destination (a file or a stream)
        /// </summary>
        /// <param name="exportOptions">options to specify the source and destination of the export</param>
        /// <exception cref="UnauthorizedAccessException">The caller does not have the required permission.-or- specified a file that is read-only. </exception>
        /// <exception cref="DirectoryNotFoundException">The specified path is invalid (for example, it is on an unmapped drive). </exception>
        /// <exception cref="IOException">An I/O error occurred while creating the file. </exception>
        /// <exception cref="SmugglerExportException">Encapsulates exception that happens when actually exporting data. See InnerException for details.</exception>
        public async Task <CounterOperationState> ExportData(SmugglerExportOptions <CounterConnectionStringOptions> exportOptions)
        {
            if (exportOptions.From == null)
            {
                throw new ArgumentNullException("exportOptions.From");
            }

            if (string.IsNullOrWhiteSpace(exportOptions.ToFile) && exportOptions.ToStream == null)
            {
                throw new ArgumentException("ToFile or ToStream property in options must be non-null");
            }

            var result       = new CounterOperationState();
            var exportFolder = string.Empty;

            if (Options.Incremental)
            {
                ShowProgress("Starting incremental export..");
                exportFolder = CalculateExportFile(exportOptions, exportFolder);
            }
            else
            {
                ShowProgress("Starting full export...");
            }

            SmugglerExportException lastException = null;

            var ownedStream = exportOptions.ToStream == null;
            var stream      = exportOptions.ToStream ?? File.Create(exportOptions.ToFile);

            if (ownedStream)
            {
                ShowProgress("Export to dump file " + exportOptions.ToFile);
            }
            try
            {
                using (var counterStore = new CounterStore
                {
                    Url = exportOptions.From.Url,
                    Name = exportOptions.From.CounterStoreId,
                    Credentials = new OperationCredentials(exportOptions.From.ApiKey, exportOptions.From.Credentials)
                })
                    using (var gZipStream = new GZipStream(stream, CompressionMode.Compress, leaveOpen: true))
                        using (var streamWriter = new StreamWriter(gZipStream))
                        {
                            counterStore.Initialize();
                            var jsonWriter = new JsonTextWriter(streamWriter)
                            {
                                Formatting = Formatting.Indented
                            };
                            jsonWriter.WriteStartObject();
                            jsonWriter.WritePropertyName(Options.Incremental ? "CountersDeltas" : "CounterSnapshots"); //also for human readability
                            jsonWriter.WriteStartArray();

                            try
                            {
                                if (Options.Incremental)
                                {
                                    await ExportIncrementalData(counterStore, exportFolder, jsonWriter).WithCancellation(CancellationToken).ConfigureAwait(false);
                                }
                                else
                                {
                                    await ExportFullData(counterStore, jsonWriter).WithCancellation(CancellationToken).ConfigureAwait(false);
                                }
                            }
                            catch (SmugglerExportException e)
                            {
                                Debug.Assert(e.Data.Keys.Cast <string>().Contains("LastEtag"));
                                result.LastWrittenEtag = (long)e.Data["LastEtag"];
                                lastException          = e;
                                var operation = Options.Incremental ? "Incremental" : "Full";
                                ShowProgress($"{operation} Export failed. {e}");
                            }

                            jsonWriter.WriteEndArray();
                            jsonWriter.WriteEndObject();
                            streamWriter.Flush();
                        }

                if (lastException != null)
                {
                    throw lastException;
                }
                return(result);
            }
            finally
            {
                if (ownedStream && stream != null)
                {
                    stream.Flush();
                    stream.Dispose();
                    ShowProgress("Finished export and closed file...");
                }
                else
                {
                    ShowProgress("Finished export...");
                }
            }
        }
Exemplo n.º 4
0
        public async Task <HttpResponseMessage> Export()
        {
            try
            {
                // Setup an export using RavenDb's Smuggler API
                var exportTimestamp = DateTime.Now;
                var fileName        = $"augurk-{exportTimestamp.ToString("yyyy-dd-M-HHmmss")}.bak";
                var options         = new SmugglerDatabaseOptions
                {
                    OperateOnTypes = ItemType.Documents,
                    Filters        = new List <FilterSetting>
                    {
                        new FilterSetting
                        {
                            Path        = "@metadata.@id",
                            ShouldMatch = false,
                            Values      = new List <string>
                            {
                                ConfigurationManager.DOCUMENT_KEY,
                                CustomizationManager.DOCUMENT_KEY,
                            }
                        }
                    }
                };

                // Determine the appropriate import method to use
                SmugglerDatabaseApiBase      exporter;
                RavenConnectionStringOptions connectionStringOptions;
                if (Database.DocumentStore is EmbeddableDocumentStore embeddableDocumentStore)
                {
                    exporter = new DatabaseDataDumper(embeddableDocumentStore.DocumentDatabase, options);
                    connectionStringOptions = new EmbeddedRavenConnectionStringOptions();
                }
                else
                {
                    exporter = new SmugglerDatabaseApi(options);
                    connectionStringOptions = new RavenConnectionStringOptions()
                    {
                        Url = Database.DocumentStore.Url
                    };
                }

                var exportOptions = new SmugglerExportOptions <RavenConnectionStringOptions>()
                {
                    ToFile = Path.Combine(Path.GetTempPath(), fileName),
                    From   = connectionStringOptions
                };

                // Perform the export
                await exporter.ExportData(exportOptions);

                // Stream the backup back to the client
                var result = new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new ByteArrayContent(File.ReadAllBytes(exportOptions.ToFile))
                };

                result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
                {
                    FileName = fileName
                };

                result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                return(result);
            }
            catch
            {
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "An exception occured while generating export."));
            }
        }
Exemplo n.º 5
0
		public async Task Foo()
		{
			{
				#region smuggler_api_1

				SmugglerFilesApi smugglerApi = new SmugglerFilesApi();

				var exportOptions = new SmugglerExportOptions<FilesConnectionStringOptions>
				{
					ToFile = "dump.ravenfs",
					From = new FilesConnectionStringOptions
					{
						Url = "http://localhost:8080",
						DefaultFileSystem = "NorthwindFS"
					}
				};

				var exportResult = await smugglerApi.ExportData(exportOptions);

				#endregion
			}

			{
				#region smuggler_api_2

				SmugglerFilesApi smugglerApi = new SmugglerFilesApi();

				var importOptions = new SmugglerImportOptions<FilesConnectionStringOptions>
				{
					FromFile = "dump.ravenfs",
					To = new FilesConnectionStringOptions
					{
						Url = "http://localhost:8080",
						DefaultFileSystem = "NewNorthwindFS"
					}
				};

				await smugglerApi.ImportData(importOptions);

				#endregion
			}

			{
				#region smuggler_api_3
				// export files
				// from NorthwindFS file system
				// found on http://localhost:8080
				// and import them to NewNorthwindFS
				// found on the same server
				SmugglerFilesApi smugglerApi = new SmugglerFilesApi();

				var betweenOptions = new SmugglerBetweenOptions<FilesConnectionStringOptions>
				{
					From = new FilesConnectionStringOptions
					{
						Url = "http://localhost:8080",
						DefaultFileSystem = "NorthwindFS"
					},
					To = new FilesConnectionStringOptions
					{
						Url = "http://localhost:8080",
						DefaultFileSystem = "NewNorthwindFS"
					}
				};

				await smugglerApi.Between(betweenOptions);
				#endregion
			}
		}