コード例 #1
0
        public static void BackupDB(string backupFilePath, IConfiguration configuration, Guid requestId, ILogger<DatabaseUpgrater> logger)
        {
            logger.LogInformation("Backup database operation started...");
            ProgressTracker.add(requestId, "Backup database operation started...");

            ServerConnection connection = null;

            try
            {
                var connectionString = configuration["MasterConnection"];

                // Define a Backup object variable
                Backup backup = new Backup();

                // Set type of backup to be performed to database
                backup.Action = BackupActionType.Database;
                backup.BackupSetDescription = "Full backup of Northwind";
                // Set the name used to identify a particular backup set
                backup.BackupSetName = "Northwind Backup";
                // Specify the name of the database to back up
                backup.Database = "Northwind";

                // Set up the backup device to use filesystem
                BackupDeviceItem deviceItem = new BackupDeviceItem(backupFilePath, DeviceType.File);
                // Add the device to the Backup object
                backup.Devices.Add(deviceItem);

                // Setup a new connection to the data server
                connection = new ServerConnection(new SqlConnection(connectionString));
                Server sqlServer = new Server(connection);

                // Initialize devices associated with a backup operation
                backup.Initialize = true;
                backup.Checksum = true;
                // Set it to true to have the process continue even after checksum error
                backup.ContinueAfterError = true;
                // Set the Incremental property to False to specify that this is a full database backup  
                backup.Incremental = false;
                // Set the backup expiration date
                backup.ExpirationDate = DateTime.Now.AddYears(1);
                // Specify that the log must be truncated after the backup is complete
                backup.LogTruncation = BackupTruncateLogType.Truncate;

                backup.PercentCompleteNotification = 10;

                backup.PercentComplete += (s, e) =>
                {
                    // Inform the user percent complete
                    logger.LogInformation($"Percent Complete: {e.Percent}");
                    ProgressTracker.add(requestId, $"Percent Complete: {e.Percent}");
                };

                // Run SqlBackup to perform the full database backup on the instance of SQL Server
                backup.SqlBackup(sqlServer);

                // Inform the user that the backup has been completed 
                logger.LogInformation("Backup database has been completed");
                ProgressTracker.add(requestId, "Backup database has been completed");
            }
            finally
            {
                if (connection != null)
                    connection.Disconnect();
            }
        }