Backing Up WinCC Projects: Solving SQL Database Lock Failures
This field reference documents the procedure for backing up Siemens WinCC v7 SCADA projects on a single-user or distributed multi-client system when the Microsoft SQL Server instance MSSQL$WINCC holds file handles open against the runtime database files. Root causes, recovery tools, an automated batch script, verification steps, and a troubleshooting matrix are included.
1. Problem Overview
When archiving a WinCC runtime project from disk to a backup location (external drive, network share, or local archive folder), Windows file-copy operations fail with access-violation errors on the database files. The error occurs even when the project has been closed through WinCC Explorer. The MSSQL$WINCC service continues to hold locks on the .MDF and .LDF files of the archive database, blocking the copy.
Typical symptoms:
- Copying a WinCC project folder with Windows Explorer returns The process cannot access the file because it is being used by another process.
- Robocopy reports
ERROR 5 (0x00000005) Access is deniedand retries the operation. - Third-party archivers (WinZip, 7-Zip, NTBackup) return sharing-violation errors on the
.MDFdatabase files. - Even after selecting Close project and Exit Explorer in WinCC Explorer, the database files remain locked.
2. WinCC Project Directory Layout
Understanding the layout is required to choose the right copy strategy. The default install creates a project root with the structure below.
| Path (relative to project root) | Content | Locked at runtime |
|---|---|---|
\<ProjectName>\<ComputerName>\ |
Project configuration: *.pck, *.lck, *.MDF (CC database), *.LDF
|
Yes, .MDF/.LDF
|
\CommonArchiving\ |
Tag logging segments (*.dtb) and alarm logging segments |
Yes, active segment |
\PAS\ |
Process Historian archive (only on Historian installations) | Yes |
\ScriptLib\ |
C / VBS project scripts, *.pas, *.vb
|
No |
\Textbibliothek\ |
Text Library exports | No |
\GraCS\ |
Graphics: *.pdl, *.emf
|
No (but may be held briefly by WinCC Explorer) |
The files that are guaranteed to require a clean WinCC shutdown (or a SQL service cycle) are the CC_<ProjectName>_<ComputerName>.MDF configuration database and the ARCHIVE.MDF runtime archive. Configuration files (graphics, scripts, text library) can be copied at any time, although the running runtime may overwrite an inconsistent copy if it is not stopped.
3. Root Cause: Database Detachment Behaviour
WinCC v7 stores runtime data in a Microsoft SQL Server database instance named MSSQL$WINCC. The archive database file ARCHIVE.MDF and the configuration-related CC_<ProjectName>_<ComputerName>.MDF files are attached to this instance when the project is opened. Under a normal shutdown, WinCC issues sp_detach_db to the SQL instance so that all handles are released and the files can be operated on externally.
In rare cases this automatic detachment does not occur on project close. The known causes are documented below.
| Cause | Trigger | Effect |
|---|---|---|
| Path with spaces in installation directory | WinCC installed under C:\Program Files\Siemens\WinCC on English Windows 2000/XP |
Detach commands silently fail; handles remain |
| Stale WinCC Explorer process | Project closed via Exit Explorer instead of Close project and Exit Explorer | SQL session kept open by residual process |
| Client connection still active | A WinCC client on a multi-client system remains connected | Server-side SQL handles not released |
| Project name collision | WinCC project folder named WinCC (case insensitive) |
Documented internal conflict; detach fails |
| Anti-virus on-access scan | Real-time AV opens brief handles during scan | File copy fails intermittently |
| Service account mismatch | Logged-on user does not match the service account that started MSSQL$WINCC
|
Detach authenticates against wrong SID and is rejected |
Once the handles remain, the only guaranteed way to release them is to either restart the MSSQL$WINCC service or to use the recovery tool Reset_WinCC.vbs shipped with the WinCC installation.
4. Prerequisites
-
Local administrator privileges on the WinCC server, or membership in
SQLServerMSSQLUser$<COMPUTERNAME>$MSSQLSERVERwith explicit permission to stop/start the SQL service. - Backup destination with sufficient free space. Allocate 2× the on-disk project size to accommodate deflated copies, transaction logs, and growth between runs.
-
Project directory location. Default is
C:\UFO_WINCC_PROJ\<ProjectName>\for the project root. -
Service name. The default instance name is
MSSQL$WINCC(the literalWINCCsuffix, not the project name). Confirm withsc queryex type= service state= all | findstr /I "MSSQL"in an elevated command prompt. -
Credentials. The Windows user running the backup must be the same user that started the SQL service, or hold the SQL
sysadminserver role. - SQL Server Management Studio (SSMS) for the optional native-database backup path described in section 7.
5. Proper WinCC Project Shutdown Procedure
To minimise the chance of stale database locks, follow this ordered shutdown sequence.
- On every WinCC client connected to the server, close all faceplates and log off the runtime. Do not simply click the Windows close button on the client window; the connection persists.
- On the WinCC server, open WinCC Explorer.
- From the menu bar select File → Close Project. A dialog appears with two options:
- Exit Explorer — leaves the SQL instance attached.
- Close project and Exit Explorer — issues the proper detach sequence.
- Select Close project and Exit Explorer. Wait for the WinCC Explorer window to close. Do not force-close via Task Manager.
- Open Computer Management → Services and Applications → Services and verify that the following services are stopped:
-
SIMATIC WinCC Explorer(CCExplorer.exe) CCAgent-
SQL Server (WINCC)(display name), service nameMSSQL$WINCC -
SIMATIC WinCC CCArchiveServer(only present on multi-client systems)
-
If, after step 4, the database files are still locked, proceed to the recovery procedure in the next section.
6. Recovery Tool: Reset_WinCC.vbs
Siemens ships a VBScript recovery tool that forces WinCC components and attached SQL databases into a known clean state. The tool is intended for exceptional cases only and should not be used for routine shutdown.
| WinCC version | Default path |
|---|---|
| v7.0 / v7.2 (x86) | C:\Program Files\Siemens\WinCC\bin\Reset_WinCC.vbs |
| v7.3 / v7.4 / v7.5 (x86) | C:\Program Files (x86)\Siemens\Automation\WinCC\bin\Reset_WinCC.vbs |
| v7.4 / v7.5 (x64) | C:\Program Files\Siemens\Automation\WinCC\bin\Reset_WinCC.vbs |
To execute:
- Open an elevated command prompt: right-click Command Prompt and choose Run as administrator.
- Navigate to the bin folder and run:
cscript //NoLogo "C:\Program Files\Siemens\Automation\WinCC\bin\Reset_WinCC.vbs" - The script terminates all WinCC processes, stops the CCAgent, and detaches all configured project databases from
MSSQL$WINCC. - After the script returns (typically under 30 seconds), verify the project files are unlocked. Open the project directory in Windows Explorer and try to delete the
<ProjectName>.MDFfile. If deletion succeeds, the detach completed.
C:\Program Files\Siemens\WinCC on an English Windows 2000/XP system), the Reset_WinCC.vbs tool cannot detach the databases and the script will report success while the files remain locked. On any modern system, prefer installing WinCC to a path without spaces, for example C:\Siemens\WinCC.
Reset_WinCC.vbs is intended for emergency cleanup only. Routine use can mask underlying configuration problems and may corrupt an open archive segment if called while a tag logging segment is being written.
7. Automated Backup Script
When the shutdown procedure above still leaves handles open — a common situation on multi-client systems where one client fails to release its connection cleanly — the most reliable approach is to cycle the SQL service during the backup window. The batch script below implements that approach and is hardened against repeated runs and partial copies.
@echo off
REM =============================================================
REM WinCC v7 project backup with SQL service cycle
REM Run this script from an elevated command prompt on the
REM WinCC server. It refuses to run from any other host so that
REM the user has the right to stop the SQL service and to read
REM the security descriptors on the files.
REM =============================================================
setlocal EnableExtensions EnableDelayedExpansion
REM --- Safety: host check -------------------------------------
if /i "%COMPUTERNAME%" neq "SRVWINCC" (
echo -------------------------------------------------
echo Execute this from SRVWINCC to ensure all
echo permissions and SQL handles are scoped to the
echo correct server. Aborting.
echo -------------------------------------------------
exit /b 1
)
REM --- Configuration ------------------------------------------
set "SRC=C:\UFO_WINCC_PROJ\UFO_WINCC_RUN"
set "DST=D:\UFO_WINCC_PROJ\UFO_WINCC_RUN"
set "SVC=MSSQL$WINCC"
set "LOG=%~dp0backup_%DATE:~10,4%-%DATE:~4,2%-%DATE:~7,2%.log"
echo [%DATE% %TIME%] Backup start src=%SRC% dst=%DST% > "%LOG%"
REM --- Optional: force a clean WinCC close --------------------
REM cscript //NoLogo "C:\Program Files\Siemens\Automation\WinCC\bin\Reset_WinCC.vbs" >> "%LOG%" 2>&1
REM Remove the REM above if you want to force a detach before
REM stopping the SQL service. Required only when Reset_WinCC
REM is needed (see notes above).
REM --- Stop SQL ------------------------------------------------
echo [%DATE% %TIME%] Stopping %SVC%... >> "%LOG%"
net stop "%SVC%" >> "%LOG%" 2>&1
if errorlevel 1 (
echo [%DATE% %TIME%] ERROR: could not stop %SVC%. Aborting. >> "%LOG%"
type "%LOG%"
exit /b 2
)
REM --- Robocopy mirror with security descriptors --------------
REM /E = subdirectories, including empty ones
REM /COPYALL = D A T S O U (data, attributes, timestamps,
REM security, owner, audit info)
REM /R:1 /W:1 = one retry with a one-second wait on transient
REM errors
REM /NP = no progress, easier log parsing
REM /LOG+ = append to log file
robocopy "%SRC%" "%DST%" /E /COPYALL /R:1 /W:1 /NP /LOG+:"%LOG%"
set "RC=%ERRORLEVEL%"
REM --- Restart SQL --------------------------------------------
echo [%DATE% %TIME%] Starting %SVC%... >> "%LOG%"
net start "%SVC%" >> "%LOG%" 2>&1
REM --- Report --------------------------------------------------
if %RC% GEQ 8 (
echo [%DATE% %TIME%] ERROR: robocopy returned %RC% - some files failed. >> "%LOG%"
type "%LOG%"
exit /b 3
) else (
echo [%DATE% %TIME%] Backup complete, robocopy RC=%RC%. >> "%LOG%"
type "%LOG%"
exit /b 0
)
7.1 Script behaviour notes
- The host check at the top prevents the script from being run on the wrong machine, which would silently produce a half-empty backup because the local SQL service would be unaware of the remote project.
- Stopping the service takes 5–20 seconds depending on the size of the open archive segment. Plan a backup window of at least two minutes.
- Robocopy return codes 0–7 indicate success with various levels of file copy. Codes 8 and above mean errors. The script treats ≥8 as failure.
- The
/COPYALLswitch copies NTFS ACLs, audit settings, owner, and timestamps. This is critical for WinCC projects because the runtime reads security descriptors on the project directory at start-up. A copy without ACLs may fail to start the project on the new server. - Re-running the script is incremental: files that exist at the destination with matching content and ACLs are skipped on the second run.
7.2 Robocopy switch quick reference
| Switch | Purpose | Why it matters for WinCC |
|---|---|---|
/E |
Copy subdirectories including empty ones | Captures empty tag logging directories used by the next segment |
/COPYALL (= /COPY:DATSOU) |
Copy data, attributes, timestamps, security, owner, audit info | Required to preserve NTFS ACLs that the runtime checks on startup |
/R:1 /W:1 |
One retry, one-second wait | Fast-fail on persistent SQL handle conflicts rather than hanging |
/NP |
No per-file progress | Makes the log file easier to diff between runs |
/LOG+:file |
Append to log file | Builds a history of backup runs for audit |
/MIR |
Mirror (deletes files in destination not in source) | Do not use on a destination that is reused for non-WinCC files |
Reference: Microsoft Learn: robocopy.
8. Alternative: Native SQL Database Backup
For a database-consistent backup of the WinCC archive data, use the SQL Server native backup path in addition to the file copy. This produces a transactionally consistent backup independent of the file copy.
- Open SQL Server Management Studio (SSMS) and connect to
SRVWINCC\WINCCwith Windows authentication. - Right-click the database
CC_<ProjectName>_<ComputerName>and select Tasks → Back Up. - Set backup type to Full, destination to a
.bakfile, and click OK. - Repeat for the
ARCHIVEdatabase if archiving is enabled.
The native SQL backup is safe to perform while WinCC is running, in contrast to the file copy, which is the operation that originally fails due to file locks.
9. Step-by-Step Backup Procedure
Combine the procedures above into the following ordered workflow. This is the recommended sequence for a planned maintenance window.
- Announce the backup window to operations. Tag logging and alarm logging continue until step 3.
- On each WinCC client, close the runtime. Confirm in WinCC Explorer on the server that the client count is zero: Tools → Status of Server → Connections.
- On the server, use File → Close Project and select Close project and Exit Explorer. Wait for the Explorer window to close.
- Verify with
handle.exe -a | findstr /I winccfrom Sysinternals that no WinCC process is running. (If Sysinternals is not available, use Task Manager with Show processes from all users enabled.) - Attempt a file copy. If it succeeds, go to step 9. If it fails with a sharing violation, continue to step 6.
- Open an elevated command prompt and run
cscript //NoLogo "<wincc-bin>\Reset_WinCC.vbs". - Retry the file copy. If it fails again, continue to step 8.
- Stop the SQL service with
net stop MSSQL$WINCCand perform the copy. Restart the service after the copy withnet start MSSQL$WINCC. - Once the copy is complete, perform a native SQL backup of the project database and the archive database for an additional safety net.
- Restart WinCC Explorer and validate the project by switching to runtime for at least 60 seconds. Confirm tag logging and alarm logging are updating.
10. Verification
After a backup, validate it by performing the following checks before relying on the archive.
| # | Check | Method | Pass criterion |
|---|---|---|---|
| 1 | File count parity |
robocopy "%SRC%" "%DST%" /L /E /NJH /NJS lists any extra or missing files |
Empty list |
| 2 | ACL parity |
icacls "%SRC%\*" > src.txt against %DST%; diff the two |
No differences |
| 3 | Database integrity | Restore the native SQL .bak onto a scratch server; run DBCC CHECKDB
|
DBCC execution completed. If no errors were printed, contact your system administrator. |
| 4 | Project start | Copy archive to a scratch server, configure host entries, start WinCC | Runtime starts; last-value tag logging shows recent entries |
| 5 | Configuration consistency | Diff the project file <ProjectName>.pck SHA-256 against the source |
Hashes match |
| 6 | Segment continuity | Open Tag Logging editor on the restored project; verify last segment timestamp matches the last entry in the database | No gap |
Reference for the integrity check: Microsoft Learn: DBCC CHECKDB.
11. Troubleshooting Matrix
| Symptom | Probable cause | First action | Second action |
|---|---|---|---|
Access denied on .MDF when copying with Explorer |
SQL handles still open | Run Reset_WinCC.vbs
|
Stop MSSQL$WINCC service and copy |
| Robocopy retries files indefinitely | SQL handles still open | Stop MSSQL$WINCC service and copy |
Check for orphaned WinCC client connections on the server |
| The system cannot find the file specified for Reset_WinCC.vbs | Wrong path / version mismatch | Search for the script: dir /s Reset_WinCC.vbs %SystemDrive%
|
Reinstall WinCC runtime if the file is missing |
| Reset_WinCC.vbs runs but files remain locked | WinCC installed under path with spaces | Move WinCC to a path without spaces, or stop MSSQL$WINCC manually |
Open a support request via Siemens Industry Online Support |
| Backup completes, but project does not start on the restore server | NTFS ACLs were not copied | Re-run copy with /COPYALL
|
Apply original ACLs with icacls /save on source and icacls /restore on destination |
Native SQL .bak restore fails with database in use
|
SQL service account on target has active connections | Set database to single-user mode: ALTER DATABASE [name] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
|
Retry restore |
Robocopy reports ERROR 5 (0x00000005) on hidden files |
Source files are marked hidden or system | Re-run robocopy with /A-:SH to include hidden and system files |
Check ACL on the hidden files explicitly |
| Backup takes hours even though project is small | Antivirus on-access scan is locking files | Add WinCC project directory to AV exclusion list | Schedule AV full scan outside the backup window |
Copy succeeds on the project folder but DBCC CHECKDB reports corruption |
File copy ran while SQL was active and an open page was missed | Discard the file-copy backup; rely on the native SQL .bak
|
Restore from the last known good .bak and replay transaction logs |
| Backup script returns ERROR: could not stop MSSQL$WINCC | Active client connections prevent service stop | Run Reset_WinCC.vbs first to drop client sessions, then retry |
Wait for the SQL service stop timeout (default 30 s) and check Event Viewer |
12. Edge Cases and Field-Proven Caveats
12.1 Project folder named "WinCC"
A documented internal issue exists when the WinCC project folder is named WinCC (case insensitive). The detach sequence uses the project name as part of an internal key, and the literal name causes a key collision. Rename the project to a unique name (for example, the company name or plant identifier) and the symptom disappears.
12.2 WinCC client keeps a connection
On a multi-client system, simply logging off a client is not enough. The client must close the runtime from the WinCC Explorer toolbar, or the connection is reused on the next login. Use netstat -ano | findstr :<port> on the server, where the port is the WinCC server port (default 5017), to identify clients that still hold TCP sessions.
12.3 Anti-virus interference
Real-time anti-virus scanners (Windows Defender, Trend Micro, McAfee) take brief file locks during on-access scans. The locks are short but frequent enough to cause robocopy to retry and to cause random zip-archive failures. Configure an exclusion for the WinCC project directory in the AV console, or run the backup during a maintenance window where real-time scanning is paused.
12.4 VSS snapshots versus file copy
Some administrators use Volume Shadow Copy Service to back up the WinCC project while it is running. This is generally safe for the configuration files but does not guarantee a transactionally consistent copy of the SQL database files unless the SQL Server VSS writer is registered and active. For a project where the archive database is mission-critical, run a native SQL .bak backup in addition to any VSS-based file copy.
12.5 Distributed system with redundant servers
On a WinCC Redundancy configuration, both servers attach the same project database at start-up. The backup must be performed on the server that is currently the master, or the SQL service must be stopped on both servers before the file copy. After the copy, restart the services and re-establish the redundancy pair with the WinCC Redundancy control panel.
12.6 Scheduled task integration
To schedule the script, create a Windows Scheduled Task with the following settings:
- Run as: a domain account that is a local administrator on the WinCC server and a member of
SQLServerMSSQLUser$<COMPUTERNAME>$MSSQLSERVER. - Run whether user is logged on or not: enabled.
- Run with highest privileges: enabled.
- Trigger: weekly at the planned maintenance window.
- Action:
C:\Windows\System32\cmd.exe /c C:\Scripts\backup_wincc.bat
12.7 Disk space and growth
Tag logging segments grow over time. A project that fits in 4 GB at commissioning can exceed 50 GB within a year. Configure the SQL archive to retain only the last 90 days in the primary database, and back up the older segments separately using the WinCC Segment Manager or the SQL Server maintenance plan. The file copy of the live project should still fit in 2–3× the on-disk size of the configuration files plus the current archive segment.
12.8 UAC and elevated services
The net stop and net start commands only succeed when the prompt is elevated, even if the calling user is a local administrator. The backup script in section 7 must therefore be invoked from an elevated Scheduled Task, or from an interactive admin console. Otherwise the stop command silently returns a permission error after a 30-second timeout.
12.9 Network share destination latency
Copying the WinCC project to a network share (UNC path) at gigabit speeds can take 30–60 minutes for a 20 GB project. The SQL service is stopped for the duration; tag logging and alarm logging are paused, and any client request fails with server unavailable. Schedule the window accordingly and inform the operations team. Where possible, copy to a local USB drive first and then copy from the drive to the network share, so the SQL service downtime is minimised.
12.10 What does NOT need a SQL service stop
Graphics files (*.pdl), C and VBS scripts, the text library, and the alarm logging configuration can be backed up at any time, even while the runtime is active. The runtime briefly opens the .pdl files but releases them after the picture cache is loaded. A copy of those files is useful for source control but is not a full project backup.
Frequently Asked Questions
Why does the SQL service lock the WinCC database files even after I close the project?
WinCC normally issues an sp_detach_db command to MSSQL$WINCC when the project is closed, releasing the handles. The detach is skipped or fails silently when the WinCC installation path contains a space, when the project is closed via Exit Explorer instead of Close project and Exit Explorer, or when a stale client connection keeps a session alive. The first remediation is to close the project through the correct menu, the second is to run Reset_WinCC.vbs, and the last resort is to stop the MSSQL$WINCC service during the backup window.
Is it safe to use Reset_WinCC.vbs as a routine shutdown tool?
No. The tool is documented for exceptional cases only. Routine use can interrupt a tag logging segment mid-write, which can leave the archive database in a state that requires DBCC CHECKDB to recover. Use File → Close Project → Close project and Exit Explorer for routine shutdown, and reserve Reset_WinCC.vbs for the situation where the project does not close cleanly.
Which Windows service must be stopped to release the .MDF files?
The service is named MSSQL$WINCC (display name SQL Server (WINCC)). The exact stop command is net stop MSSQL$WINCC from an elevated command prompt. The service is then restarted with net start MSSQL$WINCC after the file copy completes. Expect a 5–20 second wait for the stop to complete, longer on systems with very large open archive segments.
Do I need to stop MSSQL$WINCC if I use a native SQL .bak backup?
No. A native SQL Server backup runs through the SQL engine itself, so the .bak file is written by the service and is fully consistent. The file copy of the .MDF is the operation that conflicts with the service handles. Many production sites use both: a file copy for full project migration, and a .bak for daily archive integrity.
Can I use Volume Shadow Copy Service (VSS) to back up the live WinCC project?
VSS can capture the configuration files in a consistent state because the VSS writer coordinates with the applications. However, VSS does not guarantee a transactionally consistent copy of the SQL database files unless the SQL Server VSS writer is registered and active. For a project where the archive database is mission-critical, run a native SQL .bak backup in addition to any VSS-based file copy.
Why is the project folder name "WinCC" problematic?
The internal detach sequence uses the project folder name as part of a key, and the literal name WinCC causes a collision that prevents the detach from completing. Rename the project to a unique identifier (for example, the plant or company name) and the lock problem disappears. The error manifests as persistent access-violation errors on the .MDF file even after WinCC Explorer is closed.
How do I back up a WinCC Redundancy pair?
Back up the master server while it is the active partner. Stop the standby server's WinCC services first, then run the backup on the master. Alternatively, stop both MSSQL$WINCC services, perform the file copy on both servers in parallel, then restart the services and re-establish the redundancy pair. Do not perform a file copy on both servers while both are active; the SQL services would race on the shared database.