Ejemplo n.º 1
0
        private void CreateBackup(
            DatabaseSmugglerOptionsServerSide options, string backupFilePath,
            long?startDocumentEtag, DocumentsOperationContext context,
            Action <IOperationProgress> onProgress)
        {
            // the last etag is already included in the last backup
            startDocumentEtag = startDocumentEtag == null ? 0 : ++startDocumentEtag;

            using (var file = File.Open(backupFilePath, FileMode.CreateNew))
            {
                var smugglerSource      = new DatabaseSource(_database, startDocumentEtag.Value);
                var smugglerDestination = new StreamDestination(file, context, smugglerSource);
                var smuggler            = new Smuggler.Documents.DatabaseSmuggler(_database,
                                                                                  smugglerSource,
                                                                                  smugglerDestination,
                                                                                  _database.Time,
                                                                                  options: options,
                                                                                  result: _backupResult,
                                                                                  onProgress: onProgress,
                                                                                  token: TaskCancelToken.Token);

                smuggler.Execute();
                file.Flush(flushToDisk: true);
            }
        }
Ejemplo n.º 2
0
 private IOperationResult ExportDatabaseInternal(DatabaseSmugglerOptions options, Action <IOperationProgress> onProgress, DocumentsOperationContext context, OperationCancelToken token)
 {
     using (token)
     {
         var source      = new DatabaseSource(Database, 0);
         var destination = new StreamDestination(ResponseBodyStream(), context, source);
         var smuggler    = new DatabaseSmuggler(Database, source, destination, Database.Time, options, onProgress: onProgress, token: token.Token);
         return(smuggler.Execute());
     }
 }
Ejemplo n.º 3
0
        private InternalBackupResult CreateBackup(
            DatabaseSmugglerOptionsServerSide options, string backupFilePath, long?startDocumentEtag, long?startRaftIndex)
        {
            // the last etag is already included in the last backup
            var currentBackupResults = new InternalBackupResult();

            startDocumentEtag = startDocumentEtag == null ? 0 : ++startDocumentEtag;
            startRaftIndex    = startRaftIndex == null ? 0 : ++startRaftIndex;

            using (Stream fileStream = File.Open(backupFilePath, FileMode.CreateNew))
                using (var outputStream = GetOutputStream(fileStream))
                    using (_database.DocumentsStorage.ContextPool.AllocateOperationContext(out DocumentsOperationContext context))
                    {
                        var smugglerSource      = new DatabaseSource(_database, startDocumentEtag.Value, startRaftIndex.Value, _logger);
                        var smugglerDestination = new StreamDestination(outputStream, context, smugglerSource);
                        var smuggler            = new DatabaseSmuggler(_database,
                                                                       smugglerSource,
                                                                       smugglerDestination,
                                                                       _database.Time,
                                                                       options: options,
                                                                       result: _backupResult,
                                                                       onProgress: _onProgress,
                                                                       token: TaskCancelToken.Token);

                        smuggler.ExecuteAsync().Wait();

                        switch (outputStream)
                        {
                        case EncryptingXChaCha20Poly1305Stream encryptedStream:
                            encryptedStream.Flush(flushToDisk: true);
                            break;

                        case FileStream file:
                            file.Flush(flushToDisk: true);
                            break;

                        default:
                            throw new InvalidOperationException($" {outputStream.GetType()} not supported");
                        }

                        currentBackupResults.LastDocumentEtag         = smugglerSource.LastEtag;
                        currentBackupResults.LastDatabaseChangeVector = smugglerSource.LastDatabaseChangeVector;
                        currentBackupResults.LastRaftIndex            = smugglerSource.LastRaftIndex;

                        return(currentBackupResults);
                    }
        }
Ejemplo n.º 4
0
 private IOperationResult ExportDatabaseInternal(
     DatabaseSmugglerOptionsServerSide options,
     long startDocumentEtag,
     long startRaftIndex,
     Action <IOperationProgress> onProgress,
     DocumentsOperationContext context,
     OperationCancelToken token)
 {
     using (token)
     {
         var source = new DatabaseSource(Database, startDocumentEtag, startRaftIndex);
         using (var outputStream = GetOutputStream(ResponseBodyStream(), options))
         {
             var destination = new StreamDestination(outputStream, context, source);
             var smuggler    = new DatabaseSmuggler(Database, source, destination, Database.Time, options, onProgress: onProgress, token: token.Token);
             return(smuggler.Execute());
         }
     }
 }
Ejemplo n.º 5
0
        private SmugglerResult CreateBackup(DatabaseSmugglerOptionsServerSide options, string backupFilePath, long?startDocumentEtag, DocumentsOperationContext context)
        {
            // the last etag is already included in the last backup
            startDocumentEtag = startDocumentEtag == null ? 0 : ++startDocumentEtag;

            SmugglerResult result;

            using (var file = File.Open(backupFilePath, FileMode.CreateNew))
            {
                var smugglerSource      = new DatabaseSource(_database, startDocumentEtag.Value);
                var smugglerDestination = new StreamDestination(file, context, smugglerSource);
                var smuggler            = new DatabaseSmuggler(_database,
                                                               smugglerSource,
                                                               smugglerDestination,
                                                               _database.Time,
                                                               token: _cancellationToken.Token,
                                                               options: options);

                result = smuggler.Execute();
            }
            return(result);
        }
Ejemplo n.º 6
0
        public void FullBackupTo(string backupPath)
        {
            using (var file = SafeFileStream.Create(backupPath, FileMode.Create))
                using (var package = new ZipArchive(file, ZipArchiveMode.Create, leaveOpen: true))
                    using (_serverStore.ContextPool.AllocateOperationContext(out TransactionOperationContext context))
                        using (context.OpenReadTransaction())
                        {
                            var databaseRecord = _serverStore.Cluster.ReadDatabase(context, Name);
                            Debug.Assert(databaseRecord != null);

                            var zipArchiveEntry = package.CreateEntry(RestoreSettings.SmugglerValuesFileName, CompressionLevel.Optimal);
                            using (var zipStream = zipArchiveEntry.Open())
                            {
                                var smugglerSource = new DatabaseSource(this, 0);
                                using (DocumentsStorage.ContextPool.AllocateOperationContext(out DocumentsOperationContext ctx))
                                    using (ctx.OpenReadTransaction())
                                    {
                                        var smugglerDestination = new StreamDestination(zipStream, ctx, smugglerSource);
                                        var databaseSmugglerOptionsServerSide = new DatabaseSmugglerOptionsServerSide
                                        {
                                            AuthorizationStatus = AuthorizationStatus.DatabaseAdmin,
                                            OperateOnTypes      = DatabaseItemType.CompareExchange | DatabaseItemType.Identities
                                        };
                                        var smuggler = new DatabaseSmuggler(this,
                                                                            smugglerSource,
                                                                            smugglerDestination,
                                                                            this.Time,
                                                                            options: databaseSmugglerOptionsServerSide);

                                        smuggler.Execute();
                                    }
                            }

                            zipArchiveEntry = package.CreateEntry(RestoreSettings.SettingsFileName, CompressionLevel.Optimal);
                            using (var zipStream = zipArchiveEntry.Open())
                                using (var writer = new BlittableJsonTextWriter(context, zipStream))
                                {
                                    //TODO: encrypt this file using the MasterKey
                                    //http://issues.hibernatingrhinos.com/issue/RavenDB-7546

                                    writer.WriteStartObject();

                                    // save the database record
                                    writer.WritePropertyName(nameof(RestoreSettings.DatabaseRecord));
                                    var databaseRecordBlittable = EntityToBlittable.ConvertEntityToBlittable(databaseRecord, DocumentConventions.Default, context);
                                    context.Write(writer, databaseRecordBlittable);

                                    // save the database values (subscriptions, periodic backups statuses, etl states...)
                                    writer.WriteComma();
                                    writer.WritePropertyName(nameof(RestoreSettings.DatabaseValues));
                                    writer.WriteStartObject();

                                    var first  = true;
                                    var prefix = Helpers.ClusterStateMachineValuesPrefix(Name);
                                    foreach (var keyValue in ClusterStateMachine.ReadValuesStartingWith(context, prefix))
                                    {
                                        if (first == false)
                                        {
                                            writer.WriteComma();
                                        }

                                        first = false;

                                        var key = keyValue.Key.ToString().Substring(prefix.Length);
                                        writer.WritePropertyName(key);
                                        context.Write(writer, keyValue.Value);
                                    }

                                    writer.WriteEndObject();
                                    // end of values

                                    writer.WriteEndObject();
                                }

                            BackupMethods.Full.ToFile(GetAllStoragesForBackup(), package);

                            file.Flush(true); // make sure that we fully flushed to disk
                        }
        }