Ejemplo n.º 1
0
        //[Fact]
        public void ScriptBackupTest()
        {
            DisasterRecoveryService service   = new DisasterRecoveryService();
            string             databaseName   = "testbackup_" + new Random().Next(10000000, 99999999);
            SqlTestDb          testDb         = SqlTestDb.CreateNew(TestServerType.OnPrem, false, databaseName);
            var                liveConnection = LiveConnectionHelper.InitLiveConnectionInfo(databaseName);
            DatabaseTaskHelper helper         = AdminService.CreateDatabaseTaskHelper(liveConnection.ConnectionInfo, databaseExists: true);
            SqlConnection      sqlConn        = ConnectionService.OpenSqlConnection(liveConnection.ConnectionInfo);
            string             backupPath     = GetDefaultBackupFullPath(service, databaseName, helper.DataContainer, sqlConn);

            BackupInfo backupInfo = CreateDefaultBackupInfo(databaseName,
                                                            BackupType.Full,
                                                            new List <string>()
            {
                backupPath
            },
                                                            new Dictionary <string, int>()
            {
                { backupPath, (int)DeviceType.File }
            });
            BackupOperation backupOperation = CreateBackupOperation(service, liveConnection.ConnectionInfo.OwnerUri, backupInfo, helper.DataContainer, sqlConn);

            // Generate script for backup
            service.ScriptBackup(backupOperation);
            string script = backupOperation.ScriptContent;

            Assert.True(!string.IsNullOrEmpty(script));

            // Execute the script
            testDb.RunQuery(script);

            VerifyAndCleanBackup(backupPath);
            testDb.Cleanup();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Test creating backup with advanced options set.
        /// </summary>
        //[Fact]
        public void ScriptBackupWithAdvancedOptionsTest()
        {
            DisasterRecoveryService service   = new DisasterRecoveryService();
            string             databaseName   = "testbackup_" + new Random().Next(10000000, 99999999);
            SqlTestDb          testDb         = SqlTestDb.CreateNew(TestServerType.OnPrem, false, databaseName);
            var                liveConnection = LiveConnectionHelper.InitLiveConnectionInfo(databaseName);
            DatabaseTaskHelper helper         = AdminService.CreateDatabaseTaskHelper(liveConnection.ConnectionInfo, databaseExists: true);
            SqlConnection      sqlConn        = ConnectionService.OpenSqlConnection(liveConnection.ConnectionInfo);
            string             backupPath     = GetDefaultBackupFullPath(service, databaseName, helper.DataContainer, sqlConn);

            string certificateName         = CreateCertificate(testDb);
            string cleanupCertificateQuery = string.Format(CleanupCertificateQueryFormat, certificateName);

            BackupInfo backupInfo = CreateDefaultBackupInfo(databaseName,
                                                            BackupType.Full,
                                                            new List <string>()
            {
                backupPath
            },
                                                            new Dictionary <string, int>()
            {
                { backupPath, (int)DeviceType.File }
            });

            backupInfo.FormatMedia         = true;
            backupInfo.SkipTapeHeader      = true;
            backupInfo.Initialize          = true;
            backupInfo.MediaName           = "backup test media";
            backupInfo.MediaDescription    = "backup test";
            backupInfo.EncryptionAlgorithm = (int)BackupEncryptionAlgorithm.Aes128;
            backupInfo.EncryptorType       = (int)BackupEncryptorType.ServerCertificate;
            backupInfo.EncryptorName       = certificateName;

            BackupOperation backupOperation = CreateBackupOperation(service, liveConnection.ConnectionInfo.OwnerUri, backupInfo, helper.DataContainer, sqlConn);

            // Backup the database
            Console.WriteLine("Generate script for backup operation..");
            service.ScriptBackup(backupOperation);
            string script = backupOperation.ScriptContent;

            // Run the script
            Console.WriteLine("Execute the script..");
            testDb.RunQuery(script);

            // Remove the backup file
            Console.WriteLine("Verify the backup file exists and remove..");
            VerifyAndCleanBackup(backupPath);

            // Delete certificate and master key
            Console.WriteLine("Remove certificate and master key..");
            testDb.RunQuery(cleanupCertificateQuery);

            // Clean up the database
            Console.WriteLine("Clean up database..");
            testDb.Cleanup();
        }
Ejemplo n.º 3
0
        private string GenerateScriptForBackupType(BackupType backupType, string databaseName)
        {
            DisasterRecoveryService service = new DisasterRecoveryService();
            var liveConnection            = LiveConnectionHelper.InitLiveConnectionInfo(databaseName);
            DatabaseTaskHelper helper     = AdminService.CreateDatabaseTaskHelper(liveConnection.ConnectionInfo, databaseExists: true);
            SqlConnection      sqlConn    = ConnectionService.OpenSqlConnection(liveConnection.ConnectionInfo);
            string             backupPath = GetDefaultBackupFullPath(service, databaseName, helper.DataContainer, sqlConn);

            BackupInfo backupInfoLog = CreateDefaultBackupInfo(databaseName,
                                                               backupType,
                                                               new List <string>()
            {
                backupPath
            },
                                                               new Dictionary <string, int>()
            {
                { backupPath, (int)DeviceType.File }
            });

            backupInfoLog.FormatMedia      = true;
            backupInfoLog.SkipTapeHeader   = true;
            backupInfoLog.Initialize       = true;
            backupInfoLog.MediaName        = "backup test media";
            backupInfoLog.MediaDescription = "backup test";
            BackupOperation backupOperation = CreateBackupOperation(service, liveConnection.ConnectionInfo.OwnerUri, backupInfoLog, helper.DataContainer, sqlConn);

            // Generate Script
            Console.WriteLine("Generate script for backup operation..");
            service.ScriptBackup(backupOperation);
            string script = backupOperation.ScriptContent;

            // There shouldnt be any backup file created
            Assert.True(!File.Exists(backupPath), "Backup file is not expected to be created");

            sqlConn.Close();
            return(script);
        }