A PowerShell script for automated SQL Server database backups with compression, rotation, cloud synchronization, and email notifications.
Vibe coded with minor human intervention using Windsurf AI and Claude 3.7 Sonnet.
- Backs up all or specified SQL Server databases.
- Compresses backups using 7-Zip.
- Rotates backups based on configurable retention period.
- Synchronizes backups to Backblaze B2 cloud storage.
- Sends email notifications on success or failure.
- Fully configurable via JSON configuration file.
- Windows Server with PowerShell 5.1 or later.
- SQL Server instance.
- 7-Zip installed.
- Backblaze B2 CLI installed (if using B2 sync).
- SMTP server access (if using email notifications).
- Clone or download this repository to your SQL Server.
- Copy config-sample.json to
config.json
and edit the new file to match your environment. - Test the script by running it manually.
- Set up a scheduled task to run the script automatically.
The script is configured using the config.json
file. Here's an explanation of each section:
"SQLServer": {
"ServerInstance": "localhost",
"UseIntegratedSecurity": true,
"Username": "",
"Password": "",
"Databases": []
}
ServerInstance
: SQL Server instance nameUseIntegratedSecurity
: Set totrue
to use Windows authentication,false
to use SQL authenticationUsername
andPassword
: SQL Server credentials (only used ifUseIntegratedSecurity
isfalse
)Databases
: Array of database names to backup. Leave empty to backup all user databases.
"BackupPath": "C:\\SQLBackups",
"RetentionDays": 7,
BackupPath
: Local path where backups will be stored.RetentionDays
: Number of days to keep backups before deletion.
"Compression": {
"Path": "C:\\Program Files\\7-Zip\\7z.exe",
"Level": 5
}
Path
: Path to 7-Zip executable.Level
: Compression level (0-9, where 9 is maximum compression).
"BackblazeB2": {
"Enabled": true,
"KeyId": "",
"ApplicationKey": "",
"BucketName": "",
"FolderPath": "SQLBackups",
"CliPath": "C:\\Program Files\\Backblaze\\b2.exe"
}
Enabled
: Set totrue
to enable B2 sync,false
to disable.KeyId
: Your Backblaze B2 application key ID.ApplicationKey
: Your Backblaze B2 application key.BucketName
: Name of the B2 bucket to upload to.FolderPath
: Optional folder path within the bucket.CliPath
: Path to the B2 CLI executable.
"Email": {
"Enabled": true,
"SmtpServer": "",
"Port": 587,
"EnableSsl": true,
"Username": "",
"Password": "",
"From": "",
"To": ""
}
Enabled
: Set totrue
to enable email notifications,false
to disable.SmtpServer
: SMTP server address.Port
: SMTP server port.EnableSsl
: Set totrue
to use SSL/TLS,false
otherwise.Username
andPassword
: SMTP server credentials.From
: Sender email address.To
: Recipient email address(es), comma-separated.
.\Backup-SQLServer.ps1 -ConfigPath ".\config.json"
To set up a scheduled task to run the script daily:
- Open Task Scheduler.
- Create a new task.
- Set the trigger to run daily at your preferred time.
- Add a new action:
- Action: Start a program.
- Program/script:
powershell.exe
. - Arguments:
-ExecutionPolicy Bypass -File "C:\path\to\Backup-SQLServer.ps1" -ConfigPath "C:\path\to\config.json"
.
The script creates a log file in the logs
directory under the script directory, named Backup-SQLServer_YYYYMMDD_HHMMSS.log
. This log contains detailed information about the backup process.
This script is tested to work on the same machine where SQL Server is installed. If you are having connection issues, check if SQL Server Management Objects (SMO) is installed on the machine.
Also, please note that the SQLPS module is an older, deprecated SQL Server PowerShell module that is no longer maintained, while the SqlServer module is the current, actively maintained module for managing SQL Server instances via PowerShell. You may try installing the SqlServer PowerShell module if it's not already installed:
Install-Module -Name SqlServer -Force -SkipPublisherCheck
This project is licensed under the MIT License - see the LICENSE file for details.