DB Snapshot
How I take a SQL Server database snapshot of AxDB on a D365 F&O dev box, and roll back to it later, using d365fo.tools to stop and start the services.
Before a risky test on my D365 F&O dev box I take a database snapshot of AxDB. If something goes wrong, I restore it in a minute instead of restoring a full backup.
We need the d365fo.tools PowerShell module to stop and start the services. Run PowerShell as administrator to install it.
Install-Module -Name d365fo.tools
Import-Module -Name d365fo.tools
Stop all the services with PowerShell.
Stop-D365Environment -All
Create a snapshot
The NAME in the snapshot must be the logical name of the AxDB data file, not the database name. If you don’t know it, look it up first:
USE AxDB;
SELECT name, physical_name, type_desc
FROM sys.database_files;
Use the row with type_desc = ROWS. If there is more than one data file, every one of them needs its own ( NAME = ..., FILENAME = ... ) entry. The log file is not included in a snapshot.
CREATE DATABASE AxDB_Snapshot_20250911 ON
( NAME = 'AxDB',
FILENAME = 'J:\Snapshots\AxDB_Snapshot_20250911.ss' )
AS SNAPSHOT OF AxDB;
The folder must exist and be on an NTFS volume, because the snapshot is a sparse file. It only grows when pages in AxDB change.
Test the snapshot:
SELECT
name AS snapshot_name,
database_id,
source_database_id,
create_date,
state_desc
FROM sys.databases
WHERE source_database_id = DB_ID('AxDB');
Start all the services again.
Start-D365Environment -All
Work normally and do something.
Restore the snapshot
Stop all the services with PowerShell.
Stop-D365Environment -All
List the sessions that are still using AxDB or the snapshot:
USE master;
SELECT
session_id,
login_name,
program_name,
DB_NAME(database_id) AS db
FROM sys.dm_exec_sessions
WHERE database_id IN (DB_ID('AxDB'), DB_ID('AxDB_Snapshot_20250911'));
Kill them if any exist (use the session_id from the query):
KILL 999;
Restore the snapshot. SET SINGLE_USER WITH ROLLBACK IMMEDIATE also kicks out the remaining connections to AxDB. Note that the restore only works if this is the only snapshot of AxDB, so drop the other snapshots first.
USE master;
ALTER DATABASE AxDB SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
RESTORE DATABASE AxDB FROM DATABASE_SNAPSHOT = 'AxDB_Snapshot_20250911';
ALTER DATABASE AxDB SET MULTI_USER;
Start all the services with PowerShell.
Start-D365Environment -All
The snapshot is still there after the restore, so you can go back to it again later.
Drop a snapshot
When you don’t need it anymore, drop it like a normal database:
USE master;
DROP DATABASE AxDB_Snapshot_20250911;