Storage Migration Guide¶
Overview¶
Readur stores uploaded documents on the local filesystem (UPLOAD_PATH) by default, or in an S3-compatible bucket when S3 storage is enabled. Documents uploaded while local storage was active keep a local file path in the database. The migrate_to_s3 tool copies those files into the S3 bucket and points the database records at the new S3 objects.
The tool only migrates local to S3. There is no tool for moving documents from S3 back to local storage or between buckets.
The full option reference is in Command-Line Tools. This page describes the procedure.
When You Need This¶
- You started Readur with local storage and want to switch to S3 storage without losing existing documents.
What the Tool Does¶
For every selected document whose stored file path does not start with s3://, the tool:
- Reads the file from the path stored in the database. If it does not exist, the document counts as failed.
- Uploads it to the bucket under
documents/<user-id>/<year>/<month>/<document-id>.<extension>(year and month of the migration run; files larger than the multipart threshold use a multipart upload). - Changes the document's file path in the database to
s3://<key>. - Uploads the thumbnail
<UPLOAD_PATH>/thumbnails/<document-id>_thumb.jpgtothumbnails/<user-id>/<document-id>_thumb.jpgand the processed image<UPLOAD_PATH>/processed_images/<document-id>_processed.pngtoprocessed_images/<user-id>/<document-id>_processed.png, if those files exist. A failure here is logged as a warning and does not fail the document. - With
--delete-local, deletes the local original, thumbnail and processed image after they were uploaded.
Documents are processed one at a time, oldest first.
Which documents are selected¶
Every document whose file path does not start with s3:// (with --user-id ID, only that user's), oldest first. The tool reads them in pages of 500, so any number of documents is covered in one run. With --limit N it stops after N documents.
Documents already in S3 are never selected, so a later run migrates only what is still local (documents added since, and earlier failures), and a run after everything has moved finds nothing to do.
Prerequisites¶
- A source checkout of Readur and a Rust toolchain. The tool is not included in the Docker image, which contains only the
readurserver binary. - The same environment as the server: database settings (
DATABASE_URLor thePOSTGRES_*variables) andUPLOAD_PATH. - S3 settings:
S3_ENABLED=true(orSTORAGE_BACKEND=s3),S3_BUCKET_NAME,S3_ACCESS_KEY_ID,S3_SECRET_ACCESS_KEY, and as neededS3_REGION(defaultus-east-1),S3_ENDPOINT_URLandS3_FORCE_PATH_STYLEfor S3-compatible services. IfS3_PREFIXis set, migrated documents, thumbnails and processed images are written below that prefix, just like new uploads. See the configuration reference. - Read access to the document files at the paths stored in the database. In the Docker setup these are absolute paths under
/app/uploads, so the tool must run where the uploads directory is available at that same path. Relative paths are resolved against the tool's working directory.
Before you start¶
- Back up the database:
- Back up the uploads directory:
Step-by-Step Migration Process¶
Step 1: Build the tool¶
The binary is target/release/migrate_to_s3. Alternatively, cargo run --release --bin migrate_to_s3 -- <options> builds and runs it in one step.
Step 2: Set the environment¶
export DATABASE_URL="postgresql://readur:readur@localhost:5432/readur"
export UPLOAD_PATH="/app/uploads"
export S3_ENABLED=true
export S3_BUCKET_NAME="readur-documents"
export S3_ACCESS_KEY_ID="your-access-key"
export S3_SECRET_ACCESS_KEY="your-secret-key"
export S3_REGION="us-east-1"
# Only for S3-compatible services such as MinIO:
export S3_ENDPOINT_URL="https://s3.example.com"
If S3_ENABLED is not true (and STORAGE_BACKEND is not s3), the tool logs S3 is not enabled in configuration. Set S3_ENABLED=true and provide S3 configuration. and exits with status 1. If the bucket cannot be listed, it logs S3 connection failed and exits with status 1.
Step 3: Dry run¶
A dry run connects to the database and to S3 (and fails the same way if either is unreachable), then lists the documents it would migrate, one log line each, and changes nothing:
INFO migrate_to_s3: 📋 Found 2 documents with local file paths
INFO migrate_to_s3: 🔍 DRY RUN - Would migrate the following files:
INFO migrate_to_s3: - invoice-2024.pdf (User: 3f6c1e2a-8d4b-4c1e-9a77-2b1f0c9d5e10, Size: 184320 bytes)
INFO migrate_to_s3: - scan.png (User: 3f6c1e2a-8d4b-4c1e-9a77-2b1f0c9d5e10, Size: 92011 bytes)
INFO migrate_to_s3: 💡 Run without --dry-run to perform actual migration
Log lines also carry a timestamp. The log level of this tool is fixed at info; RUST_LOG does not change it.
Step 4: Run the migration¶
The tool logs one 📦 Migrating: <filename> (<document-id>) [n/total] line per document followed by a success or failure line, and finishes with 🎉 Migration completed! and the number of migrated and failed files. Without --enable-rollback, a failed document is recorded and the tool continues with the next one; the run still exits with status 0.
Keep the local files (do not pass --delete-local) until you have checked the result.
Step 5: Check the result¶
- Read
migration_state.jsonin the directory you ran the tool from (see State file). Itsfailed_migrationslist names every document that was not migrated and why. - Count documents still stored locally:
- Run the tool again: documents already in S3 are skipped, so a second run only retries what is left.
Step 6: Switch the server to S3¶
Give the server the same S3 settings, for example in docker-compose.yml:
services:
readur:
environment:
S3_ENABLED: "true"
S3_BUCKET_NAME: readur-documents
S3_ACCESS_KEY_ID: your-access-key
S3_SECRET_ACCESS_KEY: your-secret-key
S3_REGION: us-east-1
Restart Readur. Documents uploaded by the still-running server while the migration ran are stored locally; run the tool once more after the switch to move them.
The tool only deletes local files of documents it migrates in a run started with --delete-local; a later run skips documents already in S3 and does not delete their local copies. Remove those yourself once you are satisfied (keep the .readur directory under UPLOAD_PATH, which holds Readur's own files such as the generated initial admin password).
Advanced Usage¶
State file¶
The tool writes migration_state.json to its working directory after every 10 successfully migrated documents and at the end of the run. It contains:
started_atcompleted_migrations: per documentdocument_id,user_id,original_path,s3_key,migrated_at, andassociated_files(each withfile_typethumbnailorprocessed_image,original_path,s3_key)failed_migrations: per documentdocument_id,original_path,error,failed_attotal_files,processed_files
The file is a record for you; the tool never reads it back. Each run overwrites it.
Rollback on failure¶
With --enable-rollback, the first failed document stops the run. The tool then undoes every migration completed in this run, newest first: it restores the original file path in the database and deletes the uploaded document, thumbnail and processed image objects from S3. It exits with status 1 (Migration failed and was rolled back).
- Errors during the rollback are written to
rollback_errors.jsonin the working directory. - If the rollback itself fails, the state is saved to
migration_state.jsonfor manual recovery. - Local files removed by
--delete-localare not restored. Do not combine--delete-localwith--enable-rollbackunless you have a backup of the uploads directory. - There is no command to roll back an earlier, finished run.
Resuming¶
Because documents already in S3 are skipped, rerunning the tool after an interruption continues with the remaining documents. --resume-from <document-id> additionally skips every document older than that one (it starts at that document); an ID that does not exist is an error.
Migrating one user¶
Find the user ID:
Then migrate that user's documents:
Security Considerations¶
- Pass credentials through environment variables, not on the command line.
migration_state.jsonandrollback_errors.jsoncontain document IDs, user IDs and file paths; delete them when you no longer need them.- Configure bucket policies and server-side encryption on the S3 side as your requirements demand; the tool does not set them.
Support¶
If you encounter issues during migration:
- Check the troubleshooting guide.
- Read the tool's log output and the
errorfields inmigration_state.json. - Test the migration against a copy of the database and a test bucket first when possible.