Skip to content

Dapplesoft-AD/DB-Backup-Restore

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🧠 PostgreSQL Auto Backup & Cleanup Service

A cross-platform .NET Worker Service that automatically backs up all PostgreSQL databases at regular intervals, uploads them to IDrive e2 (S3-compatible) cloud storage, and cleans up old backups (both locally and remotely).


πŸš€ Features

βœ… Automatic PostgreSQL Backup

  • Detects all databases in your PostgreSQL instance (except system ones like template0, template1).
  • Uses pg_dump to back up each database individually.
  • Compresses all backups into a single .zip file.
  • Stores it in a local Backups directory.

βœ… Cloud Storage (IDrive e2)

  • Uploads each zipped backup to your IDrive bucket.
  • Uses AWS S3–compatible SDK with secure credentials.

βœ… Automatic Cleanup

  • Deletes backups older than 7 days from both:
    • Local storage.
    • IDrive cloud bucket.

βœ… Interval Scheduling

  • Takes backups every 30 minutes (configurable).
  • Cleanup runs once per day automatically.
  • Fully asynchronous and non-blocking.

βœ… Cross-Platform

  • Works on Windows, Linux, or macOS.
  • Can run as a Windows Service or Linux systemd service.

🧩 Project Structure

Backup_Service/
β”‚
β”œβ”€β”€ Program.cs                       # Application entry point
β”œβ”€β”€ BackupService.cs                  # Periodic backup and upload logic
β”œβ”€β”€ CleanupService.cs                 # Daily cleanup of old backups
β”œβ”€β”€ IDriveService.cs                  # Handles all IDrive S3 operations
β”‚
β”œβ”€β”€ Models/
β”‚   β”œβ”€β”€ BackupSettings.cs             # Backup configuration
β”‚   └── CleanupSettings.cs            # Cleanup configuration (optional)
β”‚
β”œβ”€β”€ appsettings.json                  # Main configuration
└── README.md

βš™οΈ Configuration (appsettings.json)

{
  "PostgresSettings": {
    "Host": "localhost",
    "Port": "5432",
    "Username": "postgres",
    "Password": "yourpassword"
  },
  "IDrive": {
    "AccessKey": "YOUR_ACCESS_KEY",
    "SecretKey": "YOUR_SECRET_KEY",
    "BucketName": "backupautomation",
    "Region": "ap-southeast-1",
    "ServiceURL": "https://s3.ap-southeast-1.idrivee2.com"
  },
  "BackupSettings": {
    "LocalBackupFolder": "Backups",
    "IntervalInMinutes": 30,
    "StartTime": "00:00:00",
    "PgDumpPath": "C:\\Program Files\\PostgreSQL\\17\\bin\\pg_dump.exe"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information"
    }
  }
}

πŸ“ Note:

Ensure your PgDumpPath points to the correct version of PostgreSQL installed.

Region and ServiceURL must match your IDrive e2 region (Singapore example shown above).

πŸ› οΈ How It Works

When the service starts, it immediately performs a backup of all databases.

Each backup is saved to a .dump file and zipped.

The zip file is uploaded to the configured IDrive bucket.

Every 30 minutes, this process repeats.

Once per day, the cleanup service deletes files older than 7 days.

πŸ§ͺ Run Locally

1.Open the project in Visual Studio or VS Code.

2.Configure your connection in appsettings.json.

3.Build and run:

dotnet run

4.Watch the logs in the console for:

Backup successful
Cleanup complete

🧰 Running as a Windows Service 1.Publish the project:

dotnet publish -c Release -r win-x64 --self-contained true

2.Install as a Windows Service (PowerShell):

sc create PostgresBackupService binPath= "C:\path\to\published\Backup_Service.exe"
sc start PostgresBackupService

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages