backup_influxdb.sh 3.98 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 59 60
#!/bin/bash

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

backup_path="/data/influx_lt_backup"
mkdir -p ${backup_path}

#date_now --rfc-3339=seconds | sed 's/ /T/'
date_now=`date +%Y-%m-%d\ %H:%M:%S`
seconds_now=`date --date="$date_now" +%s`

MARIADB_HOST=localhost

source_host=${INFLUXDB_HOST}
database=${INFLUXDB_DATABASE}

influx_version=1.7.7
if [ `command -v influx` ]; then
  influx_ver=`influx --version`
  influx_version=${influx_ver##* }
fi

# output has the following columns
# id database retention_policy shard_group start_time end_time expiry_time owners
shards=`echo "show shards" | influx -host $source_host -port 8086 -username ${INFLUXDB_USER} -password ${INFLUXDB_PASSWORD} | grep "\s$database\s"`

while IFS= read -r line ; do
  # read line into array
  read -ra shard_arr <<<"$line"

  # get array len
  len=${#shard_arr[@]}

  if [ $len -lt 7 ]; then
    continue
  fi

  #echo "Shard ID: ${shard_arr[0]}" #${shard_arr[3]}
  #echo "Database: ${shard_arr[1]}"
  #echo "Retentiob policy (name): ${shard_arr[2]}"
  #echo "Start time: ${shard_arr[4]}"
  #echo "End time: ${shard_arr[5]}"

  #replace 'T' with space and convert to seconds since epoche
  start_seconds=`date --date="$(echo ${shard_arr[4]} | sed 's/T/ /')" +%s`
  end_seconds=`date --date="$(echo ${shard_arr[5]} | sed 's/T/ /')" +%s`
  shard_id=${shard_arr[0]}
  shard_duration=$((($end_seconds - $start_seconds)/3600))
  #echo "shard $shard_id of ${shard_arr[1]} (${shard_arr[2]}): about $shard_duration hours ($start_seconds s -> $end_seconds s)"

  if [ $end_seconds -lt $seconds_now ]; then
    #echo "Shard $shard_id is ready for backup"
    backup_avail=`mysql_command "SELECT count(shard_id) FROM Backup WHERE shard_id=$shard_id AND start=$start_seconds LIMIT 1"`
    
    # check if 
    if [ "x$backup_avail" != "x"  ] && [ $backup_avail -eq 0 ]; then
      #echo "Shard ${shard_id} is not in backup"
61
      echo -e "Backup shard ${shard_id} of ${shard_arr[1]} (${shard_arr[2]}): about $shard_duration hours ($start_seconds s -> $end_seconds s)"
fwinkler's avatar
fwinkler committed
62 63 64 65 66 67 68 69 70 71 72

      backup_dir="${backup_path}/${shard_id}_${start_seconds}"
      
      influxd backup -portable -host $source_host:8088 -database $database -shard $shard_id -rp "backup" ${backup_dir}

      # extra wait
      sleep 180
      
      #if directory exists, directory is not empty, no file name contains "pending", and a "tar.gz" file exists, then add backup to backup table
      if [ -d "${backup_dir}" ] && [ "$(ls -A ${backup_dir})" ] && [ $(ls -l ${backup_dir} | grep -c pending) -eq 0 ] && [ $(ls -l ${backup_dir} | grep -c tar.gz) -gt 0 ]; then
        success=1
73
        echo -e "Backup of shard ${shard_id} into ${backup_dir} successful!"
fwinkler's avatar
fwinkler committed
74 75
      else
        success=0
76
        echo -e "Backup of shard ${shard_id} into ${backup_dir} failed!"
fwinkler's avatar
fwinkler committed
77 78

        if [ -d "${backup_dir}" ]; then
79
           echo -e "Backup directory ${backup_dir} exists."
fwinkler's avatar
fwinkler committed
80 81 82
        fi

        if [ "$(ls -A ${backup_dir})" ]; then
83
           echo -e "Backup directory ${backup_dir} is not empty."
fwinkler's avatar
fwinkler committed
84
        else
85
           echo -e "Backup directory ${backup_dir} is empty: ls -A ${backup_dir} = $(ls -A ${backup_dir})"
fwinkler's avatar
fwinkler committed
86 87 88
        fi

        if [ $(ls -l ${backup_dir} | grep -c pending) -eq 0 ]; then
89
           echo -e "No filename contains pending."
fwinkler's avatar
fwinkler committed
90 91 92
        fi

        if [ $(ls -l ${backup_dir} | grep -c tar.gz) -gt 0 ]; then
93
           echo -e "An archive tar.gz exists"
fwinkler's avatar
fwinkler committed
94 95 96 97 98 99 100 101 102
        fi

        [[ ${backup_dir} = *influx* ]] && [[ ${backup_dir} = *backup* ]] && rm -rf ${backup_dir}
      fi

      query="INSERT INTO Backup (shard_id,start,end,success,restored,influx_version,from_server) VALUES (${shard_id}, ${start_seconds}, ${end_seconds},${success},0,\"${influx_version}\",\"${source_host}\")"
      mysql_command "${query}"

    else
103
      echo -e "Shard $shard_id is already in backup ($backup_avail)."
fwinkler's avatar
fwinkler committed
104 105
    fi
  else
106
    echo -e "Shard $shard_id is incomplete"
fwinkler's avatar
fwinkler committed
107 108 109 110
  fi

done <<< "$(echo "$shards")"