Note: This was implemented for InfluxDB Version 1.6. I have since upgraded to InfluxDB Version 2.x and a different method is required for this version.
It makes sense to periodically backup InfluxDB to an external drive in-case of corruption of onboard memory. I am using a USB memory stick.
A simple cronjob can take care of this (every night 2am), open Crontab:
sudo crontab -e
and insert the below line: (change for your storage device)
0 2 * * * influxd backup -portable /media/usb/drive
Backup names start with the date it was generated but it can get messy after a few weeks so long term its better to run a backup script to put backups in individual directories and catch errors etc., create a python file for this and use the below example, update crontab -e instead to:
sudo crontab -e
0 2 * * * python /home/pi/influx_scripts/influx_backup.py
Create our python backup file:
nano /home/pi/influx_scripts/influx_backup.py
import os from datetime import date today = date.today() d1 = today.strftime("%Y_%m_%d") print("Date", d1) command = "mkdir /media/usb-backup/" + d1 #print(command) os.system(command) command = "influxd backup -portable /media/usb-backup/" + d1 os.system(command) command = "kapacitor backup /media/usb-backup/"+d1+"/kapacitor.db" os.system(command) command = "sudo find /media/usb-backup/* -mtime +7 -type d -exec rm -rf {} \;" os.system(command) os.system("echo Backups Done!")
You can keep an eye on the USB memory stick size by the below snip of script which can be logged to InfluxDB. An Influx alert keeps an eye on the size and alerts if getting close to capacity.
The above already deletes all files over 7 Days old.
DIRECTORY="/media/usb/drive" if [ -d "$DIRECTORY" ]; then usb_mem_usage=$(du -s $DIRECTORY | awk 'NR==1{print $1}') else usb_mem_usage="-1" fi echo $usb_mem_usage
All done!
Resources I used:
– https://stackoverflow.com/questions/31389483/find-and-delete-file-or-folder-older-than-x-days