|
| 1 | +# Data migration |
| 2 | + |
| 3 | +This document describes the process of migrating `lnd`'s database state from one |
| 4 | +type of database backend (for example the `bbolt` based database files `*.db` |
| 5 | +such as the `channel.db` or `wallet.db` files) to another (for example the new |
| 6 | +remote database backends such as `etcd` or `postgres` introduced in |
| 7 | +`lnd v0.14.0-beta`). |
| 8 | + |
| 9 | +## Prepare the destination database |
| 10 | + |
| 11 | +### Using etcd as the destination remote database |
| 12 | + |
| 13 | +When using `etcd` as the main remote database, some specific environment |
| 14 | +variables need to be set to ensure smooth operations both during the data |
| 15 | +migration and also later for running `lnd` in production. |
| 16 | + |
| 17 | +Make sure you are running **at least `etcd v3.5.0` or later** with the following |
| 18 | +environment variables (or their command line flag counterparts) set: |
| 19 | + |
| 20 | +```shell |
| 21 | +# Allow lnd to batch up to 16k operations into a single transaction with a max |
| 22 | +# total TX size of 104MB. |
| 23 | +ETCD_MAX_TXN_OPS=16384 |
| 24 | +ETCD_MAX_REQUEST_BYTES=104857600 |
| 25 | + |
| 26 | +# Keep 10k revisions for raft consensus in clustered mode. |
| 27 | +ETCD_AUTO_COMPACTION_RETENTION=10000 |
| 28 | +ETCD_AUTO_COMPACTION_MODE=revision |
| 29 | + |
| 30 | +# Allow the total database size to be up to 15GB. Adjust this to your needs! |
| 31 | +ETCD_QUOTA_BACKEND_BYTES=16106127360 |
| 32 | +``` |
| 33 | + |
| 34 | +Make sure you set the `ETCD_QUOTA_BACKEND_BYTES` to something that is |
| 35 | +sufficiently larger than your current size of the `channel.db` file! |
| 36 | + |
| 37 | +### Using postgres as the destination remote database |
| 38 | + |
| 39 | +Prepare a user and database as described in the [Postgres](postgres.md) |
| 40 | +documentation. You'll need the Data Source Name (DSN) for both the data |
| 41 | +migration and then the `lnd` configuration, so keep that string somewhere |
| 42 | +(should be something with the format of `postgres://xx:yy@localhost:5432/zz`). |
| 43 | + |
| 44 | +No additional steps are required to prepare the Postgres database for the data |
| 45 | +migration. The migration tool will create the database schema automatically, so |
| 46 | +no DDL scripts need to be run in advance. |
| 47 | + |
| 48 | +## Prepare the source database |
| 49 | + |
| 50 | +Assuming we want to migrate the database state from the pre-0.16.0 individual |
| 51 | +`bbolt` based `*.db` files to a remote database, we first need to make sure the |
| 52 | +source files are in the correct state. |
| 53 | + |
| 54 | +The following steps should be performed *before* running the data migration: |
| 55 | +1. Stop `lnd` |
| 56 | +2. Upgrade the `lnd` binary to the latest version (e.g. `v0.16.0-beta` or later) |
| 57 | +3. Make sure to add config options like `gc-canceled-invoices-on-startup=true` |
| 58 | + and `db.bolt.auto-compact=true` to your `lnd.conf` to optimize the source |
| 59 | + database size by removing canceled invoices and compacting it on startup. |
| 60 | +4. Start `lnd` normally, using the flags mentioned above but not yet changing |
| 61 | + any database backend related configuration options. Check the log that the |
| 62 | + database schema was migrated successfully, for example: `Checking for |
| 63 | + schema update: latest_version=XX, db_version=XX` |
| 64 | +5. Remove any data from the source database that you can. The fewer entries are |
| 65 | + in the source database, the quicker the migration will complete. For example |
| 66 | + failed payments (or their failed HTLC attempts) can be removed with |
| 67 | + `lncli deletepayments`. |
| 68 | +6. Stop `lnd` again and make sure it isn't started again by accident during the |
| 69 | + data migration (e.g. disable any `systemd` or other scripts that start/stop |
| 70 | + `lnd`). |
| 71 | + |
| 72 | +NOTE: If you were using the experimental `etcd` cluster mode that was introduced |
| 73 | +in `lnd v0.13.0` it is highly recommended starting a fresh node. While the data |
| 74 | +can in theory be migrated from the partial/mixed `etcd` and `bbolt` based state |
| 75 | +the migration tool does not support it. |
| 76 | + |
| 77 | +## Run the migration |
| 78 | + |
| 79 | +Depending on the destination database type, run the migration with a command |
| 80 | +similar to one of the following examples: |
| 81 | + |
| 82 | +**Example: Migrate from `bbolt` to `etcd`:** |
| 83 | + |
| 84 | +```shell |
| 85 | +⛰ lndinit -v migrate-db \ |
| 86 | + --source.bolt.data-dir /home/myuser/.lnd/data \ |
| 87 | + --source.bolt.tower-dir /home/myuser/.lnd/watchtower \ |
| 88 | + --dest.backend etcd \ |
| 89 | + --dest.etcd.host=my-etcd-cluster-address:2379 |
| 90 | +``` |
| 91 | +If you weren't running a watchtower server, you can remove the line with |
| 92 | +`--source.bolt.tower-dir`. |
| 93 | + |
| 94 | +**Example: Migrate from `bbolt` to `postgres`:** |
| 95 | + |
| 96 | +```shell |
| 97 | +⛰ lndinit -v migrate-db \ |
| 98 | + --source.bolt.data-dir /home/myuser/.lnd/data \ |
| 99 | + --source.bolt.tower-dir /home/myuser/.lnd/data/watchtower \ |
| 100 | + --dest.backend postgres \ |
| 101 | + --dest.postgres.dsn=postgres://postgres:postgres@localhost:5432/postgres |
| 102 | +``` |
| 103 | + |
| 104 | +If you weren't running a watchtower server, you can remove the line with |
| 105 | +`--source.bolt.tower-dir`. |
0 commit comments