Copy an MFSQL database on the same server without the installer

This page describes how to make a second copy of an MFSQL Connector database on the same SQL Server instance by backup and restore, without running the installation package against the copy. Use it for a test copy, a verification copy, or a copy to point at another vault, such as an M-Files Cloud vault, when the source database must not be touched.

In the steps, <SourceDb> is the database being copied, <CopyDb> is the new copy, and <Owner> is the owner of the source database.

Note

How far this method is proven. It has been run once, end to end, against a production MFSQL database: backup, restore under a new name, the copy made independent of the source, the copy pointed at an M-Files Cloud vault, and spMFVaultConnectionTest succeeded. The source database was checked afterward and was unchanged. Filling the copy’s class tables from the vault (spMFDropAndUpdateMetadata with a class table reset) has not yet been proven with this method. Until it is, treat the method as proven up to the connection test.

When to use this method

The recommended way to create a database with a new name is to install the Connector into a new database with the installation package (see Moving the Connector Database). Use this method instead only when that route is not possible or not safe:

  • The installation package for the version the source database runs is not at hand.

  • The server’s shared assemblies folder (MFSettings.AssemblyInstallPath, typically C:\MFSQL\Assemblies\) holds files of a different version than the latest row of MFDeploymentDetail in the source database records. A package install writes into that shared folder, so it can change the files that every other MFSQL database on the server reloads from.

To move a database to a different server, follow Restore MFSQL database to a different server instead. This method relies on the assemblies stored in the database matching the assemblies folder and the M-Files client on the same server.

Why a restored copy is not safe as restored

Installation files and some procedures are compiled for each database name during installation. In a copy restored under a new name, the following still point at the source database:

  1. The App_Database row of MFSettings (source_key App_Default) still holds <SourceDb>.

  2. spMFUpdateAssemblies carries <SourceDb> as its default @DBName and contains ALTER DATABASE [<SourceDb>] SET TRUSTWORTHY ON. Run in the copy as restored, it acts on the source database.

  3. Custom procedures may refer to <SourceDb> by a three-part name, so the copy reads, or writes, the source database’s tables.

The restore also resets two database properties: TRUSTWORTHY is off, and the owner is the login that ran the restore. The Connector’s CLR assemblies are stored in the database and come across with the restore unchanged. They are UNSAFE assemblies, which the source database runs with TRUSTWORTHY on.

Steps

Every read below is read-only. Every write targets <CopyDb> only and names it in full, so a query window that is connected to the wrong database cannot write to the source.

  1. Take an inventory of the source (read-only). Record, as the baseline:

    • SQL Server Agent jobs that name <SourceDb> in a job step, command or job name, with their schedules and whether they are enabled;

    • custom objects outside the dbo schema;

    • triggers on tables (sys.triggers with parent_class = 1), database-level triggers, synonyms, and Service Broker queues with activation;

    • the App_Database setting;

    • fnMFLastConnectorVersion() (see fnMFLastConnectorVersion) and the latest MFDeploymentDetail rows;

    • the file versions in the assemblies folder and sys.assemblies.modify_date.

    Do not select job step command text or any password column.

  2. Take a copy-only backup. First check that the backup folder exists: the instance’s default backup folder setting can name a folder that does not exist.

    BACKUP DATABASE [<SourceDb>]
        TO DISK = N'<existing folder>\<file>.bak'
        WITH COPY_ONLY, CHECKSUM, STATS = 10;
    

    Confirm the backup in msdb.dbo.backupset: is_copy_only = 1, its size, and its start and finish times.

  3. Restore under the new name. Read the logical file names with RESTORE FILELISTONLY. They may be names from an earlier life of the database; MOVE needs them exactly as they are.

    RESTORE DATABASE [<CopyDb>]
        FROM DISK = N'<existing folder>\<file>.bak'
        WITH MOVE N'<data logical name>' TO N'<new path>\<CopyDb>.mdf',
             MOVE N'<log logical name>' TO N'<new path>\<CopyDb>_log.ldf',
             CHECKSUM, STATS = 10;
    

    Never use REPLACE. Without it, an existing database with the same name makes the restore fail instead of overwriting it. The restore allocates the source’s full data file size up front, so check the free disk space first.

  4. Find every module that names the source. Search <CopyDb>.sys.sql_modules for <SourceDb> and for App_Database, and return the position of each hit with about 120 characters around it. Sort the hits:

    • a hard-coded database name: fix it in step 5;

    • a column that happens to be named App_Database: leave it;

    • a comment: leave it;

    • a read of the App_Database setting: step 6 fixes it.

  5. Rewrite the hard-coded name in the copy’s procedures. Use one script with a dry-run switch that, for each procedure found in step 4:

    • reads the procedure’s definition from <CopyDb> and changes its leading CREATE to ALTER;

    • replaces only the delimited forms that cannot match the new name: [<SourceDb>], '<SourceDb>' and <SourceDb>.<schema>.;

    • keeps the module’s stored ANSI_NULLS and QUOTED_IDENTIFIER settings;

    • runs the change with EXEC [<CopyDb>].sys.sp_executesql;

    • sizes every output column for the widest value, because a truncation error stops the batch before the write.

    Run the dry run first. Apply the change only when the number of replacements equals the number of hits found in step 4, no <SourceDb> is left, and each definition starts with a plain CREATE PROC... with the procedure’s own name (not CREATE OR ALTER).

  6. Update the copy’s App_Database setting. Run the update in a transaction that commits only when exactly one row changed:

    UPDATE [<CopyDb>].dbo.MFSettings
    SET    Value = CONVERT(sql_variant, CONVERT(nvarchar(100), N'<CopyDb>'))
    WHERE  Name = N'App_Database'
      AND  source_key = 'App_Default';
    

    MFSettings marks this value “do not edit” because the installer sets it. In a copy made without the installer, this update does the installer’s job. Value is a sql_variant: convert it before you use LIKE on it.

  7. Set the owner, then TRUSTWORTHY, to match the source. Change the owner first: TRUSTWORTHY on a database owned by a personal login is the risky combination.

    ALTER AUTHORIZATION ON DATABASE::[<CopyDb>] TO [<Owner>];
    ALTER DATABASE [<CopyDb>] SET TRUSTWORTHY ON;
    
  8. Make sure the copy cannot act by itself.

    • Confirm that no SQL Server Agent job step targets <CopyDb>. No installer ran, so none was created.

    • Decide on the error mail trigger tMF_OnError_SendEmail on MFLog. It mails through the SupportEMailProfile and SupportEmailRecipient settings the copy inherited from the source (see User messages and notifications), so disable it in the copy unless those mails are wanted.

    • Do not run any custom integration procedure in the copy.

    Write down what can still run in the copy, and why none of it can start by itself.

  9. Check for orphaned users. On the same server, expect none.

    EXEC [<CopyDb>].sys.sp_change_users_login 'Report';
    
  10. Point the copy at its vault. Run spMFsettingsForVaultUpdate in <CopyDb>, in your own query window, and type the password only there. Take the cloud values from a database on the same server that already connects to a cloud vault:

    • @MFProtocolType_ID = 5 (gRPC), @Endpoint = 443 and @MFAuthenticationType_ID = 4;

    • @NetworkAddress: the bare host name, <vault>.cloudvault.m-files.com;

    • @Vaultname: the cloud vault’s name;

    • @VaultGUID: the cloud vault’s own GUID, with braces. The Connector stores it in MFSettings.VaultGUID, not in MFVaultSettings.

    The procedure returns 1. It also returns a result set that includes the EncryptedPassword column: do not paste that output anywhere.

  11. Test the connection. In <CopyDb>:

    EXEC dbo.spMFVaultConnectionTest;
    

    See spMFVaultConnectionTest.

  12. Prove that the source is unchanged. Rerun the job query from step 1 and compare it with the baseline. Check App_Database, fnMFLastConnectorVersion() and the latest MFDeploymentDetail rows, the source’s MFVaultSettings and VaultGUID, and the modify_date of the source’s procedures that were rewritten in the copy (it should not be today).

Notes and limits

  • The compiled .NET code cannot be searched with T-SQL, so whether it reads App_Database cannot be checked this way. Step 6 covers it either way.

  • A later install or upgrade of the copy with the installation package replaces the rewritten procedures with the package’s own. That is expected.

  • The SQL Server Management Studio results grid truncates long text. Read procedure definitions by position and context, not whole.

  • Log every write with who ran it and when. Keep only counts and names in the log.