Exemplo n.º 1
0
        public override Task <int> InvokeAsync(InvocationContext context)
        {
            var store = new BackupStore(Options.Server, Microsoft.Extensions.Options.Options.Create(Options.GetBackupStoreSettings()));
            IEnumerable <BackupHeader>?backups;

            if (Options.LatestOnly)
            {
                if (string.IsNullOrWhiteSpace(Options.SourceServer) || string.IsNullOrWhiteSpace(Options.SourceDatabase))
                {
                    throw new InvalidOperationException(SqlBackup.Properties.Resources.SourceRequiredWithLastestOption);
                }
                backups = store.GetLatestBackupSet(
                    Options.SourceServer,
                    Options.SourceDatabase,
                    Options.BackupType == BackupRestoreType.Diff || Options.BackupType == BackupRestoreType.All,
                    Options.BackupType == BackupRestoreType.Log || Options.BackupType == BackupRestoreType.All,
                    Options.Before);
            }
            else
            {
                backups = store.GetBackups
                          (
                    Options.SourceServer,
                    Options.SourceDatabase,
                    Options.BackupType switch
                {
                    BackupRestoreType.Full => BackupType.Full,
                    BackupRestoreType.Diff => BackupType.Differential,
                    BackupRestoreType.Log => BackupType.Log,
                    _ => null
                },
Exemplo n.º 2
0
        /// <summary>
        /// Execute the list command
        /// </summary>
        public override Task <int> InvokeAsync(InvocationContext context)
        {
            var store = new BackupStore(Options.Server, Microsoft.Extensions.Options.Options.Create(Options.GetBackupStoreSettings()));

            _ = Options.SourceServer ?? throw new NotSupportedException("The source server must be defined.");
            _ = Options.SourceDatabase ?? throw new NotSupportedException("The source database must be defined.");
            IEnumerable <BackupHeader> backups = store.GetLatestBackupSet
                                                 (
                Options.SourceServer,
                Options.SourceDatabase,
                Options.BackupType == BackupRestoreType.Diff || Options.BackupType == BackupRestoreType.All,
                Options.BackupType == BackupRestoreType.Log || Options.BackupType == BackupRestoreType.All,
                Options.Before
                                                 );

            DatabaseRestore?restore = backups.Any() ? new DatabaseRestore(
                Options.Server,
                Options.Database,
                backups,
                null,
                ServiceProvider.GetRequiredService <ILogger <DatabaseRestore> >()) : null;
            var view = new RestoreView(backups, Options);

            view.Initialize();
            if (!Options.Silent)
            {
                _ = context.Console ?? throw new ArgumentException("Console property is null", nameof(context));
                using var screen = new ScreenView(ConsoleRenderer, context.Console)
                      {
                          Child = view
                      };
                screen.Render();
            }
            if (restore != null)
            {
                restore.Execute();
                return(Task.FromResult(0));
            }
            else
            {
                return(Task.FromResult(-1));
            }
        }
Exemplo n.º 3
0
        public void GetLatestBefore03_08_2020_15h_ReturnFull1Diff2()
        {
            IOptions <BackupStoreSettings> options = Options.Create(new BackupStoreSettings {
                BackupFileExtensions = { "BAK" }, BackupPaths = { Path.Combine(AppContext.BaseDirectory, "Bak") }
            });

            var store = new BackupStore(GetServer(), options);

            IEnumerable <BackupHeader> infos = store.GetLatestBackupSet("DESKTOP-NVACFK6", "Test", true, false, new DateTime(2020, 8, 3, 15, 0, 0));

            infos.Should().HaveCount(2);
            BackupHeader info = infos.First();

            info.Should().NotBeNull();
            info.BackupType.Should().Be(BackupType.Full);
            info.DatabaseName.Should().Be("Test");
            info.BackupName.Should().Be("Test-Full Database Backup");
            info.StartDate.Should().Be(new DateTime(2020, 7, 25, 12, 45, 00));
            info.BackupFinishDate.Should().Be(new DateTime(2020, 7, 25, 12, 45, 00));
            info.FirstLSN.Should().Be(37000000091400001M);
            info.LastLSN.Should().Be(37000000091700001M);
            info.Position.Should().Be(1);
            info.SoftwareVersionMajor.Should().Be(15);
            info.Values.Count.Should().Be(56);
            info = infos.Last();
            info.Should().NotBeNull();
            info.BackupType.Should().Be(BackupType.Differential);
            info.DatabaseName.Should().Be("Test");
            info.BackupName.Should().Be("Test-Diff Database Backup");
            info.StartDate.Should().Be(new DateTime(2020, 8, 3, 14, 47, 32));
            info.BackupFinishDate.Should().Be(new DateTime(2020, 8, 3, 14, 47, 32));
            info.FirstLSN.Should().Be(37000000133600001M);
            info.LastLSN.Should().Be(37000000133900001M);
            info.Position.Should().Be(2);
            info.SoftwareVersionMajor.Should().Be(15);
            info.Values.Count.Should().Be(56);
        }