r/PowerShell • u/Due-Fishing-6119 • Jun 24 '26
Script Sharing Built a script to automate SQL Server backups
I’ve been working on a PowerShell tool to automate SQL Server backup workflows.
It supports interactive selection of one or multiple servers and databases, runs backups asynchronously, and handles the SqlServer module automatically if it’s missing. The main goal was to reduce manual steps, improve reliability, and avoid common mistakes in backup operations.
I’d be curious to hear how others here approach backup automation in PowerShell.
15
Upvotes
29
u/VladDBA Jun 24 '26
Besides dbatools, why not just use SQL Server Agent jobs and Ola Hallengren's maintenance solution?
With a bit of minimal configuration you can manage all the jobs from one central instance using CMS and then collect all the execution log data in a single table for any potential audits.
16
u/mickeykarimzadeh Jun 24 '26
Why not dbatools?
3
u/ashimbo Jun 24 '26
There are plenty of built-in and free methods for backing up SQL, plus most business-focused backup solutions will handle SQL server.
I think most businesses are probably using Veeam or Rubrik, both of which handle SQL w/o issue.
What problem are you actually trying to solve?
2
u/BlackV Jun 24 '26
Did you have a link to your code/tool?
2
u/jcpham Jun 24 '26 edited Jun 24 '26
I'm going to drop mine and you're going to laugh but I've been dumping MS SQL databases for decades this way. There is no powershell here just old school dumb shit.
db_dump.cmd
db_backup.sql
Then there's usually a delodfiles.vbs Sometimes a blat command to email the log file.
1
u/purplemonkeymad Jun 24 '26
Not needing to do it for a farm of databases. I would typically just use the [System.Data.SqlClient.*] namespace to start a backup that dumps to an existing file backup location, and manually input the database info. Worked without any additional dependencies, can be run as a service account/gmsa.
1
u/Vern_Anderson Jun 24 '26 edited Jun 24 '26
If this is your only means of backing up the database it's a great script. However, if you already have a backup solution that backs up the server and or the databases on the instance, then running this will change the order in which you need to restore the backups. So, you could actually be complicating things and breaking the backup chain. Are your databases in simple mode or full recovery model?
1
u/Fallingdamage Jun 24 '26
I backup transaction logs and do full daily backups (since our DB's are small and easy to manage.)
Just a PS1 glued to a scheduled task. You can use other SQL tools and built in tooling to do backups, but MS decided that backing up to network locations was too much effort to add, so instead we do it with powershell to avoid paying a mortage on a third part SQL backup solution that basically would do the same thing.
Works great and is light weight!
Thought about using WPF to build a nice GUI for the backups being created to make restore points easier to select. Between Xlogs, Differentials and Full backups, it wouldnt be hard to build something that can restore/rebuild a database to a specific point that looks clean and just executes some 1-liners in the background for me.
But yeah. Been using powershell to backup and mange SQL for like 5 years now. Never looked back.
1
u/NoBee8106 Jun 26 '26
Why not use the built in tool to do this via automation?
-1
u/hihcadore Jun 24 '26
Using PowerShell to automate backup jobs seems to me to be a hacky way to do it. There’s always going to be a better solution, and usually it’s pretty cheap compared to dealing with data loss.
That being said I’ve def mirrored folders or drives with robocopy using a script triggered by task scheduler before. It’s a down a dirty way to have a copy of whatever you need. Again, now I just use OneDrive so…