29 May 2018

How to Backup Docker Swarm

As you may already know, backing up Swarm is absolutely essential. In this article, I'll be demonstrating how to perform a full backup of Swarm and how to restore it in case of any unfortunate incidents. But before we get into that, let me share some tips with you:

  • I'm assuming that you're using Ubuntu or a similar Linux distribution.
  • Swarm configurations are stored in the /var/lib/docker/swarm directory.
  • You can use any manager node to take the backup. For instance, if you have three managers, you can use any one of them to perform a full backup of Swarm.
  • It's advisable to avoid using the leader-manager node as much as possible and opt for another manager instead.
  • Lastly, I highly recommend scheduling regular backups.

Now, let's move on to the first step, which is to stop Docker:

sudo service docker stop

Create the backup by compressing the /var/lib/docker/swarm in a tarball:

tar -czvf swarm.backup.tar /var/lib/docker/swarm/

Our backup is ready now 🙂

In case of an unfortunate incident, and you need to restore the backup, you can easily unzip the tarball by following these steps:

service docker stop

rm -Rf /var/lib/docker/swarm

tar -zxvf swarm.backup.tar -C /var/lib/docker

That's not all! We need to initialize Swarm again, but don't worry, there's a magic flag that can help us with that:

docker swarm init --force-new-cluster