SQL Databases

PowerShell script to create backups of all SQL databases on a device.

Overview

Dev Insight: This script was written to provide SQL backups to clients using SQL Express with a backup solution that doesn't natively support SQL. It is designed to be used on a schedule in tandem with a file/image-based backup solution.

This script uses the SqlServer module to automate the creation of SQL database backups.

Prerequisites:

For this script to run successfully, the user running the script will need to have permission to perform SQL database backups on all databases, including system databases. It is recommended to run the script as SYSTEM and set permissions on each database using the process below.

Granting backup database permissions to SYSTEM:

  1. Install & login to Microsoft SQL Server Management Studio (SSMS) on the SQL Server

  2. Navigate to Server\Instance > Security > Logins

  3. Right-click NT AUTHORITY\SYSTEM and choose Properties

  4. Under Server Roles, make sure public is checked

  5. Under User Mapping, check each database and select the following database role memberships for each:

    1. public

    2. db_backupoperator

After database permissions are set, you can deploy the script via your RMM.

Notes:

  • Unless changed manually, the default SQL backup location is C:\Program Files\Microsoft SQL Server\MSSQL.[INSTANCENAME]\MSSQL\Backup


Script

https://github.com/wise-io/scripts/blob/main/scripts/BackupSQL.ps1

Examples

Example 1

This example creates a backup of all SQL databases on every detected instance, excluding temp databases.

Example 2

This example displays database backups performed in the last month. It does not perform new backups.


Parameters

-AuditOnly

Optional switch parameter that outputs a history of all database backups for the last month. No additional backups are performed when using this parameter.

Last updated

Was this helpful?