Automated SQL Server backup (multiple servers, instances and databases)

on 13th December
  • windows
  • sql server
  • instance
  • backup
  • automated
  • database

SQL Server is one of the most stable and widely used database engines around in corporate environments. Given that SQL Server 2008 R2 Express+ is available free of charge, is rock solid and can support databases up to 10GB in size, it's a logical choice for many web and desktop applications hosted in-house in corporate environments. 

What can often be overlooked until it's too late (i.e. malware has corrupted the database, ransomware has encrypted it or the hard disk has given up the ghost) is a robust backup strategy. Commercial SQL Server backup tools can cost thousands but our method of choice is a robust Windows Powershell script. This script:


  • Works its way across the network
  • Pings instances to make sure they're online
  • Connects to those instances (it uses Windows Authentication so make sure it's run under an account with suitable privileges - or, the script could easily be modified to use a SQL Server login)
  • Loops through the databases in that instance
  • Runs a DBCC CHECKDB command to ensure the database is OK
  • Backs each database up in turn to a destination specified by a configuration file
  • Verifies that the database can be restored (i.e. that the backup is valid)
  • Generates a 7-zip backup for each machine it backed up data from
  • Verifies that the compressed archive is valid
  • and finally, it emails the log file to the system administrator at the end of the process

The Powershell code looks as follows:


You'll notice that a few configuration files adjust the way the script functions. Specifically, sql_server_backup_path.txt will simply contain the path (local to the SQL Server instance) to backup the databases to - in our example, we've set it to c:\PSBackup. sql_server_backup_servers.txt contains the list of machines that we should connect to, in this example, I'm connecting to 2 local machines and one remote machine:

REKII
REKIII
REKRMVI

Finally, sql_server_backup_exclude.txt contains a list of machines, instances or databases that should be excluded from the backup plan, i.e. in our example, we don't want to backup any databases from the IMF instance on REKII and we don't want to backup the country_data database in the BIS instance on REKIII:

REKII\IMF
REKIII\BIS\country_data

There are a couple of dependencies for all aspects of this script to work, i.e. you need the Powershell SQL snap-ins, the 7-zip command line utility and you should enter in your SMTP server and account credentials so you get the summary log email. I hope it offers at the very least a solid foundation for implementing a commercial SQL Server backup strategy.

As with all articles on Celtic Productions, this article is protected by international copyright laws. It may be linked to (we are of course most grateful of links to our articles), however, it may never be reproduced without the prior express permission of its owners, Celtic Productions. All code is provided as-is, without warranty of any kind, either express or implied.