예제 #1
0
        public void GetFileNames_MockAllDirectories_ShouldReturnFiveFiles()
        {
            IOptions <BackupStoreSettings> options = Options.Create(new BackupStoreSettings
                                                                    (
                                                                        new List <string> {
                "BAK"
            },
                                                                        new List <string> {
                @"C:\"
            },
                                                                        null,
                                                                        null,
                                                                        true
                                                                    ));

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

            IEnumerable <string> backupFiles = store.BackupFiles;

            backupFiles.Should().HaveCount(5);
            backupFiles.Should().Contain(@"c:\backfile1.bak");
            backupFiles.Should().Contain(@"c:\backup\backfile1.bak");
            backupFiles.Should().Contain(@"c:\backup\fullbak\backfile1.bak");
            backupFiles.Should().Contain(@"c:\backup\diffbak\backfile1.bak");
            backupFiles.Should().Contain(@"c:\backup\logbak\backfile1.bak");
        }
        public void BackupMediaHeaders_ShouldReturnFullMediaHeader()
        {
            IOptions <BackupStoreSettings> options = Options.Create(new BackupStoreSettings {
                BackupFileExtensions = { "BAK" }, BackupPaths = { Path.Combine(AppContext.BaseDirectory, "Bak") }
            });

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

            List <BackupMediaHeader> backupMediaHeaders = store.BackupMediaHeaders;

            backupMediaHeaders.Should().HaveCount(5);
            BackupMediaHeader media = backupMediaHeaders[0];

            media.MediaSetId.Should().Be(new Guid("68727d35-8696-462f-a312-25d596dd0705"));
            media.MediaDate.Should().Be(new DateTime(2020, 7, 27, 9, 32, 5));
            media = backupMediaHeaders[1];
            media.MediaSetId.Should().Be(new Guid("02375638-8846-470a-aa7f-5e803c3e102e"));
            media.MediaDate.Should().Be(new DateTime(2020, 7, 25, 12, 45, 00));
            media = backupMediaHeaders[2];
            media.MediaSetId.Should().Be(new Guid("7876e286-e78e-48b7-8ccd-3c102c2ca74d"));
            media.MediaDate.Should().Be(new DateTime(2020, 8, 3, 16, 15, 53));
            media = backupMediaHeaders[3];
            media.MediaSetId.Should().Be(new Guid("c844e72e-6d3c-4402-ba6a-bc618c53a233"));
            media.MediaDate.Should().Be(new DateTime(2020, 8, 3, 12, 19, 56));
            media = backupMediaHeaders[4];
            media.MediaSetId.Should().Be(new Guid("22288477-48c3-4ce2-a553-ce6390d82968"));
            media.MediaDate.Should().Be(new DateTime(2020, 8, 3, 14, 30, 58));
        }
예제 #3
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
                },
예제 #4
0
        public void BackupHeaders_ShouldReturnFullBackupHeader()
        {
            IOptions <BackupStoreSettings> options = Options.Create(new BackupStoreSettings {
                BackupFileExtensions = { "BAK" }, BackupPaths = { Path.Combine(AppContext.BaseDirectory, "Bak") }
            });

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

            List <BackupHeader> backupHeaders = store.BackupHeaders;

            backupHeaders.Should().HaveCount(6);
            BackupHeader info = backupHeaders[0];

            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, 7, 27, 9, 32, 5));
            info.BackupFinishDate.Should().Be(new DateTime(2020, 7, 27, 9, 32, 5));
            info.FirstLSN.Should().Be(37000000102900001M);
            info.LastLSN.Should().Be(37000000103200001M);
            info.Position.Should().Be(1);
            info.SoftwareVersionMajor.Should().Be(15);
            info.Values.Count.Should().Be(56);
            info = backupHeaders[1];
            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 = backupHeaders[2];
            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, 8, 3, 16, 15, 53));
            info.BackupFinishDate.Should().Be(new DateTime(2020, 8, 3, 16, 15, 53));
            info.FirstLSN.Should().Be(37000000136800001M);
            info.LastLSN.Should().Be(37000000137100001M);
            info.Position.Should().Be(1);
            info.SoftwareVersionMajor.Should().Be(15);
            info.Values.Count.Should().Be(56);
            info = backupHeaders[3];
            info.BackupType.Should().Be(BackupType.Log);
            info.DatabaseName.Should().Be("Test");
            info.BackupName.Should().Be("Test-Log Database Backup");
            info.StartDate.Should().Be(new DateTime(2020, 8, 3, 12, 19, 56));
            info.BackupFinishDate.Should().Be(new DateTime(2020, 8, 3, 12, 19, 56));
            info.FirstLSN.Should().Be(37000000019700001M);
            info.LastLSN.Should().Be(37000000128700001M);
            info.Position.Should().Be(1);
            info.SoftwareVersionMajor.Should().Be(15);
            info.Values.Count.Should().Be(56);
        }
        public void BackupFiles_MockC_ShouldReturnOneFile()
        {
            IOptions <BackupStoreSettings> options = Options.Create(new BackupStoreSettings {
                BackupFileExtensions = { "BAK" }, BackupPaths = { @"C:\" }
            });

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

            List <string> backupFiles = store.BackupFiles;

            backupFiles.Should().HaveCount(1);
            backupFiles.Should().Contain(@"c:\backfile1.bak");
        }
        public void GetFileNames_MockFsEmpty_ShouldReturnEmptyList()
        {
            var fileSystem = new MockFileSystem(new Dictionary <string, MockFileData>());
            IOptions <BackupStoreSettings> options = Options.Create(new BackupStoreSettings {
                BackupFileExtensions = { "BAK" }, BackupPaths = { @"C:\" }
            });

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

            List <string> backupFiles = store.BackupFiles;

            backupFiles.Should().BeEmpty();
        }
예제 #7
0
        public void BackupFiles_MockC_ShouldReturnOneFile()
        {
            IOptions <BackupStoreSettings> options = Options.Create(new BackupStoreSettings {
                BackupFileExtensions = { "BAK" }, BackupPaths = { @"c:\backup\fullbak" }
            });

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

            IEnumerable <string> backupFiles = store.BackupFiles;

            backupFiles.Should().HaveCount(1);
            backupFiles.Should().Contain(@"c:\backup\fullbak\backfile1.bak");
        }
예제 #8
0
        public override Task <int> InvokeAsync(InvocationContext context)
        {
            var store = new BackupStore(Options.Server, Microsoft.Extensions.Options.Options.Create(Options.GetBackupStoreSettings()));

            IEnumerable <BackupHeader>?backups = store.GetBackups
                                                 (
                Options.SourceServer,
                Options.SourceDatabase,
                Options.BackupType switch
            {
                BackupRestoreType.Full => BackupType.Full,
                BackupRestoreType.Diff => BackupType.Differential,
                BackupRestoreType.Log => BackupType.Log,
                _ => null
            });
        public void BackupFiles_ShouldReturnFiles()
        {
            IOptions <BackupStoreSettings> options = Options.Create(new BackupStoreSettings {
                BackupFileExtensions = { "BAK" }, BackupPaths = { Path.Combine(AppContext.BaseDirectory, "Bak") }
            });

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

            List <string> backupFiles = store.BackupFiles;

            backupFiles.Should().HaveCount(5);
            backupFiles.Should().ContainMatch(@"*TestFull1.bak");
            backupFiles.Should().ContainMatch(@"*TestFull2.bak");
            backupFiles.Should().ContainMatch(@"*TestDiff1_1.bak");
            backupFiles.Should().ContainMatch(@"*TestLog1_1_1.bak");
            backupFiles.Should().ContainMatch(@"*TestLog1_1_2_Diff_1_2.bak");
        }
예제 #10
0
        public void GetFileNames_MockAllDirectories_ShouldReturnFiveFiles()
        {
            IOptions <BackupStoreSettings> options = Options.Create(new BackupStoreSettings {
                BackupFileExtensions = { "BAK" }, BackupPaths = { @"C:\", @"C:\Backup", @"C:\Backup\FULLBAK", @"C:\Backup\diffbak", @"C:\Backup\LogBak" }
            });

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

            List <string> backupFiles = store.BackupFiles;

            backupFiles.Should().HaveCount(5);
            backupFiles.Should().Contain(@"c:\backfile1.bak");
            backupFiles.Should().Contain(@"c:\backup\backfile1.bak");
            backupFiles.Should().Contain(@"c:\backup\fullbak\backfile1.bak");
            backupFiles.Should().Contain(@"c:\backup\diffbak\backfile1.bak");
            backupFiles.Should().Contain(@"c:\backup\logbak\backfile1.bak");
        }
예제 #11
0
        public void GetFileNames_MockFsEmpty_ShouldReturnEmptyList()
        {
            var directoryService = new SqlBackup.Fakes.StubIDirectoryService()
            {
                GetFilesIEnumerableOfStringIEnumerableOfStringBoolean =
                    (paths, extensions, recurse) => Array.Empty <string>()
            };
            IOptions <BackupStoreSettings> options = Options.Create(new BackupStoreSettings {
                BackupFileExtensions = { "BAK" }, BackupPaths = { @"C:\" }
            });

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

            IEnumerable <string> backupFiles = store.BackupFiles;

            backupFiles.Should().BeEmpty();
        }
예제 #12
0
        public void BackupDatabaseFiles_ShouldReturnFullBackupFiles()
        {
            IOptions <BackupStoreSettings> options = Options.Create(new BackupStoreSettings {
                BackupFileExtensions = { "BAK" }, BackupPaths = { Path.Combine(AppContext.BaseDirectory, "Bak") }
            });

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

            List <BackupDatabaseFile> backupFiles = store.BackupDatabaseFiles;

            backupFiles.Should().HaveCount(10);
            BackupDatabaseFile file = backupFiles[0];

            file.LogicalName.Should().Be("Test");
            file.PhysicalName.Should().EndWith("Test.mdf");
            file = backupFiles[1];
            file.LogicalName.Should().Be("Test_log");
            file.PhysicalName.Should().EndWith("Test_log.ldf");
            file = backupFiles[2];
            file.LogicalName.Should().Be("Test");
            file.PhysicalName.Should().EndWith("Test.mdf");
            file = backupFiles[3];
            file.LogicalName.Should().Be("Test_log");
            file.PhysicalName.Should().EndWith("Test_log.ldf");
            file = backupFiles[4];
            file.LogicalName.Should().Be("Test");
            file.PhysicalName.Should().EndWith("Test.mdf");
            file = backupFiles[5];
            file.LogicalName.Should().Be("Test_log");
            file.PhysicalName.Should().EndWith("Test_log.ldf");
            file = backupFiles[6];
            file.LogicalName.Should().Be("Test");
            file.PhysicalName.Should().EndWith("Test.mdf");
            file = backupFiles[7];
            file.LogicalName.Should().Be("Test_log");
            file.PhysicalName.Should().EndWith("Test_log.ldf");
            file = backupFiles[8];
            file.LogicalName.Should().Be("Test");
            file.PhysicalName.Should().EndWith("Test.mdf");
            file = backupFiles[9];
            file.LogicalName.Should().Be("Test_log");
            file.PhysicalName.Should().EndWith("Test_log.ldf");
        }
예제 #13
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));
            }
        }
예제 #14
0
        public void GetLatestFull_ReturnFull2()
        {
            IOptions <BackupStoreSettings> options = Options.Create(new BackupStoreSettings {
                BackupFileExtensions = { "BAK" }, BackupPaths = { Path.Combine(AppContext.BaseDirectory, "Bak") }
            });

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

            BackupHeader info = store.GetLatestFull("DESKTOP-NVACFK6", "Test");

            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, 8, 3, 16, 15, 53));
            info.FinishDate.Should().Be(new DateTime(2020, 8, 3, 16, 15, 53));
            info.FirstLSN.Should().Be(37000000136800001M);
            info.LastLSN.Should().Be(37000000137100001M);
            info.Position.Should().Be(1);
            info.SoftwareVersionMajor.Should().Be(15);
            info.Values.Count.Should().Be(56);
        }
예제 #15
0
        public void GetLatestDiffWithFull_ReturnFull1Diff2()
        {
            IOptions <BackupStoreSettings> options = Options.Create(new BackupStoreSettings {
                BackupFileExtensions = { "BAK" }, BackupPaths = { Path.Combine(AppContext.BaseDirectory, "Bak") }
            });

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

            IEnumerable <BackupHeader> infos = store.GetLatestDiffWithFull("DESKTOP-NVACFK6", "Test");

            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.FinishDate.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.FinishDate.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);
        }