Skip to content

Automatically import new books (Linux)

James Armstrong edited this page Oct 14, 2024 · 7 revisions

Calibre web doesn't have an automatic library scan feature, so by default you have to modify the associated Calibre database yourself. However it's possible to instead setup a service which can monitor a directory for changes. After which it triggers a script to import new books. This removes the need to setup something like 'Calibre Content Server'.

Pre-requisites

Before even going through this, check if all you need is to import multiple books at once. You can do this by shift-clicking in the upload window when selecting your books to upload.

This example is based on using Readarr to download books, but you could easily adapt these steps to fit your own process.

  • systemd based distro (Debian, Ubuntu etc. Otherwise you'll have to change how you run the script)
  • calibre (You've likely installed this alongside Calibre web, otherwise you'll need to install it now so we can use it's executable commands)
  • inotify-tools (Used to watch for directory changes, run sudo apt install inotify-tools)

Folder structure

It's best to have separate directories for each step in the process, for example:

  1. Readarr downloads: /data/downloads/books (Books are first downloaded here)
  2. Readarr root library: /data/media/books (Once downloaded, books are hardlinked/copied here and this folder is monitored for changes)
  3. Calibre library: /data/media/calibre (New books are imported into Calibre's database metadata.db here)

DO NOT make your Readarr root library and Calibre library the same directory

When books are imported into the Calibre's database, an additional copy of the book is made inside the Calibre library directory. This is the copy that the Calibre database serves to Calibre web. What you do with the other copies is up to you. But if you're using hardlinks and only downloading relatively small eBooks (say 5-10mb), it's likely not going to be an impactful storage problem.

Optional folder

  1. Readarr misc library: /data/media/documents (If you download PDF files, or other non-standard eBooks, then I recommend you don't add them to your main library. As they don't normally have any associated metadata to tell the importer what book it's adding. And these books should still be imported manually)

Warning

This script below will only detect and scan subfolders if you copy (ie cp) or download the folders into the folder being monitored. It can't detect folders moved (ie mv) into the monitored folder. Files moved in will be detected correctly though.

See the top of this page for the Calibre-Web-Automated project for a full-fledged implementation

Create the import script

This script can be placed anywhere your system can access it nano /data/media/calibre/.scripts/calibre-scan.sh.

#!/bin/bash

# This script is used to automatically import downloaded eBook's into a Calibre database.
# Reference: https://manual.calibre-ebook.com/generated/en/calibredb.html#add
echo "STARTING CALIBRE SCANNER"

# Folder to monitor
WATCH_FOLDER="/data/media/books"
echo "Watching folder: $WATCH_FOLDER"

# Calibre library path
CALIBRE_LIBRARY="/data/media/calibre"
echo "Calibre library: $CALIBRE_LIBRARY"

# Function to add new eBook to Calibre database
add_to_calibre() {
    # Path to calibredb executable
    CALIBREDB="/usr/bin/calibredb"
    
    # Run calibredb command to add the new eBook to the database
    $CALIBREDB add -r $WATCH_FOLDER --library-path="$CALIBRE_LIBRARY"
    echo "Added $1 to Calibre database"
}

# Monitor the folder for new files
inotifywait -m -r --format="%e %w%f" -e close_write -e moved_to "$WATCH_FOLDER" |
while read -r events filepath; do
        echo "New file detected: $filepath"
        add_to_calibre "$filepath"
done

Change WATCH_FOLDER and CALIBRE_LIBRARY where required.

Create the service

  1. Create the systemd service sudo nano /etc/systemd/system/calibre-scan.service.
[Unit]
Description=Auto imports eBooks into calibre db

[Service]
Type=simple
ExecStart=/bin/bash /data/media/calibre/.scripts/calibre-scan.sh
Restart=always

[Install]
WantedBy=multi-user.target
  1. Enable the service sudo systemctl enable calibre-scan.service
  2. Start the service sudo systemctl start calibre-scan.service
  3. Check the status of the service or any errors sudo systemctl status calibre-scan.service

If everything is working correctly, any books added to the watched directory will now be imported into your Calibre database, appearing almost immediately in Calibre web!

Notes

Warnings

  • From my testing, I don't recommend specifying the detected filename in the calibredb command.
  • I don't recommend using incron. Whilst incron seemed perfect for this task, it became a nightmare to troubleshoot, whereas inotifywait has worked great.

Credit

@SirHazza for creating this guide and @scrapix who originally solved this issue with their original script.