Skip to content

Instantly share code, notes, and snippets.

@itsananderson
Last active May 9, 2025 23:41
Show Gist options
  • Save itsananderson/bc567e1490a088873ea12ac26456696c to your computer and use it in GitHub Desktop.
Save itsananderson/bc567e1490a088873ea12ac26456696c to your computer and use it in GitHub Desktop.
Script to create a sqlite3 binary that can recover Calibre databases

This script produces a sqlite3 binary with the necessary extensions to recover a Calibre metadata.db database. This seems to have worked for me, but I am not affiliated with the Calibre project, and it's also very possible that this will break your Calibre database in some subtle way. YMMV

First run calibre-sqlite.sh to produce a sqlite3 binary:

chmod +x calibre-sqlite.sh
./calibre-sqlite.sh

Now you can use the CLI to attempt to recover your DB

./sqlite3 /path/to/library/metadata.db ".recover" | ./sqlite3 recovered.db

# Check new DB for corruption
./sqlite3 recovered.db "pragma integrity_check;"

# Backup your existing database
mv /path/to/library/metadata.db /path/to/library/metadata.backup.db

# Move the recovered database into place
mv recovered.db /path/to/library/metadata.db
#!/bin/bash
# Make sure needed compilation deps are available
sudo apt-get install build-essential libreadline-dev wget
wget -c 'https://sqlite.org/src/tarball/SQLite-trunk.tgz?uuid=trunk' -O SQLite-trunk.tgz
tar -xzf SQLite-trunk.tgz
cd SQLite-trunk/
# Configure with FTS5 enabled (not sure the --fts5 flag is actually necessary since we also pass SQLITE_ENABLE_FTS5)
./configure --fts5
# Compile the CLI binary
gcc -Os -I. \
-DSQLITE_THREADSAFE=0 -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_FTS5 -DSQLITE_ENABLE_JSON1 \
-DSQLITE_ENABLE_RTREE -DSQLITE_ENABLE_EXPLAIN_COMMENTS \
-DHAVE_READLINE -DSQLITE_ENABLE_DBPAGE_VTAB -DSQLITE_SHELL_HAVE_RECOVER \
shell.c sqlite3.c -ldl -lm -lreadline -lncurses -o sqlite3
./sqlite3 --version
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment