[KongchangAI]
· 2 min read· 1,052 words

sqlite3_rsync: A Remote Backup Tool for SQLite Without Local Copies

sqlite3_rsync: A Remote Backup Tool for SQLite Without Local Copies

sqlite3_rsync uses page-level differential transfer over SSH to remotely back up SQLite snapshots without local copies.

sqlite3_rsync is a low-key yet practical remote backup tool in the SQLite ecosystem, added experimentally in SQLite 3.47.0 and enhanced in 3.50.0. It overcomes the traditional backup bottleneck (backup API or VACUUM INTO) that requires local disk space for a full database copy by transferring the snapshot directly to a remote machine over SSH. Its core is hash-based differential transfer over SQLite's page structure: the replica sends page hashes, the origin returns only changed pages, and syncing an existing replica adds at most a few percentage points of bandwidth overhead. Unlike regular rsync, it ensures consistent snapshots, making it suitable for backing up running databases, especially with tight disk, remote backup needs, or minimal bandwidth.

sqlite3_rsync remote backup tool

There's a low-key but practical tool in the SQLite ecosystem: sqlite3_rsync. It was added as an experimental feature back in SQLite 3.47.0 and later enhanced in SQLite 3.50.0. Despite having been around for a while, one of its key characteristics has only recently been fully appreciated—the ability to copy a snapshot to another machine over SSH without creating a full local copy of the database.

The Disk Bottleneck of Traditional Backup Methods

Normally, to make a snapshot copy of a SQLite database, you'd use the backup API or the VACUUM INTO command. Both methods require enough free local disk space to hold a complete copy of the database. If you're running on a small instance and disk usage is already over half, then unfortunately—you can't complete the backup.

This limitation is quite common in real-world operations: small VPSs, embedded devices, or container environments often have tight storage, and database files may keep growing. Traditional approaches force you to clear space before backing up, or risk copying files directly (potentially getting an inconsistent snapshot).

VACUUM INTO is a command introduced in SQLite 3.27.0 that rebuilds the current database contents into a new database file, defragmenting and rebuilding indexes in the process, so the resulting copy is compact and consistent. The backup API is the C interface provided by SQLite (such as sqlite3_backup_init, sqlite3_backup_step) that allows pages to be copied incrementally between database connections, commonly used for online backups. Both require allocating local disk space roughly equal to the source database size to hold the copy—this is the fundamental limitation in disk-constrained scenarios.

How sqlite3_rsync Bypasses the Disk Limitation

The core innovation of sqlite3_rsync is that it transfers the database snapshot directly to a remote machine over an SSH connection, without needing to generate a full local copy first. This means that even if local disk space is very limited, you can still safely back up the database.

It works similarly to regular rsync, but is optimized for SQLite's page structure. According to the official documentation:

The core idea of the protocol is: the replica side sends cryptographic hashes of pages or groups of pages to the origin side. The origin returns pages with different content, or requests finer-grained hashes when multi-page hashes don't match. If the origin and replica initially differ significantly, total network bandwidth may exceed the size of the entire database due to the overhead of exchanging hashes, but the excess is small—at most a few percentage points.

This hash-based differential transfer mechanism means that syncing an existing replica doesn't require retransmitting the entire database, greatly saving network bandwidth.

A SQLite database file consists of fixed-size pages (4096 bytes by default), which are the basic unit of reading, writing, and caching. The database header records metadata such as page size and page count, and structures like B-trees are also organized in units of pages. Page-level differential transfer leverages this structure: it first computes hashes per page, then compares hashes on both ends, and only transmits pages with different content. This both pinpoints changes precisely and avoids whole-file transmission, forming the basis for efficient incremental sync.

Key Differences from Regular rsync

Although it has rsync in its name, sqlite3_rsync is not simply copying files. It understands SQLite's page structure and can ensure that what's transmitted is a consistent snapshot, rather than a file that might be in the middle of a write. This is especially important for a running database—directly rsyncing an active SQLite file could yield a corrupted copy.

Another advantage is incremental sync capability: if the target already has an older copy of the database, the tool only transfers changed pages rather than the entire file. For frequent backup scenarios, this can significantly reduce network and time overhead.

Regular rsync operates at the file or block level and doesn't know about SQLite's page layout or transaction state. Therefore, copying while the database is being written could read partially written pages or B-trees in intermediate states, resulting in a corrupted copy. sqlite3_rsync, on the other hand, follows SQLite's consistent snapshot semantics during transfer, ensuring the copy corresponds to a complete state at some transaction boundary, so it's safe to back up while the database is active.

Use Cases and Caveats

sqlite3_rsync is particularly suited for the following situations:

  • Local disk space is insufficient to create a full copy
  • Need to push database snapshots to a remote backup server
  • Periodically sync the database to another machine with minimal bandwidth consumption
  • A running database needs a consistent backup without downtime

Note that the tool was initially released as experimental, so you should check the stability notes for your current SQLite version before use. Additionally, it relies on SSH connections, so remote access permissions need to be configured.

An Overlooked Practical Tool

The value of sqlite3_rsync lies in solving a specific and common pain point: remote backup when disk space is constrained. Its design approach—leveraging SQLite's page-level structure for differential transfer—both ensures snapshot consistency and optimizes network efficiency. For developers managing SQLite, this tool deserves a place in your backup strategy toolbox.

According to Simon Willison's blog, he introduced this application when the tool was first released, but only recently rediscovered its unique value. This also shows that some seemingly niche tools in the SQLite ecosystem can play a key role in specific scenarios.

One-line Summary

sqlite3_rsync enables remote backup of SQLite snapshots over SSH via page-level differential transfer, with no local copy needed.

Paragraph Summary

sqlite3_rsync is a low-key but practical remote backup tool in the SQLite ecosystem, added as an experimental feature in SQLite 3.47.0 and enhanced in 3.50.0. It solves the bottleneck of traditional backup methods (backup API or VACUUM INTO) that require local disk space for a full database copy by transferring the database snapshot directly to a remote machine over SSH. Its core is hash-based differential transfer built on SQLite's page structure: the replica side sends page hashes, and the origin returns only pages with different content, with bandwidth overhead increasing by at most a few percentage points when syncing an existing replica. Unlike regular rsync, it ensures consistent snapshot transfer, making it suitable for backing up running databases, especially in scenarios with tight disk space, remote backup needs, or minimal bandwidth requirements.

#sqlite3_rsync#SQLite remote backup#SQLite incremental sync#VACUUM INTO#SQLite backup API#SSH backup
Share:

Related articles