Scheduling a daily backup and maintenance for SQL Server Express
Scheduling a daily backup and maintenance for SQL Server Express
PDM Standard comprises two key components. The Vault Database, which contains the vault definition,
metadata, references and the history of all files, and the Archive, which contains all the files managed by
PDM (and their previous versions).
When considering a Disaster recovery plan you should be backing up the Database, the Archives and
Archive Server settings. Unless you have a specific SQL Agent for your Backup solution it is likely you will
need to manually create the SQL Backups locally then back up externally along with the Archive folders /
settings also being backed up at the same time.
PDM Standard uses SQL 2014 Express Edition, which does not support “Maintenance Plans” nor the SQL
Server Agent service that we would use to automate these within SQL Server Standard Edition or above.
What this means for you is that it is not possible to automatically schedule backups and maintenance tasks
directly within the Management Studio via said Maintenance Plan.
One possible solution to this is documented below. This is based on two parts that create a backup solution
for your PDM Standard Database but also allows for the creation of other tasks relating to the health of your
Database.
In this document we explain how to use these two parts to create your database backup and maintenance
solution.
PLEASE NOTE: The ‘expressmaint’ stored procedure was not written by, nor validated by, Solid
Solutions or SOLIDWORKS. Although all tests indicate that this is a valid, widely used and safe
toolset we can accept no responsibility for any data loss or corruption resulting from its
implementation. I would advise creating a dummy vault / vault database to test these tools in your
own environment before implementing into your live Vault Database.
Page 1 of 15
TECHNICAL DOCUMENT
NOTE: In order to allow scripts to be ran via a Scheduled Task the following changes need to be
made in the SQL Management Studio.
Right Click over the Server name and click Facets
Page 2 of 15
TECHNICAL DOCUMENT
Further changes may also be required based on the success / failure of the Scheduled Task to execute. This
step is discussed later as a troubleshooting exercise.
Page 3 of 15
TECHNICAL DOCUMENT
Verify that the Stored Procedure has been added to the Master Database by expanding
Master/Programmability/Stored Procedures
A complete set of parameters for each feature is included at the rear of this document.
With PDM Standard Databases set to ‘Simple’ Recovery mode by default, we are not concerned about the
Differential Database Backup or Log Backups so will focus on the three scenarios typical to maintaining the
health of a PDM Standard Database: Backup, Re-index and Re-organise
Page 4 of 15
TECHNICAL DOCUMENT
NOTE: You will need to know the name of your Vault Database prior to starting. If you do not know this then
open the PDM Administration tool. Right Click over your Vault Name and choose Properties (You may be
prompted to log in at this stage)
Page 5 of 15
TECHNICAL DOCUMENT
Step 1 Create the Script: Open a text editor and copy the following code into it. Change the Database name
and the paths for the Backup and Report to the folder you just created, then save it to (e.g.)
D:\Backups\EXPRESS scripts\Daily_Full_Backup.sql.
When executed, this will create a full database backup of a database called SWPDM_PDMSeminar to
D:\Backups\Backup, verify the backup and write a report to D:\Backups\Reports. Backups are retained in this
location for 4 days and reports for 1 week:
exec master.dbo.expressmaint
@database = 'SWPDM_PDMSeminar',
@optype = 'DB',
@backupfldr = 'D:\Backups\Backup',
@reportfldr = 'D:\Backups\Reports',
@verify = 1,
@dbretainunit = 'days',
@dbretainval = 4,
@rptretainunit = 'weeks',
@rptretainval = 1,
@report = 1
Step 2 On the Server, locate and open the Task Scheduler by clicking on Start menu and typing ‘Schedule
Tasks’
Step 3 Click Create Basic Task to start the Scheduled Task Wizard.
Page 6 of 15
TECHNICAL DOCUMENT
Page 7 of 15
TECHNICAL DOCUMENT
Step 6 Click Next, specify the information about the time to run the task. Set Start time to an appropriate
value when the load on the PDM database is low, set the Recur every option to 1 day, and then click Next.
Step 7 Choose Start a program from the task to perform list, and then click Next.
Page 8 of 15
TECHNICAL DOCUMENT
Step 8 Click Browse, browse to SQLCMD.exe (for SQL Express 2014 you can find it here - C:\Program
Files\Microsoft SQL Server\Client SDK\ODBC\110\Tools\Binn\SQLCMD.EXE)
Step 9 Type the following content to the Add arguments text box (changing script path to what you created
earlier and including your SQL Instance name (e.g PDMServer\PDMSTD), then click Next:
Step 10 Select the checkbox to Open the Advanced Properties for this task and then click Finish.
Page 9 of 15
TECHNICAL DOCUMENT
Step 11 Verify that he user account linked to the task is a Windows User account that is a SysAdmin for your
Database. This should be the User who installed PDM / SQL Express as they will have been added as a
SysAdmin automatically.
Step 12 Click OK. If prompted, supply the password for the account again.
Step 13 Test the created task by right-click on it and select Run. A command prompt windows named
SQLCMD has to be opened while the task is running. Check the created backup file and the log files.
Page 10 of 15
TECHNICAL DOCUMENT
If the task fails, try to run the task command manually in a command prompt window. If you see a message
like this
Msg 15281, Level 16, State 1, Server CERBERUS, Procedure sp_OACreate, Line 1
SQL Server blocked access to procedure 'sys.sp_OACreate' of component 'Ole Autom
ation Procedures' because this component is turned off as part of the security c
onfiguration for this server. A system administrator can enable the use of 'Ole
Automation Procedures' by using sp_configure. For more information about enablin
g 'Ole Automation Procedures', search for 'Ole Automation Procedures' in SQL Ser
ver Books Online.
Msg 50000, Level 16, State 1, Server CERBERUS, Procedure expressmaint, Line 113
Database SWPDM_PDMSeminar is invalid or database status is not ONLINE
Msg 15281, Level 16, State 1, Server CERBERUS, Procedure sp_OADestroy, Line 1
SQL Server blocked access to procedure 'sys.sp_OADestroy' of component 'Ole Auto
mation Procedures' because this component is turned off as part of the security
configuration for this server. A system administrator can enable the use of 'Ole
Automation Procedures' by using sp_configure. For more information about enabli
ng 'Ole Automation Procedures', search for 'Ole Automation Procedures' in SQL Se
rver Books Online.
Then you will need use the SQL Management Studio to enable ‘Ole Automation Procedures’ using the
following query
USE master
GO
sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'Ole Automation Procedures', 1;
GO
RECONFIGURE WITH OVERRIDE
GO
EXEC sp_configure 'xp_cmdshell', 1
GO
RECONFIGURE WITH OVERRIDE
GO
EXEC sp_configure 'show advanced options', 0
GO
RECONFIGURE;
GO
Page 11 of 15
TECHNICAL DOCUMENT
Many tables in the file SOLIDWORKS® PDM file vault database contain indexes – to ensure data is found
efficiently when lookups are done by various database operations. Microsoft® SQL server automatically
maintains these indexes whenever insert, update, or delete operations are made to the underlying data.
Over time as you perform a lot of file operations in the vault - for example after an upgrade where a lot of
records may have been modified, or performed a migration where many new records were added - the
various database table indexes becomes scattered. This is similar to how a hard drive can become
fragmented. Using variations of the Backup script we can schedule tasks to rebuild or reorganize the
indexes. If the indexes are very fragmented, this operation may improve general performance with
searching, browsing etc. in the file vault.
SOLIDWORKS recommends rebuilding the file vault database indexes at least once a month to improve
overall performance. In cases of larger databases and more activity - more frequent rebuilds such as once
per week may prove beneficial.
Rebuilding the indexes is a bit more database intensive (compared to reorganize) and could take longer to
complete so it should not run as frequently. It’s difficult to give an exact recommendation how often you
should reorganize or rebuild the indexes because it depends on the vault usage, if a lot of data is added
frequently, then you should reorganize more often. We will schedule the re-organise to be a weekly task with
the Re-indexing done monthly.
Note that the time it takes to complete the operations depend on the size of the database and how
fragmented the indexes currently are. It is possible to run it while the vault is in use, but the overall
performance might be affected while the maintenance plan is running. You may wish to schedule the
operation to run later in the day when less activity is done in the vault.
Page 12 of 15
TECHNICAL DOCUMENT
Step 1 Create the Script: Open a text editor and copy the following code into it then save it to e.g.
D:\Backups\EXPRESS scripts\REORGANISE_Indexes.sql and, as before, changing the Database name to
match your own.
exec master.dbo.expressmaint
@database = 'SWPDM_PDMSeminar',
@optype = 'REORG',
@reportfldr = 'D:\Backups\Reports',
@rptretainunit = 'weeks',
@rptretainval = 1,
@report = 1
This will run the reorg task and create a log file in the Reports directory which will be retained for one week.
Step 2 Create Scheduled Task as before, selecting this script instead and setting the schedule to be Weekly
at a day / time of your preference. (I have selected midnight on a Saturday)
Page 13 of 15
TECHNICAL DOCUMENT
Step 1 Create the Script: Open a text editor and copy the following code into it then save it to e.g.
D:\Backups\EXPRESS scripts\REINDEX_Indexes.sql and, as before, changing the Database name to match
your own.
exec master.dbo.expressmaint
@database = 'SWPDM_PDMSeminar',
@optype = 'REINDEX',
@reportfldr = 'D:\Backups\Reports',
@rptretainunit = 'weeks',
@rptretainval = 1,
@report = 1
This will run the reindex task and create a log file in the Reports directory which will be retained for one
week.
Step 2 Create Scheduled Task as before, selecting this script instead and setting the schedule to be Weekly
at a day / time of your preference. (I have selected every month on the last day of the month)
Page 14 of 15
TECHNICAL DOCUMENT
@database Yes None The target database for the maintenance operation. Valid values are a single
database name, ALL_USER which will process all user databases and
ALL_SYSTEM which will process all system databases
@optype Yes None The type of maintenance operation to be performed. Valid values are:
DB - Full Database Backup
DIFF - Differential Database Backup
LOG - Log Backup
CHECKDB - Database Integrity Check
REINDEX - Rebuild all indexes
REORG - Reorganize all indexes
@backupwith No Null Specify additional backup options as documented in BOL for the BACKUP WITH
command
@backupfldr No Null The base folder to write the backups to. Sub folders will be created for each
database
@verifywith No Null Specify additional verify options as documented in BOL for the VERIFY WITH
command
@dbretainunit No Null The unit of measure for the @dbretainval parameter. Valid values are minutes,
hours, days, weeks, months and copies. The combination of these two
parameters determines how long or how many copies of old backup files are
kept
@reportfldr No Null The folder where maintenance reports are written to if @report = 1
@rptretainunit No Null The unit of measure for the @rptretainval parameter. Valid values are minutes,
hours, days, weeks, months and copies. The combination of these two
parameters determines how long or how many copies of old reports are kept
@checkattrib No 0 Indicates whether to check the archive bit on a backup file before deleting it. This
is a safety check to prevent deletion of files that have not been backed up onto
tape.
Valid values are 1 and 0 with 1 = TRUE and 0 = FALSE
@delfirst No 0 Indicates whether to delete old backups prior to doing the current backup. This is
not advisable but can be useful if disk space is limited.
Valid values are 1 and 0 with 1 = TRUE and 0 = FALSE
Page 15 of 15