Backing up Netmaker with Litestream: Part 2

Cameron Tully-Smith
netmaker
Published in
2 min readOct 26, 2022

Prerequisites

Before the steps below you’ll need:

  • Netmaker set up (you’ll restore on the machine acting as your server/where Netmaker is installed)
  • A previous Litestream backup. For a sample of how these are created, see the previous article on backing up Netmaker using a sidecar.
  • A Litestream container configured on your Netmaker server. See the previous article for more information.

Installing Litestream

For this article we’ll use the Docker sidecar as used for backup and adapt it for restore. We’ll start with the litestream.yml used in the backup article.

You’ll then modify the docker-compose.yml to have it use the “restore” command. The updated litestream section is:

litestream:
container_name: litestream
image: litestream/litestream
volumes:
- sqldata:/root/data
- /root/litestream.yml:/etc/litestream.yml
command: 'restore -o /root/data/netmaker-restored.db /root/data/netmaker.db'

Restoring from a replica

Restore using the following steps:

  • Log into your Netmaker server (if it’s not your local machine) eg via SSH
  • Get the Netmaker container ID with
docker ps

Note the container ID for “gravitl/netmaker…”. Then run a shell in the container as follows:

docker exec -it container_id_here /bin/bash

where container_id_here is replaced with the container ID noted above.

When in the container shell:

ls /root/data

Output of ls should show “netmaker.db” and not show “netmaker-restored.db”. If “netmaker-restored.db” shows remove it with

rm /root/data/netmaker-restored.db

Exit the container shell with

exit
  • Restore using Litestream by restarting the containers to have the Litestream container
docker-compose down
docker-compose up -d
  • Confirm the restore was successful by running
docker logs litestream

If the restore was successful you’ll see no messages in the logs.

  • Go back to the container shell using docker ps then run “docker exec -it container_id_here /bin/bash” as above where container_id_here is the container ID obtained from docker ps.
  • When in the container shell:
cd /root/data
pwd # confirm shows /root/data before continuing
rm netmaker.*
mv netmaker-restored.db netmaker.db
chmod 644 netmaker.db
ls # confirm netmaker-restored.db is not listed and netmaker.db is listed
exit

Confirm Correct Operation

  • Open the Netmaker UI and confirm that the configuration is as expected
  • Confirm that Litestream is replicating again to the cloud (S3 in this case) by running
docker logs litestream
  • Change the docker-compose.yml back to “normal operation” (replicating the database so that it’s backed up from this point forward). The updated litestream section of docker-compose.yml is:
litestream:
container_name: litestream
image: litestream/litestream
volumes:
- sqldata:/root/data
- /root/litestream.yml:/etc/litestream.yml
command: 'replicate'
  • Start the non-running container (litestream) via
docker-compose up -d
  • run
docker ps

to confirm that the litestream container is running.

  • run
docker logs litestream

to confirm that the database is being replicated again (refer to the backup article for more details).

Conclusion

You have now gotten the database restored as you would in case of a system failure or transition to a new Netmaker server. Congratulations!

--

--

Cameron Tully-Smith
netmaker
Writer for

Cameron Tully-Smith is a Sr Software Engineer at Netmaker (https://netmaker.io), a cloud networking company building the next-gen virtual networking platform.