A Windows Worker Service built with .NET 10 that automatically creates scheduled backups for SQL Server databases. The service runs continuously in the background, performs backups at configurable intervals, and logs all operations using both the built-in logging system and a custom file logger.
- Automatic SQL Server database backups
- Configurable backup interval
- Background execution using Worker Service
- Configuration with
appsettings.json - Dependency Injection (DI)
- Options Pattern (
IOptions<T>) - Built-in logging with
ILogger - Custom file logging
- Graceful shutdown using
CancellationToken - Windows Service support
- Fully asynchronous database operations (
async/await)
- C#
- .NET 10
- Worker Service
- SQL Server
- Microsoft.Data.SqlClient
- Dependency Injection
- Options Pattern
- Microsoft.Extensions.Logging
DatabaseBackupService
│
├── BackupSettings.cs
├── FileLogger.cs
├── Worker.cs
├── Program.cs
├── appsettings.json
├── InstallService.bat
├── UninstallService.bat
└── DatabaseBackupService.csproj
The application is configured using appsettings.json.
{
"BackupSettings": {
"ConnectionString": "Server=YOUR_SERVER;Database=YOUR_DATABASE;User Id=YOUR_USERNAME;Password=YOUR_PASSWORD;TrustServerCertificate=True",
"BackupFolder": "C:\\DatabaseBackups",
"LogFolder": "C:\\DatabaseBackups\\Logs",
"BackupIntervalMinutes": 60
}
}| Setting | Description |
|---|---|
| ConnectionString | SQL Server connection string |
| BackupFolder | Directory where backup files are stored |
| LogFolder | Directory where log files are stored |
| BackupIntervalMinutes | Time between backups (minutes) |
- The Worker Service starts.
- Configuration is loaded from
appsettings.json. - Backup and log directories are created automatically.
- The service executes a SQL Server
BACKUP DATABASEcommand. - Every operation is logged using both
ILoggerand a custom file logger. - The service waits for the configured interval.
- The process repeats until the service is stopped.
- Build the project in Release mode.
- Copy all generated files to a folder.
- Run
InstallService.batas Administrator.
To uninstall the service, run:
UninstallService.bat
The application records all important operations, including:
- Service startup
- Backup start
- Database name
- Backup file path
- Successful backups
- Errors and exceptions
- Service shutdown
Example:
2026-07-04 10:30:15 : Database Backup Service Started.
2026-07-04 10:30:15 : Backup Started.
2026-07-04 10:30:15 : Database: DVLD
2026-07-04 10:30:17 : Database backup completed successfully.
- SQL Server exceptions are caught and logged.
- Backup failures do not stop the service.
- Graceful shutdown is handled using
CancellationToken.
- Backup compression
- Backup retention policy
- Automatic cleanup of old backups
- Email notifications
- Restore functionality
- Support for multiple databases
Sherif Osama