|
| 1 | +# Migrate Data Between Storage Backends |
| 2 | + |
| 3 | +<!-- diataxis: how-to --> |
| 4 | + |
| 5 | +This guide shows how to copy ZODB data from one storage backend to another |
| 6 | +using the generic [zodb-convert](https://pypi.org/project/zodb-convert/) tool. |
| 7 | + |
| 8 | +## Prerequisites |
| 9 | + |
| 10 | +- The `zodb-convert` package installed in your virtual environment |
| 11 | +- The storage-specific packages for both source and destination installed |
| 12 | + (e.g. `ZODB` for filestorage, `RelStorage` for relstorage, `zodb-pgjsonb` |
| 13 | + for pgjsonb) |
| 14 | + |
| 15 | +## Approach 1: Generated configuration files |
| 16 | + |
| 17 | +When your instance uses a non-filestorage backend (RelStorage, PGJsonb, or |
| 18 | +ZEO), the cookiecutter generates `convert-import.conf` and |
| 19 | +`convert-export.conf` files in the `etc/` directory. These are ready-made |
| 20 | +configuration files for `zodb-convert` that convert between your configured |
| 21 | +backend and a FileStorage. |
| 22 | + |
| 23 | +### Step 1: Set the conversion paths |
| 24 | + |
| 25 | +In your `instance.yaml`, provide the FileStorage paths for the "other side" |
| 26 | +of the conversion: |
| 27 | + |
| 28 | +```yaml |
| 29 | +default_context: |
| 30 | + db_storage: pgjsonb |
| 31 | + db_pgjsonb_dsn: "dbname='zodb' user='zodb' host='localhost' port='5433'" |
| 32 | + |
| 33 | + # Conversion settings (FileStorage side) |
| 34 | + db_convert_import_filestorage_location: var/import/Data.fs |
| 35 | + db_convert_import_blobs_location: var/import/blobs |
| 36 | + db_convert_export_filestorage_location: var/export/Data.fs |
| 37 | + db_convert_export_blobs_location: var/export/blobs |
| 38 | +``` |
| 39 | +
|
| 40 | +These settings work with all non-filestorage backends. |
| 41 | +
|
| 42 | +### Step 2: Import data from FileStorage |
| 43 | +
|
| 44 | +Place your `Data.fs` and blobs at the import locations, then run: |
| 45 | + |
| 46 | +```bash |
| 47 | +zodb-convert etc/convert-import.conf |
| 48 | +``` |
| 49 | + |
| 50 | +### Step 3: Export data to FileStorage |
| 51 | + |
| 52 | +To create a portable FileStorage backup: |
| 53 | + |
| 54 | +```bash |
| 55 | +zodb-convert etc/convert-export.conf |
| 56 | +``` |
| 57 | + |
| 58 | +## Approach 2: Using existing zope.conf files |
| 59 | + |
| 60 | +If you have two Zope instances with different storage backends, `zodb-convert` |
| 61 | +can read storage configuration directly from their `zope.conf` files. No |
| 62 | +extra configuration files are needed. |
| 63 | + |
| 64 | +### Step 1: Generate the destination instance |
| 65 | + |
| 66 | +Create a second instance configured for the new storage backend: |
| 67 | + |
| 68 | +```yaml |
| 69 | +# instance-new.yaml |
| 70 | +default_context: |
| 71 | + db_storage: pgjsonb |
| 72 | + db_pgjsonb_dsn: "dbname='zodb' user='zodb' host='localhost' port='5433'" |
| 73 | +``` |
| 74 | + |
| 75 | +Run cookiecutter to generate: |
| 76 | + |
| 77 | +```bash |
| 78 | +cookiecutter -f --no-input --config-file instance-new.yaml \ |
| 79 | + gh:plone/cookiecutter-zope-instance |
| 80 | +``` |
| 81 | + |
| 82 | +### Step 2: Run the conversion |
| 83 | + |
| 84 | +```bash |
| 85 | +zodb-convert \ |
| 86 | + --source-zope-conf /old-instance/etc/zope.conf \ |
| 87 | + --dest-zope-conf /new-instance/etc/zope.conf |
| 88 | +``` |
| 89 | + |
| 90 | +### Specifying a named database |
| 91 | + |
| 92 | +If your `zope.conf` defines multiple `<zodb_db>` sections: |
| 93 | + |
| 94 | +```bash |
| 95 | +zodb-convert \ |
| 96 | + --source-zope-conf old/etc/zope.conf --source-db main \ |
| 97 | + --dest-zope-conf new/etc/zope.conf --dest-db main |
| 98 | +``` |
| 99 | + |
| 100 | +The default database name is `main`. |
| 101 | + |
| 102 | +## Useful options |
| 103 | + |
| 104 | +- `--dry-run` -- preview what would be copied without writing data |
| 105 | +- `--incremental` -- resume from the last transaction in the destination |
| 106 | + (useful for large databases or after interruptions) |
| 107 | +- `-v` -- show INFO-level progress; `-vv` for DEBUG-level detail |
| 108 | + |
| 109 | +## Common migration scenarios |
| 110 | + |
| 111 | +| From | To | Notes | |
| 112 | +|---|---|---| |
| 113 | +| direct (filestorage) | relstorage | Classic scale-out migration | |
| 114 | +| direct (filestorage) | pgjsonb | Move to SQL-queryable storage | |
| 115 | +| relstorage | pgjsonb | Switch to JSONB representation | |
| 116 | +| zeo | relstorage | Remove ZEO server dependency | |
| 117 | +| any | direct (filestorage) | Create a portable backup | |
| 118 | + |
| 119 | +All combinations work -- `zodb-convert` is storage-agnostic. |
| 120 | + |
| 121 | +## Configuration reference |
| 122 | + |
| 123 | +| Setting | Default | Description | |
| 124 | +|---|---|---| |
| 125 | +| `db_convert_import_filestorage_location` | *(unset)* | Path to the source FileStorage for import | |
| 126 | +| `db_convert_import_blobs_location` | *(unset)* | Path to the source blob directory for import | |
| 127 | +| `db_convert_export_filestorage_location` | *(unset)* | Path for the destination FileStorage on export | |
| 128 | +| `db_convert_export_blobs_location` | *(unset)* | Path for the destination blob directory on export | |
| 129 | + |
| 130 | +The `convert-import.conf` file is generated when both `db_convert_import_*` |
| 131 | +settings are provided. The `convert-export.conf` file is generated when both |
| 132 | +`db_convert_export_*` settings are provided. Neither file is generated for |
| 133 | +the `direct` (filestorage) backend since it *is* the portable format. |
| 134 | + |
| 135 | +## Next steps |
| 136 | + |
| 137 | +- [zodb-convert on PyPI](https://pypi.org/project/zodb-convert/) -- Full |
| 138 | + documentation and source code |
| 139 | +- {doc}`/explanation/storage-backends` -- Understanding the trade-offs |
| 140 | + between backends |
0 commit comments