Automatic daily backup solution for Microsoft SQL Server and SQL Server Express.
A lightweight, Windows-friendly backup toolkit for Microsoft SQL Server and SQL Server Express.
It uses:
sqlcmd- T-SQL
- PowerShell
- Windows Task Scheduler
No SQL Server Agent and no xp_cmdshell are required.
- Full backup of all online user databases
- Works with SQL Server Express
- One backup folder per day
CHECKSUMduring backupRESTORE VERIFYONLYafter each backup- Daily log files
- Configurable retention
- Optional include/exclude filters
- Windows or SQL authentication
- Optional console colors
- Ready for Windows Task Scheduler
- GitHub Actions validation
- No hard-coded company, server, or customer data
sql-server-daily-backup/
├── .github/
│ └── workflows/
│ └── validate.yml
├── docs/
│ ├── TASK_SCHEDULER.md
│ └── TROUBLESHOOTING.md
├── scripts/
│ ├── backup_all.bat
│ ├── backup_all.sql
│ ├── cleanup_sql_backups.ps1
│ ├── config.example.bat
│ └── test_connection.bat
├── .gitignore
├── CHANGELOG.md
├── LICENSE
└── README.md
Example:
C:\SQLBackups
Copy the scripts folder into:
C:\SQLBackups\scripts
Copy:
config.example.bat
to:
config.bat
Then edit config.bat.
Example:
set "SQLSERVER=localhost\SQLEXPRESS"
set "BACKUPROOT=C:\SQLBackups"
set "AUTH_MODE=WINDOWS"config.bat is ignored by Git, so local credentials and server details are not committed.
Run:
test_connection.bat
Run:
backup_all.bat
After a successful backup you should see:
C:\SQLBackups\YYYY-MM-DD\
with .bak files inside.
All configuration is stored in:
scripts\config.bat
Main options:
set "SQLSERVER=localhost\SQLEXPRESS"
set "BACKUPROOT=C:\SQLBackups"
set "AUTH_MODE=WINDOWS"
set "SQLUSER="
set "SQLPASSWORD="
set "DAYS_TO_KEEP_BACKUPS=30"
set "DAYS_TO_KEEP_LOGS=90"
set "INCLUDE_DATABASES="
set "EXCLUDE_DATABASES=ReportServer,ReportServerTempDB"
set "EXCLUDE_PREFIXES=XLOG"
set "EXCLUDE_SUFFIXES=_TEST,_CORRUPTED"
set "ENABLE_CONSOLE_COLORS=1"set "INCLUDE_DATABASES=Database1,Database2,Database3"Leave it empty to include all eligible user databases.
set "EXCLUDE_DATABASES=ReportServer,ReportServerTempDB"set "EXCLUDE_PREFIXES=XLOG,TEMP"
set "EXCLUDE_SUFFIXES=_TEST,_CORRUPTED"set "AUTH_MODE=WINDOWS"The Windows account running the BAT file must have permission to connect to SQL Server and perform backups.
set "AUTH_MODE=SQL"
set "SQLUSER=backup_user"
set "SQLPASSWORD=change_me"Avoid committing config.bat.
The SQL Server service account must have write permission on the backup folder.
Find the service account:
SELECT servicename, service_account
FROM sys.dm_server_services;Then grant Modify permission to that account.
Example:
icacls "C:\SQLBackups" /grant "NT SERVICE\MSSQL$SQLEXPRESS:(OI)(CI)M" /TRun the command as Administrator.
SQL Server Express does not support native backup compression in some versions/editions.
This project does not use COMPRESSION by default.
See:
docs\TASK_SCHEDULER.md
See:
docs\TROUBLESHOOTING.md
A backup on the same physical disk as the database is not enough.
Use the 3-2-1 rule:
- 3 copies of your data
- 2 different storage media
- 1 copy off-site
For example:
- local SQL backup
- NAS copy
- cloud or off-site copy
RESTORE VERIFYONLY is useful, but it is not a substitute for a real restore test.
At least once per month:
- Restore one backup to a test database.
- Run:
DBCC CHECKDB('TEST_DATABASE');- Confirm that the application can read the restored data.
MIT
