3.0 KiB
Restoring from a SQLite dump file
rqlite supports loading a node directly from two sources, either of which can be used to restore from an existing node backup:
- An actual SQLite database file. This is the fastest way to initialize a rqlite node from an existing SQLite database.
- SQLite dump file. This is another convenient manner to initialize a system from an existing SQLite database. But if your source database is large, it can be slow.
Examples
The following examples show a trivial database being generated by sqlite3
, the SQLite file being backed up, converted to the corresponding list of SQL commands, and then loaded into a rqlite node listening on localhost using each form.
HTTP
Be sure to set the Content-type header as shown in each case.
~ $ sqlite3 restore.sqlite
SQLite version 3.14.1 2016-08-11 18:53:32
Enter ".help" for usage hints.
sqlite> CREATE TABLE foo (id integer not null primary key, name text);
sqlite> INSERT INTO "foo" VALUES(1,'fiona');
sqlite>
# Load directly from the SQLite file.
~ $ curl -v -XPOST localhost:4001/db/load?fmt=binary -H "Content-type: application/octet-stream" --data-binary @restore.sqlite
# Convert SQLite database file to set of SQL commands and then load
~ $ echo '.dump' | sqlite3 restore.sqlite > restore.dump
~ $ curl -XPOST localhost:4001/db/load -H "Content-type: text/plain" --data-binary @restore.dump
After either command, we can connect to the node, and check that the data has been loaded correctly.
$ rqlite
127.0.0.1:4001> SELECT * FROM foo
+----+-------+
| id | name |
+----+-------+
| 1 | fiona |
+----+-------+
rqlite CLI
Note that the CLI currently only supports loading from a SQLite dump file.
~ $ sqlite3 restore.sqlite
SQLite version 3.22.0 2018-01-22 18:45:57
Enter ".help" for usage hints.
sqlite> CREATE TABLE foo (id integer not null primary key, name text);
sqlite> INSERT INTO "foo" VALUES(1,'fiona');
sqlite>
~ $ echo '.dump' | sqlite3 restore.sqlite > restore.dump # Convert SQLite database file to set of SQL commands
~ $ ./rqlite
Welcome to the rqlite CLI. Enter ".help" for usage hints.
127.0.0.1:4001> .schema
+-----+
| sql |
+-----+
127.0.0.1:4001> .restore restore.dump
last inserted ID: 1
rows affected: 1
database restored successfully
127.0.0.1:4001> select * from foo
+----+-------+
| id | name |
+----+-------+
| 1 | fiona |
+----+-------+
Caveats
The behavior of the restore operation when data already exists on the cluster is undefined -- you should only restore to a cluster that has no data, or a brand-new cluster. Also, please note that SQLite dump files normally contain a command to disable Foreign Key constraints. If you are running with Foreign Key Constraints enabled, and wish to re-enable this, this is the one time you should explicitly re-enable those constraints via the following curl
command:
curl -XPOST 'localhost:4001/db/execute?pretty' -H "Content-Type: application/json" -d '[
"PRAGMA foreign_keys = 1"
]'