Skip to content

Repository files navigation

Database Backup Service

A lightweight, robust background service built in C# (.NET Framework 4.8) designed to automate Microsoft SQL Server database backups. This service reads its configuration dynamically from an external configuration file, making it flexible and easy to manage without altering the source code.

🚀 Features

  • Configurable Database Name: Easily target any database by changing a single key in the configuration file.
  • Windows Authentication: Secure connection string supporting integrated security without exposing passwords or credentials.
  • Automated & Scheduled: Built to work seamlessly with Windows Task Scheduler for precise execution timing (e.g., daily or hourly backups).
  • Robust Logging: Automatically logs all successful backups, default fallbacks, and error traces to a dedicated text file with precise timestamps.
  • Console Mode Support: Includes a built-in console runner method (StartInConsole) for easy local testing and debugging.

⚙️ Configuration (App.config)

Make sure your App.config is configured properly before running the service:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    
    <!-- Target Database Name -->
    <add key="DatabaseName" value="Sales"/>

    <!-- Database Connection String (Windows Authentication) -->
    <add key="ConnectionString" value="Server=.;Integrated Security=True;"/>
   
    <!-- Backup Destination Folder -->
    <add key="BackupFolder" value="C:\DatabaseBackupService\Backups" />
   
    <!-- Log Files Folder -->
    <add key="LogFolder" value="C:\DatabaseBackupService\Logs"/>
  
  </appSettings>
  
  <startup> 
      <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
  </startup>
</configuration>

🛠️ How It Works

  1. Initialization: On startup, the service reads the database name, connection string, and directory paths from App.config. If any path is missing, it falls back to safe default directories and automatically creates them using Directory.CreateDirectory.

  2. Backup Execution: It connects to the SQL Server instance and executes the standard backup command:

BACKUP DATABASE [DatabaseName] TO DISK = 'Path\backup_timestamp.bak' WITH INIT
  1. Task Scheduler Integration: Instead of running an internal infinite timer, this service is optimized to be triggered on-demand via Windows Task Scheduler, ensuring zero resource wastage when idle.

📂 Logging

All operations are logged to C:\DatabaseBackupService\Logs\ServiceLog.txt in the following format:

[2026-08-11 20:30:00] Service Started
[2026-08-11 20:30:01] Database backup successful for [Sales]: C:\DatabaseBackupService\Backups\Sales_backup_20260811_203000.bak
[2026-08-11 20:30:01] Service Stopped.

💻 Requirements

Framework: .NET Framework 4.8

Database: Microsoft SQL Server (Local or Remote)

OS: Windows (Windows Server or Windows 10/11 for development)

About

No description or website provided.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages