SQL Databases
PowerShell script to create backups of all SQL databases on a device.
Last updated
PowerShell script to create backups of all SQL databases on a device.
Last updated
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
:
Install & login to Microsoft SQL Server Management Studio (SSMS) on the SQL Server
Navigate to Server\Instance > Security > Logins
Right-click NT AUTHORITY\SYSTEM
and choose Properties
Under Server Roles, make sure public
is checked
Under User Mapping, check each database and select the following database role memberships for each:
public
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
Backups are not performed on databases named tempdb.
This example creates a backup of all SQL databases on every detected instance, excluding temp databases.
This example displays database backups performed in the last month. It does not perform new backups.
-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.