Move InfluxDB data directory to a SDcard

Posted by ads' corner on Sunday, 2020-06-21
Posted in [Ansible][Influxdb][Linux][Other-Dbs][Raspberry-Pi][Software]

For my openHAB system I installed InfluxDB (on a separate) Raspberry Pi. The Pi has a 32 GB SDcard, but that is not enough for storing all the data, and that Pi has additional work to do as well. For that reason I also attached a 1 TB disk to the Pi, and mounted it on /data. Now all I have to do is move the InfluxDB data directory to /data.

The InfluxDB configuration file inĀ /etc/influxdb/influxdb.conf has three different settings for the storage directory:

  • meta: in [meta]->dir
  • data: in [data]->dir
  • WAL in [data]->wal-dir

By default the data lives in /var/lib/influxdb, but that is on the regular SDcard, which I don’t want to wear out. I integrated everything into an Ansible Playbook which is deploying the Pi. The relevant parts for InfluxDB:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
---

# install InfluxDB packages
- name: InfluxDB packages
  apt:
    name:
      - influxdb
      - influxdb-client
      - apt-transport-https
      - python3-influxdb
    state: present

- name: Enable and start InfluxDB service
  service:
    name: influxdb
    state: started
    enabled: yes
  register: start_influxdb

- name: Pause to start InfluxDB service
  shell: /bin/sleep 5
  when: start_influxdb.changed

# Check if the InfluxDB directory is still on the original location
- name: Retrieve /etc/influxdb/influxdb.conf
  slurp:
    src: "/etc/influxdb/influxdb.conf"
  register: etc_influxdb_influxdb_conf_retrieve

- name: Extract /etc/influxdb/influxdb.conf
  set_fact:
    etc_influxdb_influxdb_conf: "{{ etc_influxdb_influxdb_conf_retrieve.content | b64decode }}"

# move the InfluxDB directory to the attached disk
- block:

  - name: Check that /data is a mounted device
    command: mountpoint -q "/data"
    register: data_stat
    failed_when: False
    changed_when: False

  - fail:
      msg: "/data is not a mountpoint!"
    when: data_stat.rc != 0

  - name: Create new InfluxDB directory
    file:
      path: "{{ item.path }}"
      owner: influxdb
      group: influxdb
      mode: "{{ item.mode }}"
      state: directory
    loop:
      - { path: "/data/influxdb", mode: "0755" }

  - name: Stop InfluxDB service
    service:
      name: influxdb
      state: stopped

  - name: Find out if the data directory is already moved
    stat:
      path: "/var/lib/influxdb/data"
    register: var_lib_influxdb_data

  - name: Move InfluxDB directory
    shell: mv /var/lib/influxdb/* "/data/influxdb/"
    when: var_lib_influxdb_data.stat.exists == True

  - name: Replace paths in influxdb.conf
    ini_file:
      path: "/etc/influxdb/influxdb.conf"
      section: "{{ item.section }}"
      option: "{{ item.option }}"
      value: "{{ item.value }}"
      state: "{{ item.state }}"
    loop:
      - { section: "meta", option: "dir", value: '"/data/influxdb/meta"', state: present }
      - { section: "data", option: "dir", value: '"/data/influxdb/data"', state: present }
      - { section: "data", option: "wal-dir", value: '"/data/influxdb/wal"', state: present }
    notify:
      - restart influxdb

  - name: Start InfluxDB service
    service:
      name: influxdb
      state: started

  when: etc_influxdb_influxdb_conf.find('/var/lib/influxdb') != -1
  rescue:
    - fail:
        msg: 'Something went wrong moving the InfluxDB directory - check the installation!'

- meta: flush_handlers

The Playbook first reads the config file /etc/influxdb/influxdb.conf, and the entire [block](https://docs.ansible.com/ansible/latest/user_guide/playbooks_blocks.html) is only run when there is a /var/lib/influxdb path in it.

When that happens the service is first stopped, the new directory in /data/influxdb is created. Then the content of the data directory is moved - if it’s still there: it’s possible that the data directory was already moved, but the Playbook did not finish all Tasks in a previous run. Afterwards the config file is updated and every path is changed from /var/lib/influxdb to /data/influxdb. Finally the service is started again.

Finally a handler is needed for restarting InfluxDB:

1
2
3
4
5
6
---

- name: restart influxdb
  service:
    name: influxdb
    state: restarted