Skip to content

Instantly share code, notes, and snippets.

@thorian93
Last active October 21, 2023 09:59
Show Gist options
  • Save thorian93/69f19306c9cad44ce29711a2bc497dbc to your computer and use it in GitHub Desktop.
Save thorian93/69f19306c9cad44ce29711a2bc497dbc to your computer and use it in GitHub Desktop.
InfluxDB Backup Script
#!/bin/bash
#
# Written by: Robin Gierse - info@thorian93.de - on 20191016
#
# Purpose:
# This script backs up influx databases
#
# Version: 0.1 on 20191016
# Version: 0.2 on 20191017
# Version: 0.3 on 20191022 - Add cleanup routine and switch compression to bz2
# Version: 0.4 on 20191029 - Repair cleanup routine
# Version: 1.0 on 20191107 - Change logfile to work with logrotate. Also bump version to 1.0 as the script works as intended.
#
# Usage:
# ./backup-influxdb.sh
# Variables:
influxdb_logfile="/var/log/backup/$(date +%Y%m%d_%H%M%S)_influx_backup.log"
influxdb_databases='_internal'
influxdb_host='localhost'
influxdb_backup_root="/backup/influxdb"
influxdb_backup_dest="${influxdb_backup_root}/$(date +%Y%m%d)/"
influxdb_backup_retention="7" # Delete backups older than number of days
# Functions:
_initialize() {
echo "$(date): Starting InfluxDB Backup."
touch "${influxdb_logfile}"
}
_influxdb_backup() {
for influxdb_database in ${influxdb_databases}
do
echo "$(date) Backing up ${influxdb_database}:"
influxd backup -portable -database "${influxdb_database}" -host "${influxdb_host}:8088" "${influxdb_backup_dest}/${influxdb_database}"
echo "$(date) Compressing Backup.."
tar -caf "${influxdb_backup_dest}/${influxdb_database}.tar.bz2" "${influxdb_backup_dest}/${influxdb_database}"
echo "$(date) Cleanup raw backup files.."
rm -rf "${influxdb_backup_dest:?}/${influxdb_database}"
echo "$(date) ${influxdb_database} done."
done
}
_influxdb_cleanup() {
echo 'Deleting old backups:'
rm -rvf $(find ${influxdb_backup_root} -mtime +${influxdb_backup_retention})
echo "$(date) Cleanup done."
}
_finalize() {
echo "$(date): Finished InfluxDB Backup."
exit 0
}
# Main
_initialize >> "${influxdb_logfile}" 2>&1
_influxdb_backup >> "${influxdb_logfile}" 2>&1
_influxdb_cleanup >> "${influxdb_logfile}" 2>&1
_finalize >> "${influxdb_logfile}" 2>&1
@AndersV209
Copy link

hello

any chance you have an update for this script that works on influxdb 2.1 ?

@thorian93
Copy link
Author

I am still on 1.8, and I am not yet planning to upgrade. Might update this, when I do, but feel free to try yourself! I mean you just need to update the influx commands, probably. The other logic should be fine.

@KinDR007
Copy link

KinDR007 commented Mar 18, 2022

Hello, any restore script? for this backup script ?

@snow8261
Copy link

very nice script

@thorian93
Copy link
Author

@KinDR007 thanks for your patience. But I am sorry to say, there is no such script. You have to look up how to restore InfluxDB manually. But really is not hard to do. A restore script really makes no sense, because you need to understand the underlying mechanics before performing a restore.

@snow8261 thanks, I appreciate that!

@jowidotps1
Copy link

jowidotps1 commented Jul 17, 2023

@AndersV209 here is the essence working in 2.7.1


#!/bin/bash
influxdb_logfile="/var/log/influxdb_backup.log"
influxdb_backup_root="/backup/"
influxdb_backup_retention="7" # Delete backups older than number of days
influxdb_token="operator token"

# Functions:

_timestamp(){
	date +%F_%T
} 

_initialize() {
	[ -d $influxdb_backup_root ] || mkdir $influxdb_backup_root
	[ -f $influxdb_logfile ] || touch ${influxdb_logfile}
}

_influxdb_backup() {
	echo "$(_timestamp) Starting InfluxDB Backup."
	influx backup $influxdb_backup_root -t $influxdb_token
	echo "$(_timestamp) Backup done."
}

_influxdb_cleanup() {
    echo "$(_timestamp) Deleting old backups."
    rm -rvf $(find ${influxdb_backup_root} -mtime +${influxdb_backup_retention})
    echo "$(_timestamp) Cleanup done."
}

_finalize() {
    echo "$(_timestamp) Finished InfluxDB Backup."
	echo "#################################################################"
    exit 0
}

# Main
_initialize >> "${influxdb_logfile}" 2>&1
_influxdb_backup >> "${influxdb_logfile}" 2>&1
_influxdb_cleanup >> "${influxdb_logfile}" 2>&1
_finalize >> "${influxdb_logfile}" 2>&1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment