> ## Documentation Index
> Fetch the complete documentation index at: https://www.artie.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Microsoft SQL Server source: transaction log backups

> Replicate Microsoft SQL Server by reading its transaction log backups (.trn files) from an SMB share, with supplemental logging so updates are read from the log instead of the database.

> **Note:**
>
> Transaction log backups are in early access. Contact Artie to enable **Transaction log backups** for your account.

With this method, Artie reads changes from your transaction log backup (`.trn`) files as they land in a shared folder, instead of from the live database. It adds the least load to your database. Latency depends on how often you take log backups.

## What you'll need

* SQL Server 2019 and later
* Database recovery model set to `FULL` or `BULK_LOGGED`
* Log backups taken on a schedule to a folder on an SMB share Artie can reach, such as Amazon FSx for Windows File Server. See [Backup folder and file names](#backup-folder-and-file-names).
* A primary key on each replicated table
* [Supplemental logging](#supplemental-logging) on each replicated table
* A service account with `SELECT` on the database, for table metadata and off-row values

## Setup

**Step 1: Configure log backups**

Write every log backup for the database to one folder, with file names that end in `_YYYYMMDD_HHMMSS.trn`. For example, [Ola Hallengren's maintenance solution](https://ola.hallengren.com/sql-server-backup.html) writes names in this format.

```sql
EXECUTE dbo.DatabaseBackup
  @Databases = 'ANALYTICS',
  @Directory = '\\fs-0123456789abcdef0.corp.example.com\share\sqlbackups',
  @BackupType = 'LOG',
  @DirectoryStructure = NULL,
  @Compress = 'Y';
```

Use backup compression only with the `MS_XPRESS` (default) or `ZSTD` algorithm.

**Step 2: Enable supplemental logging**

Follow [Supplemental logging](#supplemental-logging) for each replicated table.

**Step 3: Add the SMB share in Artie**

In [Settings > Connectors](https://app.artie.com/settings?tab=connectors), add an SMB share connector:

* **UNC path**: the folder your log backups go to, the same path as in `BACKUP LOG ... TO DISK`. For example, `\\fs-0123456789abcdef0.corp.example.com\share\sqlbackups`.
* **Username** and **Password**: a user that can read the folder, as `DOMAIN\user` or `user@domain`.
* **PrivateLink connection (optional)**: pick one if the share is only reachable privately. It must forward port 445.

**Step 4: Enter the connection in Artie**

In **Source**, enter the database connection. Under **Replication method**, select **Transaction log backups**. Under **Where are the transaction log backups stored?**, pick the share from the previous step in the **SMB share** list.

## Supplemental logging

By default, SQL Server logs only the bytes an `UPDATE` changed. For those tables, Artie looks the row up in the database when it reads the backup, so the message carries the row as it is at that point rather than as of the update. An update to a row that has since been deleted is lost.

With supplemental logging, SQL Server logs the whole row for every update, and Artie reads updates straight from the backup. Enable it on each table in one of two ways:

**CDC (recommended)**

```sql
USE [ANALYTICS];
GO

EXEC sys.sp_cdc_enable_db;
GO

-- For each table you want to replicate:
EXEC sys.sp_cdc_enable_table @source_schema = N'dbo', @source_name = N'orders', @role_name = NULL;
GO

-- Artie does not read the cdc change tables, so stop SQL Server from filling them.
EXEC sys.sp_cdc_drop_job @job_type = N'capture';
GO
```

On Amazon RDS, enable CDC on the database with `EXEC msdb.dbo.rds_cdc_enable_db 'ANALYTICS';` instead of `sys.sp_cdc_enable_db`.

**Transactional publication**

Use this if you can't enable CDC. It requires a distributor and isn't available on Amazon RDS.

```sql
-- Once per server, if no distributor is configured:
EXEC sp_adddistributor @distributor = @@SERVERNAME, @password = N'';
EXEC sp_adddistributiondb @database = N'distribution';
EXEC sp_adddistpublisher @publisher = @@SERVERNAME, @distribution_db = N'distribution';

EXEC sp_replicationdboption @dbname = N'ANALYTICS', @optname = N'publish', @value = N'true';
GO

USE [ANALYTICS];
GO

EXEC sp_addpublication @publication = N'artie', @status = N'active', @repl_freq = N'continuous';

-- For each table you want to replicate:
EXEC sp_addarticle @publication = N'artie', @article = N'orders', @source_owner = N'dbo', @source_object = N'orders', @type = N'logbased';

-- SQL Server only logs whole rows once the publication has a subscription.
EXEC sp_addsubscription @publication = N'artie', @subscriber = @@SERVERNAME, @destination_db = N'ANALYTICS_SUBSCRIBER',
    @subscription_type = N'push', @sync_type = N'replication support only', @article = N'all';
GO
```

The subscriber database must already exist and can't be `master`. No agents need to run.

To check, `is_replicated` should be `1` for each table:

```sql
SELECT s.name AS schema_name, t.name AS table_name, t.is_replicated
FROM sys.tables t
JOIN sys.schemas s ON t.schema_id = s.schema_id;
```

When the pipeline starts, Artie logs each configured table that doesn't have supplemental logging.

### Letting the log truncate

Both options mark every transaction on these tables for replication. SQL Server keeps each one in the log until something marks it as distributed. Without the CDC capture job or a Log Reader Agent, `log_reuse_wait_desc` in `sys.databases` stays at `REPLICATION`, and the log keeps growing, even with regular log backups.

Artie reads your log backups, so it's safe to mark everything as distributed on a schedule. Each log backup still contains every change. Schedule this with SQL Server Agent:

```sql
USE msdb;
GO

EXEC sp_add_job @job_name = N'artie_mark_distributed';
EXEC sp_add_jobstep @job_name = N'artie_mark_distributed', @step_name = N'sp_repldone', @database_name = N'ANALYTICS',
    @command = N'EXEC sp_repldone @xactid = NULL, @xact_segno = NULL, @numtrans = 0, @time = 0, @reset = 1;';
EXEC sp_add_jobschedule @job_name = N'artie_mark_distributed', @name = N'every 15 minutes',
    @freq_type = 4, @freq_interval = 1, @freq_subday_type = 4, @freq_subday_interval = 15;
EXEC sp_add_jobserver @job_name = N'artie_mark_distributed';
GO
```

> **Warning:**
>
> Only run `sp_repldone` if nothing else reads these transactions: no CDC capture job, and no Log Reader Agent for another publication on the database.

Every 5 minutes, Artie checks whether the log is waiting on `REPLICATION`. It logs a warning once a database has been waiting for over an hour.

## Backup folder and file names

Artie lists one folder, without subfolders, and reads files whose names end in `_YYYYMMDD_HHMMSS.trn`. It reads them in file name order and skips any file whose name sorts before the last file it read.

* **Use one folder per database, and one naming prefix.** If backups land in subfolders, or different servers write different prefixes, Artie can skip files.
* **Keep every log backup in the folder until Artie has read it.** Artie doesn't check that each backup starts where the previous one ended.

> **Warning:**
>
> A log backup that is missing, moved, or renamed before Artie reads it isn't detected, and its changes aren't replicated. Backfill the affected tables if this happens.

Artie treats a file as complete once its size is the same in two checks 3 seconds apart, and the writer has closed it.

For availability groups, see [Log backups in an availability group](/docs/sources/microsoft-sql-server/high-availability#log-backups-in-an-availability-group).

## Advanced

**How Artie reads log backups**

* Every 5 minutes, Artie looks for new backup files in the folder.
* Artie parses each file's backup container and log records, and builds row changes from the log records of the configured tables.
* Artie publishes only committed transactions, in commit order. It drops rolled-back transactions.
* A transaction that spans several log backups is held until it ends. Once its changes pass a few MB, Artie writes them to a spool directory on disk. If open transactions need more than 10 GiB of spool, the pipeline stops with `transactions open across log backups need more than trnCdcSettings.spoolMaxBytes`. End the transaction, or contact Artie to raise the limit.
* Artie saves its position (the last file and the commit LSN) only after changes are written to its Kafka buffer. After a restart, it rereads from the first file with a still-open transaction and skips changes it has already published.

**When Artie can't read a change exactly**

SQL Server doesn't publish its log backup format. Artie checks each change against the table's layout and stops instead of guessing. The pipeline stops with:

```
halted at a change that cannot be published exactly: <kind> at LSN <lsn>, table <database.schema.table>
```

Nothing from that change on is published until it's resolved. Contact Artie with the error. Resolving it usually means a backfill of the named table.

Changes Artie stops on include an update logged without its whole row on a table with supplemental logging, an update whose row doesn't match the table's columns, and an update to an off-row value.

**Off-row columns**

Values stored off-row, such as large `VARCHAR(MAX)` values, aren't in the log with the rest of the row.

* **Inserts:** Artie reads off-row values from the database. If the row has been deleted by then, they're published empty.
* **Updates that leave an off-row value unchanged:** the value stays as it is in the destination.
* **Updates that change an off-row value:** the pipeline stops with `update_off_row_value_changed`. Backfill the table, or exclude the column.

## Limitations

* **Encrypted databases and backups:** TDE-encrypted databases and encrypted backups aren't supported.
* **Backup compression:** `QAT_DEFLATE` isn't supported. Use `MS_XPRESS` or `ZSTD`.
* **Backup location:** only SMB shares are supported. Azure Blob Storage isn't supported yet.
* **Accelerated database recovery and heap tables with row versioning:** updates aren't certified. Contact Artie before using them.
* **`TRUNCATE TABLE`** isn't allowed on tables tracked by CDC or published for replication.
* **Latency** is at least your log backup interval. Take log backups every few minutes for lower latency.