Backing up Netmaker with Litestream: Part 1
Overview
Litestream is a streaming SQLite replication solution. Netmaker is a virtual networking platform that runs over WireGuard. Bring the two together to have simple, easy backups of your Netmaker config.
Litestream
Litestream is a solution for those times where you want reliable uptime but don’t want to leave SQLite’s ease of setup and maintenance. As Litestream’s site says, by using it you get a:
Fully-replicated database with no pain and little cost.
Netmaker
By default, Netmaker uses SQLite. The default docker-compose has it set up as follows (in docker-compose.yml):
volumes:
- sqldata:/root/data
Netmaker with Litestream
Since Netmaker comes with SQLite you don’t have to set that up.
You do need to create a storage location for Litestream to use. Since we’re using Amazon S3 below, you’ll need to create an S3 bucket. Log into Amazon Web Services, go to S3 and create a new bucket (named “litestream-unique-name” below). You’ll also need to obtain your S3 secrets to access the bucket (they are unique to your account and are to be kept secret so are not reproduced below).
You will need to put the following lines in your docker-compose.yml (below the “services:” line and above the “netmaker:” indented as required by the docker-compose.yml format):
litestream:
container_name: litestream
image: litestream/litestream
volumes:
- sqldata:/root/data
- /root/litestream.yml:/etc/litestream.yml
command: 'replicate'
The above example assumes a fresh install that has root as the user (as you would have in a DigitalOcean droplet running Ubuntu 22.04).
You will also need a litestream.yml file for the above docker-compose.yml to use. The litestream.yml file (under /root as shown above if root is the user being used) needs to contain:
access-key-id: mys3accesskey
secret-access-key: mys3secretaccesskeydbs:
- path: /root/data/netmaker.db
replicas:
- url: s3://litestream-unique-name/
Where “mys3accesskey” is replaced by your actual S3 access key obtained from Amazon S3 (if using that storage approach; Litestream has more options available) and “mys3secretaccesskey” is replaced by your actual S3 secret access key.
Then run this at the shell prompt (command-line):
docker-compose down
followed by
docker-compose up -d
and check your Litestream logs with
docker logs litestream
If it’s working correctly you’ll see something similar to this (note that they main thing here is that there are lines below the “replicating to” line):
litestream v0.3.9
initialized db: /root/data/netmaker.db
replicating to: name="s3" type="s3" bucket="litestream-unique-name" path="" region="" endpoint="" sync-interval=1s
/root/data/netmaker.db: sync: new generation "4fb912e3c88632dc", no generation exists
/root/data/netmaker.db(s3): snapshot written 4fb912e3c88632dc/00000000
You can confirm that it’s working correctly by going to your S3 bucket on AWS and making sure that there is a “generations” folder present.
Conclusion
Litestream provides a simple and easy way to back up your SQLite databases, making it even easier to ensure that your Netmaker setup continues to run flawlessly. You can restore from Litestream to recover your SQLite database (netmaker.db in this case) if needed in case of a disaster.
On to Part 2: restoring a Netmaker database from a Litestream replica.