Move InfluxDB data directory to a SDcard
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:
---
# 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" 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:
---
- name: restart influxdb
service:
name: influxdb
state: restarted
Comments
Display comments as Linear | Threaded