Skip to content

Commit ad5ca79

Browse files
committed
Update initializer and readme
1 parent 1547ff8 commit ad5ca79

File tree

2 files changed

+65
-17
lines changed

2 files changed

+65
-17
lines changed

README.md

Lines changed: 19 additions & 5 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 run `litestream:env`.
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,17 @@ 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+
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/). Outside of configuring Litestream, 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+
# Base controller used for Litestream dashboard
119+
config.litestream.base_controller_class = "MyApplicationController"
120+
# Set the location of the Litestream config
121+
config.config_path = "config/litestream.yml"
122+
end
123+
```
111124

112125
### Replication
113126

@@ -232,9 +245,10 @@ You can verify the integrity of your backed-up databases using the gem's provide
232245

233246
```ruby
234247
Litestream.verify! "storage/production.sqlite3"
248+
Litestream.verify!(replication_sleep: 10) "storage/production.sqlite3"
235249
```
236250

237-
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:
251+
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. Note that if you configure a longer `sync-interval`, you may need to adjust `replication_sleep` to a longer period. 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:
238252

239253
1. exists,
240254
2. can be opened by SQLite, and

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

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,63 @@
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.
9+
#
910
# litestream_credentials = Rails.application.credentials.litestream
1011

1112
# Replica-specific bucket location.
13+
#
1214
# This will be your bucket's URL without the `https://` prefix.
1315
# For example, if you used DigitalOcean Spaces, your bucket URL could look like:
16+
#
1417
# https://myapp.fra1.digitaloceanspaces.com
18+
#
1519
# And so you should set your `replica_bucket` to:
20+
#
1621
# 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.
22+
#
2223
# config.litestream.replica_bucket = litestream_credentials&.replica_bucket
23-
24-
# Replica-specific authentication key.
24+
#
25+
#
26+
# Replica-specific authentication key
27+
#
2528
# 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.
29+
#
2730
# config.litestream.replica_key_id = litestream_credentials&.replica_key_id
28-
29-
# Replica-specific secret key.
31+
#
32+
#
33+
# Replica-specific secret key
34+
#
3035
# 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.
36+
#
3237
# config.litestream.replica_access_key = litestream_credentials&.replica_access_key
38+
#
39+
#
40+
# Replica-specific region
41+
#
42+
# Optionally specifies the bucket’s region. Only used for AWS S3 & Backblaze B2.
43+
#
44+
# config.litestream.replica_region = "us-east-1"
45+
#
46+
#
47+
# Replica-specific endpoint
48+
#
49+
# Optionally specifies the endpoint URL of the S3-compatible service. Only required for non-AWS services.
50+
#
51+
# config.litestream.replica_endpoint = "endpoint.your-objectstorage.com"
52+
53+
# Configure the default Litestream config path
54+
#
55+
# config.config_path = Rails.root.join("config", "litestream.yml")
56+
57+
# Configure the Litestream dashboard
58+
#
59+
# Set the default base controller class
60+
#
61+
# config.litestream.base_controller_class = "MyApplicationController"
62+
#
63+
# Set authentication credentials for Litestream dashboard
64+
#
65+
# config.litestream.username = Rails.application.credentials.dig(:litestream, :username)
66+
# config.litestream.password = Rails.application.credentials.dig(:litestream, :password)
3367
end

0 commit comments

Comments
 (0)