restore_influxdb.sh 1.79 KB
Newer Older
fwinkler's avatar
fwinkler committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
#!/bin/bash

################################################
# This script must not be called directly!     #
# Usage: ./pika_post_processing.sh restore     #
################################################

if [ $# -eq 0 ]
  then
    echo "No shard supplied"
    exit 1
fi

SHARD=$1
#SSD would be much faster!!!
mkdir -p /data/temp_export
TEMP_EXPORT=/data/temp_export/$SHARD

# check if shard directory exists
if [ ! -d /data/influx_lt_backup/$SHARD ]; then
  exit 1
fi

# restore shard into temporary database
influxd restore -portable -db "$INFLUXDB_DATABASE" -newdb "$SHARD" /data/influx_lt_backup/$SHARD/ &
sleep 1

echo "ALTER RETENTION POLICY frob ON \"$SHARD\" DURATION inf SHARD DURATION 7d" | /usr/bin/influx -username $INFLUXDB_USER -password $INFLUXDB_PASSWORD

wait
echo "Restore $SHARD finished!"

sleep 10

# dump shard into file
time influx_inspect export -datadir "/data/influxdb/data" -waldir "/data/influxdb/wal" -database "$SHARD" -out $TEMP_EXPORT

# modfiy metadata of dumped file ###################################################

# get first 7 lines
head -n 7 $TEMP_EXPORT > header

# modify metdadata and store in new file
sed -e "/CREATE DATABASE/c CREATE DATABASE prope_lt" -e "s/$SHARD/prope_lt/" -e '/CONTEXT-RETENTION-POLICY/d' header > ${TEMP_EXPORT}_mod
rm header

# piping tail from original file to new file
time tail -n +8 $TEMP_EXPORT >> ${TEMP_EXPORT}_mod

####################################################################################

# write data into lt database
time influx -username $INFLUXDB_USER -password $INFLUXDB_PASSWORD -import -path=${TEMP_EXPORT}_mod -precision=ns

# delete temporary database and export file
curl -POST "localhost:8086/query?u=$INFLUXDB_USER&p=$INFLUXDB_PASSWORD" --data-urlencode "q=drop database \"$SHARD\""
rm -f $TEMP_EXPORT
rm -f ${TEMP_EXPORT}_mod