A .NET Core console application that automates PostgreSQL database backups with configurable settings, logging, and retention policies. Perfect for scheduling regular backups of your PostgreSQL databases.
- 🗃️ Automated backup of multiple PostgreSQL databases
- 📅 Configurable backup retention period
- 📝 Detailed logging with retention management
- 🔄 Compatible with Windows Task Scheduler
- ☁️ Configurable backup location (supports cloud storage paths)
- 🔒 Secure password handling
- ⚡ Asynchronous operations
- ❌ Robust error handling
- PostreSQL 13
- PostreSQL 17
- .NET 8.0 or later
- PostgreSQL installed with pg_dump utility available in PATH
- Appropriate PostgreSQL user permissions for backup operations
- Clone the repository or download the latest release
- Configure the application settings in
appsettings.json
- Build the application:
dotnet build
Update the appsettings.json
file with your PostgreSQL settings:
json
{
"PostgresSettings": {
"Host": "localhost",
"Port": 5432,
"Username": "postgres",
"Password": "your_password",
"Database": "postgres",
"BackupType": "Full",
"BackupRetentionDays": 7,
"BackupPath": "C:\\Your\\Backup\\Path"
}
}
Parameter | Description | Default |
---|---|---|
Host | PostgreSQL server hostname | localhost |
Port | PostgreSQL server port | 5432 |
Username | PostgreSQL user with backup privileges | postgres |
Password | User password | - |
Database | Default database (not used for backup selection) | postgres |
BackupType | Type of backup to perform | Full |
BackupRetentionDays | Number of days to keep backups | 7 |
BackupPath | Directory where backups will be stored | - |
Run the application from the command line:
dotnet PostgresBackupConsole.dll
- Open Windows Task Scheduler
- Create a new Basic Task
- Set the trigger (schedule) as needed
- Action: Start a program
- Program/script: Path to
PostgresBackupConsole.exe
- Start in: Application directory path
- Logs are stored in the
Logs
directory - Log files are named
backup_log_YYYYMMDD_HHMMSS.txt
- Logs older than 30 days are automatically cleaned up
- Each backup operation is logged with timestamp and status
- Backups are stored in the configured backup path
- File naming format:
DatabaseName_YYYYMMDD_HHMMSS.backup
- Files use PostgreSQL's custom format (.backup)
- Old backups are automatically removed based on retention days
To restore a backup, use the pg_restore utility: bash pg_restore -h localhost -p 5432 -U postgres -d database_name backup_file.backup
The application includes comprehensive error handling for:
- Database connection issues
- Backup process failures
- File system operations
- Configuration problems
All errors are logged to both console and log file.
PostgresBackupConsole/
├── Models/
│ └── PostgresSettings.cs
├── Services/
│ ├── IPostgresService.cs
│ └── PostgresService.cs
├── Program.cs
├── appsettings.json
├── README.md
└-- MIT License
dotnet restore
dotnet build
dotnet publish -c Release
- Fork the repository
- Create a feature branch
- Commit your changes
- Push to the branch
- Create a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Built with .NET 8.0
- Uses Npgsql for PostgreSQL connectivity
- Inspired by the need for simple, reliable database backups for my Davinci Resolve Project Server
For issues, questions, or contributions, please create an issue in the GitHub repository.