Troubleshooting Guide: SQL Server Error 3131 — File Renamed Since Backup
Why This Error Happens
Error 3131 occurs when SQL Server detects that a database file’s physical path or filename stored in the backup metadata no longer matches the current name or location of that file on disk at the time of restore. This typically happens when a .mdf, .ndf, or .ldf file was manually renamed, moved, or recreated after the backup was taken, causing a mismatch between the backup header information and the actual file system state. This is informational in nature (SQL Server will proceed using the renamed file), but it can mask underlying issues such as broken maintenance scripts, inconsistent file management practices, or incomplete migration steps that may lead to confusion or improper RESTORE WITH MOVE configurations.
Step-by-Step Resolution Guide
Step 1: Verify the Backup File Metadata
- Open SQL Server Management Studio (SSMS) or a query window and run:
RESTORE FILELISTONLY FROM DISK = 'C:\Backups\YourDatabase.bak';
- Compare the
PhysicalNamecolumn output against the actual files currently present in your data/log directories (commonlyC:\Program Files\Microsoft SQL Server\MSSQL17.MSSQLSERVER\MSSQL\DATA\). - Note any discrepancies between the logical name and the physical name referenced in the backup.
Step 2: Confirm Current File Names and Paths on Disk
- Navigate to the SQL Server data directory using File Explorer or PowerShell:
Get-ChildItem "C:\Program Files\Microsoft SQL Server\MSSQL17.MSSQLSERVER\MSSQL\DATA\" -Filter *.mdf,*.ldf,*.ndf
- Identify whether the file was renamed intentionally (e.g., during a migration) or unintentionally (e.g., by an automated script or human error).
- Document the current file names for reference in the next step.
Step 3: Use RESTORE WITH MOVE to Explicitly Map Files
- Avoid relying on SQL Server’s automatic renamed-file handling by explicitly specifying file locations:
RESTORE DATABASE YourDatabase
FROM DISK = 'C:\Backups\YourDatabase.bak'
WITH MOVE 'YourDatabase_Data' TO 'C:\Program Files\Microsoft SQL Server\MSSQL17.MSSQLSERVER\MSSQL\DATA\YourDatabase.mdf',
MOVE 'YourDatabase_Log' TO 'C:\Program Files\Microsoft SQL Server\MSSQL17.MSSQLSERVER\MSSQL\DATA\YourDatabase_log.ldf',
REPLACE;
- Ensure the logical names (
YourDatabase_Data,YourDatabase_Log) match exactly what was returned in Step 1’sRESTORE FILELISTONLYoutput. - This removes ambiguity and prevents SQL Server from silently continuing with an unexpected file reference.
Step 4: Check File System Permissions and File Locks
- Confirm that the SQL Server service account (e.g.,
NT Service\MSSQLSERVER) has Full Control permissions on the target restore directory. - Use Resource Monitor or
handle.exe(Sysinternals) to check if another process has a lock on the renamed file, which can cause SQL Server to reference stale path information. - Restart the SQL Server service if a stale file handle is suspected:
Restart-Service MSSQLSERVER -Force
Step 5: Audit Scripts and Maintenance Jobs for Unintended Renames
- Review any SQL Agent jobs, PowerShell scripts, or third-party backup tools that interact with the database files.
- Look specifically for file-move, rename, or archival steps that execute between backup and restore operations.
- Update automation scripts to maintain consistent file naming conventions to prevent recurrence.
Alternative or Verification Steps
- After restoring, run
sp_helpdb YourDatabaseor querysys.master_filesto confirm the database is now pointing to the correct, consistent file paths with no orphaned references. - If the issue persists across multiple restores, consider standardizing your backup/restore process using a fixed, scripted template with explicit
MOVEclauses rather than relying on default SQL Server file resolution behavior.