DEV Community

Thibault Maekelbergh
Thibault Maekelbergh

Posted on

🐳 Migrating InfluxDB databases to docker-compose

This blogpost was originally publish on my blog

The problem

I got a brand new Intel NUC which I'm planning to use as a server with Docker. Some weeks earlier I had set up InfluxDB on a Raspberry Pi Zero W to collects my Home Assistant data and monitor my machines with Telegraf.

Incorrectly, I guessed that there would be an import/export module in Chronograf which I use to manage my InfluxDB database. The problem seems to be a bit more complicated though. The problem itself lies in the fact that the influxd service needs to be halted before the restoration of a database can happen and to replicate that scenario, you'd have to shut down the Docker container... and also making it useless to act upon.

Follow along for the quickest solution I've found using docker-compose and an intermediate container

Backing up

On the old host running InfluxDB (Rpi Zero) stop the service and export the databases. The most recent and recommend way of doing so is via the portable method:

# Ignore this if the service is already running of course.
$ sudo systemctl start influxdb
$ influxd backup -portable -database telegraf ./influxdb-backup/telegraf
$ influxd backup -portable -database home_assistant ./influxdb-backup/home_assistant
$ sudo systemctl stop influxdb

After the two databases were backed up, I copied those directories over with scp and then copied them back to NUC where I would run the Docker container

Restoring

Now I had my InfluxDB container already running to test the connection with Chronograf. Since I think the format is more readable, I use docker-compose to spin up these containers:

version: '2'

services:
  influxdb:
    image: influxdb:latest
    container_name: influxdb
    ports:
      - 8086:8086
    volumes:
      - /opt/appdata/influxdb:/var/lib/influxdb
    restart:
      always

The steps to restoring include first shutting down this container and then spinning up a throw-away container to copy the backup directories to the locally mounted volume /opt/appdata which will persist in the docker-compose created container.

First, stop our current container with docker-compose down (or docker stop influxdb)

Create intermediate container that maps the data volume and our backup directory and then enter the container's shell:

$ docker run --rm --detach -v /opt/appdata/influxdb:/var/lib/influxdb -v /home/thibmaek/influxdb-backup:/backups -p 8086 influxdb:latest
$ docker exec -it mystifying_kepler /bin/bash

Inside the intermediate container use the mapped backup folder to restore to our database and exit it afterwards:

$ influxd restore -portable -database telegraf /backups/telegraf
$ influxd restore -portable -database home_assistant /backups/home_assistant
$ exit

Kill and remove the intermediate container. Restart our previous docker-compose container and dbs should be restored:

$ docker stop mystifying_kepler
$ docker-compose up -d

Top comments (2)

Collapse
 
buzzkillb profile image
buzzkillb

Just did this restore and went very smooth. Thank you for posting the super easy method for this.

Collapse
 
myrison profile image
myrison

Cheers for taking the time to post this. Saved me a big headache migrating influx to Docker!