Skip to content

Commit c47d278

Browse files
Merge pull request #65 from hschne/25-06-readme-and-generator-updates
Readme and generator updates
2 parents 1547ff8 + 9417c79 commit c47d278

File tree

2 files changed

+53
-20
lines changed

2 files changed

+53
-20
lines changed

README.md

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,24 +80,27 @@ LITESTREAM_INSTALL_DIR=.bin
8080

8181
You configure the Litestream executable through the [`config/litestream.yml` file](https://litestream.io/reference/config/), which is a standard Litestream configuration file as if Litestream was running in a traditional installation.
8282

83-
The gem streamlines the configuration process by providing a default configuration file for you. This configuration file will backup all SQLite databases defined in your `config/database.yml` file to one replication bucket. In order to ensure that no secrets are stored in plain-text in your repository, this configuration file leverages Litestream's support for environment variables. The default configuration file looks like this if you only have one SQLite database:
83+
The gem streamlines the configuration process by providing a default configuration file for you. This configuration file will backup all SQLite databases defined in your `config/database.yml` file to one replication bucket. In order to ensure that no secrets are stored in plain-text in your repository, this configuration file leverages Litestream's support for environment variables. Inspect which environment variables are available by running the `bin/rails litestream:env` command.
84+
85+
The default configuration file looks like this if you only have one SQLite database:
8486

8587
```yaml
8688
dbs:
8789
- path: storage/production.sqlite3
8890
replicas:
8991
- type: s3
90-
bucket: $LITESTREAM_REPLICA_BUCKET
9192
path: storage/production.sqlite3
93+
bucket: $LITESTREAM_REPLICA_BUCKET
9294
access-key-id: $LITESTREAM_ACCESS_KEY_ID
9395
secret-access-key: $LITESTREAM_SECRET_ACCESS_KEY
9496
```
9597
9698
This is the default for Amazon S3. The full range of possible replica types (e.g. other S3-compatible object storage servers) are covered in Litestream's [replica guides](https://litestream.io/guides/#replica-guides).
9799
98-
The gem also provides a default initializer file at `config/initializers/litestream.rb` that allows you to configure these three environment variables referenced in the configuration file in Ruby. By providing a Ruby interface to these environment variables, you can use any method of storing secrets that you prefer. For example, the default generated file uses Rails' encrypted credentials to store your secrets:
100+
The gem also provides a default initializer file at `config/initializers/litestream.rb` that allows you to configure various variables referenced in the configuration file in Ruby. By providing a Ruby interface to these environment variables, you can use your preferred method of storing secrets. For example, the default generated file uses Rails' encrypted credentials to store your secrets.
99101

100102
```ruby
103+
# config/initializers/litestream.rb
101104
Rails.application.configure do
102105
litestream_credentials = Rails.application.credentials.litestream
103106
@@ -107,7 +110,21 @@ Rails.application.configure do
107110
end
108111
```
109112

110-
However, if you need manual control over the Litestream configuration, you can manually edit the `config/litestream.yml` file. The full range of possible configurations are covered in Litestream's [configuration reference](https://litestream.io/reference/config/). NB: If you configure a longer `sync-interval`, you may need to adjust `replication_sleep` when calling `Litestream.verify!`.
113+
Outside of configuring Litestream's replication, you may also configure various other aspects of `litestream-ruby` itself.
114+
115+
```ruby
116+
# config/initializers/litestream.rb
117+
Rails.application.configure do
118+
# ...
119+
120+
# Base controller used for Litestream dashboard
121+
config.litestream.base_controller_class = "MyApplicationController"
122+
# Set the location of the Litestream config
123+
config.litestream.config_path = "config/litestream.yml"
124+
end
125+
```
126+
127+
However, if you need manual control over the Litestream configuration, you can edit the `config/litestream.yml` file. The full range of possible configurations are covered in Litestream's [configuration reference](https://litestream.io/reference/config/).
111128

112129
### Replication
113130

@@ -232,6 +249,7 @@ You can verify the integrity of your backed-up databases using the gem's provide
232249

233250
```ruby
234251
Litestream.verify! "storage/production.sqlite3"
252+
Litestream.verify!(replication_sleep: 10) "storage/production.sqlite3"
235253
```
236254

237255
In order to verify that the backup for that database is both restorable and fresh, the method will add a new row to that database under the `_litestream_verification` table, which it will create if needed. It will then wait `replication_sleep` seconds (defaults to 10) to give the Litestream utility time to replicate that change to whatever storage providers you have configured. After that, it will download the latest backup from that storage provider and ensure that this verification row is present in the backup. If the verification row is _not_ present, the method will raise a `Litestream::VerificationFailure` exception. This check ensures that the restored database file:
@@ -242,6 +260,9 @@ In order to verify that the backup for that database is both restorable and fres
242260

243261
After restoring the backup, the `Litestream.verify!` method will delete the restored database file. If you need the restored database file, use the `litestream:restore` rake task or `Litestream::Commands.restore` method instead.
244262

263+
> [!NOTE]
264+
> If you configure Litestream's [`sync-interval`](https://litestream.io/reference/config/#replica-settings) to be longer than the default `replication_sleep` value of 10 seconds, you will need to adjust `replication_sleep` to a value larger than `sync-interval`; otherwise, `Litestream.verify!` may appear to fail where it actually simply didn't wait long enough for replication.
265+
245266
### Dashboard
246267

247268
The gem provides a web dashboard for monitoring the status of your Litestream replication. To mount the dashboard in your Rails application, add the following to your `config/routes.rb` file:

lib/litestream/generators/litestream/templates/initializer.rb

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,41 @@
55
# or some other mechanism where the values are only available at runtime.
66

77
Rails.application.configure do
8-
# An example of using Rails encrypted credentials to configure Litestream.
8+
# Configure Litestream through environment variables. Use Rails encrypted credentials for secrets.
99
# litestream_credentials = Rails.application.credentials.litestream
1010

11-
# Replica-specific bucket location.
12-
# This will be your bucket's URL without the `https://` prefix.
11+
# Replica-specific bucket location. This will be your bucket's URL without the `https://` prefix.
1312
# For example, if you used DigitalOcean Spaces, your bucket URL could look like:
13+
#
1414
# https://myapp.fra1.digitaloceanspaces.com
15+
#
1516
# And so you should set your `replica_bucket` to:
17+
#
1618
# myapp.fra1.digitaloceanspaces.com
17-
# Litestream supports Azure Blog Storage, Backblaze B2, DigitalOcean Spaces,
18-
# Scaleway Object Storage, Google Cloud Storage, Linode Object Storage, and
19-
# any SFTP server.
20-
# In this example, we are using Rails encrypted credentials to store the URL to
21-
# our storage provider bucket.
19+
#
2220
# config.litestream.replica_bucket = litestream_credentials&.replica_bucket
23-
24-
# Replica-specific authentication key.
25-
# Litestream needs authentication credentials to access your storage provider bucket.
26-
# In this example, we are using Rails encrypted credentials to store the access key ID.
21+
#
22+
# Replica-specific authentication key. Litestream needs authentication credentials to access your storage provider bucket.
2723
# config.litestream.replica_key_id = litestream_credentials&.replica_key_id
28-
29-
# Replica-specific secret key.
30-
# Litestream needs authentication credentials to access your storage provider bucket.
31-
# In this example, we are using Rails encrypted credentials to store the secret access key.
24+
#
25+
# Replica-specific secret key. Litestream needs authentication credentials to access your storage provider bucket.
3226
# config.litestream.replica_access_key = litestream_credentials&.replica_access_key
27+
#
28+
# Replica-specific region. Set the bucket’s region. Only used for AWS S3 & Backblaze B2.
29+
# config.litestream.replica_region = "us-east-1"
30+
#
31+
# Replica-specific endpoint. Set the endpoint URL of the S3-compatible service. Only required for non-AWS services.
32+
# config.litestream.replica_endpoint = "endpoint.your-objectstorage.com"
33+
34+
# Configure the default Litestream config path
35+
# config.config_path = Rails.root.join("config", "litestream.yml")
36+
37+
# Configure the Litestream dashboard
38+
#
39+
# Set the default base controller class
40+
# config.litestream.base_controller_class = "MyApplicationController"
41+
#
42+
# Set authentication credentials for Litestream dashboard
43+
# config.litestream.username = litestream_credentials&.username
44+
# config.litestream.password = litestream_credentials&.password
3345
end

0 commit comments

Comments
 (0)