Commit 3925dc62 authored by fwinkler's avatar fwinkler

Added scripts for post processing.

parent f6f6bfc6
#!/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"
echo -e "Backup shard ${shard_id} of ${shard_arr[1]} (${shard_arr[2]}): about $shard_duration hours ($start_seconds s -> $end_seconds s)" |2>&1 tee -a $DEBUG_PATH
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
echo -e "Backup of shard ${shard_id} into ${backup_dir} successful!" |2>&1 tee -a $DEBUG_PATH
else
success=0
echo -e "Backup of shard ${shard_id} into ${backup_dir} failed!" |2>&1 tee -a $DEBUG_PATH
if [ -d "${backup_dir}" ]; then
echo -e "Backup directory ${backup_dir} exists." |2>&1 tee -a $DEBUG_PATH
fi
if [ "$(ls -A ${backup_dir})" ]; then
echo -e "Backup directory ${backup_dir} is not empty." |2>&1 tee -a $DEBUG_PATH
else
echo -e "Backup directory ${backup_dir} is empty: ls -A ${backup_dir} = $(ls -A ${backup_dir})" |2>&1 tee -a $DEBUG_PATH
fi
if [ $(ls -l ${backup_dir} | grep -c pending) -eq 0 ]; then
echo -e "No filename contains pending." |2>&1 tee -a $DEBUG_PATH
fi
if [ $(ls -l ${backup_dir} | grep -c tar.gz) -gt 0 ]; then
echo -e "An archive tar.gz exists" |2>&1 tee -a $DEBUG_PATH
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
echo -e "Shard $shard_id is already in backup ($backup_avail)." |2>&1 tee -a $DEBUG_PATH
fi
else
echo -e "Shard $shard_id is incomplete" |2>&1 tee -a $DEBUG_PATH
fi
done <<< "$(echo "$shards")"
#!/bin/bash
BACKUP_PATH=/data/mariadb_backup
date_time=`date +"%Y-%m-%d_%H-%M-%S"`
BACKUP_FILE="${BACKUP_PATH}/backup-${date_time}.sql"
echo "mysqldump prope > $BACKUP_FILE"
mysqldump prope > $BACKUP_FILE
#!/bin/bash
source /sw/taurus/tools/pika/pika-current.conf
DEBUG_PATH=/tmp/pika_post_proccesing.out
LOCK_POST_PROCESSING_SCRIPT=/tmp/pika_post_proccesing.lock
# get utility functions
source ${PIKA_ROOT}/pika_utils.sh
function create_lock()
{
# lock script
if [ -f ${LOCK_POST_PROCESSING_SCRIPT} ]; then
echo -e "Post processing is already running..." |2>&1 tee -a $DEBUG_PATH
echo -e "exit 0" |2>&1 tee -a $DEBUG_PATH
exit 0
else
touch ${LOCK_POST_PROCESSING_SCRIPT}
echo -e "Start post processing..." |2>&1 tee $DEBUG_PATH
fi
}
function release_lock()
{
if [ -f ${LOCK_POST_PROCESSING_SCRIPT} ]; then
rm -f ${LOCK_POST_PROCESSING_SCRIPT}
echo -e "Post processing finished." |2>&1 tee -a $DEBUG_PATH
fi
}
function backup_influx()
{
echo "Backup influxdb..." |2>&1 tee -a $DEBUG_PATH
source ${PIKA_ROOT}/post_processing/backup_influxdb.sh |2>&1 tee -a $DEBUG_PATH
}
function restore_influx()
{
echo "Restore influxdb..." |2>&1 tee -a $DEBUG_PATH
# get all shards which are not restored yet
restore_shards=`mysql_command "select shard_id, start from Backup where success=1 and restored=0"`
echo ${restore_shards} |2>&1 tee -a $DEBUG_PATH
# convert string output into indexed array
IFS=', ' read -r -a shard_array <<< "${restore_shards}"
len=${#shard_array[@]}
if [ ${len} -lt 2 ]; then
echo "Cannot find shards for restoring." |2>&1 tee -a $DEBUG_PATH
return 0
fi
len=$(( ${len} - 2 ))
i=0
while [ $i -le ${len} ]
do
shard_id=${shard_array[i]}
start_seconds=${shard_array[i+1]}
#source ${PIKA_ROOT}/post_processing/restore_influxdb.sh ${shard_id}_${start_seconds} |2>&1 tee -a $DEBUG_PATH
echo "Test $i"
if [ $? -eq 0 ]; then
mysql_command "UPDATE Backup SET restored=1 WHERE shard_id=${shard_id} AND start=${start_seconds};"
fi
i=$(( $i + 2 ))
done
}
function revise_metadata()
{
echo "Revise metadata..." |2>&1 tee -a $DEBUG_PATH
}
function create_footprints()
{
echo "Create footprints..." |2>&1 tee -a $DEBUG_PATH
}
function create_tags()
{
echo "Create tags..." |2>&1 tee -a $DEBUG_PATH
}
if [ ! $# -eq 1 ]; then
echo "One argument required!"
echo "./pika_post_processing [all|backup|restore|revise|footprint|tag]"
else
case "$1" in
all)
create_lock
backup_influx
restore_influx
release_lock
;;
backup)
create_lock
backup_influx
release_lock
;;
restore)
create_lock
restore_influx
release_lock
;;
*)
echo "$1 is not supported."
;;
esac
fi
\ No newline at end of file
#!/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
#!/bin/bash
if [ $# -eq 0 ]
then
echo "No backup supplied"
exit
fi
BACKUP_PATH=/data/mariadb_backup
BACKUP_FILE="${BACKUP_PATH}/$1"
echo "mysql prope < $BACKUP_FILE"
mysql prope < $BACKUP_FILE
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment