Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Backup and restore influxdb inside Docker
#!/bin/bash
set -e
# Default working directory to current directory, but allow override via WORKDIR environment variable.
WORKDIR=${WORKDIR:-$PWD}
NOW="$(date +%s)"
INFLUXDIR="$WORKDIR/influxdb-$NOW"
BACKUPDIR="$WORKDIR/backup-$NOW"
# Start the first container to run the main influxd process.
CONTAINER_ID=$(docker run --rm \
--detach \
-v "$INFLUXDIR":/var/lib/influxdb \
-v "$BACKUPDIR":/backups \
-p 8086 \
influxdb:1.3
)
# Get the ephemeral port of the container.
PORT=$(docker port "$CONTAINER_ID" 8086 | cut -d: -f2)
# Delay until influxd is responsive.
until curl -s "http://localhost:${PORT}/ping"; do
sleep 0.1
done
# Put some data into database "foo".
curl -XPOST "http://localhost:${PORT}/query?q=CREATE+DATABASE+foo"
for n in $(seq 25); do
echo "ctr n=${n}i $n"
done | curl -XPOST "http://localhost:${PORT}/write?db=foo&precision=h" --data-binary @-
echo
echo "Databases before backup and restore:"
curl "http://localhost:${PORT}/query?q=SHOW+DATABASES"
# Create the backup.
docker exec "$CONTAINER_ID" influxd backup -database foo "/backups/foo.backup"
# Drop the foo database, so we have something to restore.
curl -XPOST "http://localhost:${PORT}/query?q=DROP+DATABASE+foo"
echo
echo "Databases after backup and after dropping, but before restore:"
curl "http://localhost:${PORT}/query?q=SHOW+DATABASES"
# Restoring a backup requires that influxd is stopped (note that stopping the process kills the container).
docker stop "$CONTAINER_ID"
# Run the restore command in an ephemeral container.
# This affects the previously mounted volume mapped to /var/lib/influxdb.
docker run --rm \
--entrypoint /bin/bash \
-v "$INFLUXDIR":/var/lib/influxdb \
-v "$BACKUPDIR":/backups \
influxdb:1.3 \
-c "influxd restore -metadir /var/lib/influxdb/meta -datadir /var/lib/influxdb/data -database foo /backups/foo.backup"
# Start the container just like before, and get the new container ID.
CONTAINER_ID=$(docker run --rm \
--detach \
-v "$INFLUXDIR":/var/lib/influxdb \
-v "$BACKUPDIR":/backups \
-p 8086 \
influxdb:1.3
)
PORT=$(docker port "$CONTAINER_ID" 8086 | cut -d: -f2)
until curl -s "http://localhost:${PORT}/ping"; do
sleep 0.1
done
echo
echo "Databases after backup and restore:"
curl "http://localhost:${PORT}/query?q=SHOW+DATABASES"
docker stop "$CONTAINER_ID"
@mark-rushakoff

This comment has been minimized.

Copy link
Owner Author

commented Jul 16, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.