Skip to content

Commit

Permalink
#139 renewed backup/restore influxdb containers
Browse files Browse the repository at this point in the history
  • Loading branch information
justb4 committed Aug 22, 2018
1 parent 0a538e7 commit 2314f74
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 13 deletions.
19 changes: 16 additions & 3 deletions services/influxdb-dc1/restore.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ then
exit 1
fi

# Parameter ok
./stop.sh

SCRIPT_DIR=${0%/*}

pushd ${SCRIPT_DIR}
Expand Down Expand Up @@ -65,4 +62,20 @@ popd


# On RUNNING container named influxdb
INFLUX_CMD="docker exec ${SE_CONTAINER_NAME} influx -host 127.0.0.1 -port 8086 -database ${INFLUXDB_DB} -username ${INFLUXDB_ADMIN_USER} -password ${INFLUXDB_ADMIN_PASSWORD} -execute "

# First must delete database otherwise meta restore errors
echo "DROP DATABASE ${INFLUXDB_DB}..."
${INFLUX_CMD} "DROP DATABASE ${INFLUXDB_DB}"

echo "Restore ${INFLUXDB_DB} from ${CONTAINER_BACKUP_DIR} ..."
docker exec ${SE_CONTAINER_NAME} influxd restore -portable -db ${INFLUXDB_DB} -newdb ${INFLUXDB_DB} /backup/${INFLUXDB_DB}

# Permissions are somehow not set, users should still exist
echo "Restore permissions for users ${INFLUXDB_READ_USER} and ${INFLUXDB_WRITE_USER} ..."
${INFLUX_CMD} "GRANT READ ON ${INFLUXDB_DB} TO ${INFLUXDB_READ_USER}"
${INFLUX_CMD} "GRANT ALL ON ${INFLUXDB_DB} TO ${INFLUXDB_WRITE_USER}"

# Cleanup
echo "Removing unpacked backup in ${BACKUP_DIR} ..."
rm -rf ${BACKUP_DIR}
17 changes: 10 additions & 7 deletions services/influxdb-dc1/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function query() {
QUERY="$2"
echo "db=$DB - query: ${QUERY}"
# See https://docs.influxdata.com/influxdb/v1.4/guides/querying_data/
curl -G 'http://dc1.smartemission.nl:8086/query?pretty=true' \
curl -G 'http://localhost:8086/query?pretty=true' \
-u ${INFLUXDB_READ_USER}:${INFLUXDB_READ_USER_PASSWORD} \
--data-urlencode "db=${DB}" --data-urlencode \
"q=${QUERY}"
Expand All @@ -23,14 +23,17 @@ function query() {
#query _internal "SHOW DATABASES"
#query _internal "SHOW USERS"

query ${INFLUXDB_DB} "SELECT * from ASE_NL_01 limit 2"
query ${INFLUXDB_DB} "SELECT * from ASE_NL_02 limit 2"
query ${INFLUXDB_DB} "SELECT * from ASE_NL_03 limit 2"
query ${INFLUXDB_DB} "SELECT * from ASE_NL_04 limit 2"
query ${INFLUXDB_DB} "SELECT * from ASE_NL_05 limit 2"
# On RUNNING container named influxdb
INFLUX_CMD="docker exec ${SE_CONTAINER_NAME} influx -host 127.0.0.1 -port 8086 -database ${INFLUXDB_DB} -username ${INFLUXDB_READ_USER} -password ${INFLUXDB_READ_USER_PASSWORD} -execute "

${INFLUX_CMD} "SELECT * from ASE_NL_01 limit 2"
${INFLUX_CMD} "SELECT * from ASE_NL_02 limit 2"
${INFLUX_CMD} "SELECT * from ASE_NL_03 limit 2"
${INFLUX_CMD} "SELECT * from ASE_NL_04 limit 2"
${INFLUX_CMD} "SELECT * from ASE_NL_05 limit 2"

# Get the last N records
query ${INFLUXDB_DB} "SELECT * from ASE_NL_04 WHERE time > now() - 1h ORDER BY time DESC LIMIT 21"
${INFLUX_CMD} "SELECT * from ASE_NL_04 WHERE time > now() - 1h ORDER BY time DESC LIMIT 21"

# examples
# http://test.smartemission.nl:8086/query?db=smartemission&q=SELECT%20*%20from%20rivm%20limit%202
Expand Down
25 changes: 25 additions & 0 deletions services/influxdb/init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/bash
# See https://www.influxdata.com/blog/backuprestore-of-influxdb-fromto-docker-containers/
# NB need to stop monitoring as influxdb will not stop because of cAdvisor lock!!!

SCRIPT_DIR=${0%/*}

pushd ${SCRIPT_DIR}
if [ ! -f influxdb.env ]
then
echo "Bestand influxdb.env niet gevonden."
exit 1
fi
source influxdb.env
popd

# On RUNNING container named influxdb
INFLUX_CMD="docker exec ${SE_CONTAINER_NAME} influx -host 127.0.0.1 -port 8086 -database ${INFLUXDB_DB} -username ${INFLUXDB_ADMIN_USER} -password ${INFLUXDB_ADMIN_PASSWORD} -execute "

echo "DROP DATABASE ${INFLUXDB_DB}..."
${INFLUX_CMD} "DROP DATABASE ${INFLUXDB_DB}"

# Permissions are somehow not set, users should still exist
echo "Restore permissions for users ${INFLUXDB_READ_USER} and ${INFLUXDB_WRITE_USER} ..."
${INFLUX_CMD} "GRANT READ ON ${INFLUXDB_DB} TO ${INFLUXDB_READ_USER}"
${INFLUX_CMD} "GRANT ALL ON ${INFLUXDB_DB} TO ${INFLUXDB_WRITE_USER}"
18 changes: 15 additions & 3 deletions services/influxdb/restore.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ pushd ${SCRIPT_DIR}
exit 1
fi
source influxdb.env

# Parameter ok
./stop.sh
popd

# Make sure vars are set
Expand Down Expand Up @@ -66,4 +63,19 @@ popd


# On RUNNING container named influxdb
INFLUX_CMD="docker exec ${SE_CONTAINER_NAME} influx -host 127.0.0.1 -port 8086 -database ${INFLUXDB_DB} -username ${INFLUXDB_ADMIN_USER} -password ${INFLUXDB_ADMIN_PASSWORD} -execute "
# First must delete database otherwise meta restore errors
echo "DROP DATABASE ${INFLUXDB_DB}..."
${INFLUX_CMD} "DROP DATABASE ${INFLUXDB_DB}"

echo "Restore ${INFLUXDB_DB} from ${CONTAINER_BACKUP_DIR} ..."
docker exec ${SE_CONTAINER_NAME} influxd restore -portable -db ${INFLUXDB_DB} -newdb ${INFLUXDB_DB} /backup/${INFLUXDB_DB}

# Permissions are somehow not set, users should still exist
echo "Restore permissions for users ${INFLUXDB_READ_USER} and ${INFLUXDB_WRITE_USER} ..."
${INFLUX_CMD} "GRANT READ ON ${INFLUXDB_DB} TO ${INFLUXDB_READ_USER}"
${INFLUX_CMD} "GRANT ALL ON ${INFLUXDB_DB} TO ${INFLUXDB_WRITE_USER}"

# Cleanup
echo "Removing unpacked backup in ${BACKUP_DIR} ..."
rm -rf ${BACKUP_DIR}

0 comments on commit 2314f74

Please sign in to comment.