Axione-IPE-Viewer/data-ingest/fetch_axione_ipe.sh
2024-05-09 01:58:10 +02:00

74 lines
2.3 KiB
Bash
Executable file

#!/usr/bin/env bash
### --------------------------------------
### Daily IPE files FTP synchro & ingest
### Sacha - 20220414
### Johan - 20220519 - fix appel ingest & ajout City fast
### -------------------------------------
set -Eeuo pipefail
# Note: les fichiers IPE arrivent parfois très tard, on a vu du 4h du matin le lendemain... Pour être un peu plus sûr d'avoir les données, il vaut mieux prendre l'avant-veille...
date=$(date +"%Y%m%d" -d "2 days ago")
# Script Configuration
localDir="/srv/ftth_ipe_data/tmp_ipe"
ingest="/srv/www/ftth-ipe-map/data-ingest/ingest"
db_target_path="/srv/ftth_ipe_data/axione_data/ipe.sqlite"
db_temp="/srv/ftth_ipe_data/axione_data/ipe_temp.sqlite"
webapp_svc="ftth-ipe-map.service"
axione_files_pattern="A-IPE*$date*.csv V-IPEZTD*CTYF*$date*.csv"
axione_ftp_user=
axione_ftp_pass=
# First clean csv files in temp dir and temp sqlite if exists
rm -f $localDir/*csv*
rm -f $db_temp
# FTP Sync
# Le fichier CTYF n'a pas d'entête mais s'il est ingéré après les autres ça passe, pas ouf mais bon
lftp -u "$axione_ftp_user","$axione_ftp_pass" ftp.axione.fr -e "mget $axione_files_pattern -O $localDir; exit" ||:
# Check if no files mail
echo "Check if found files"
files=$(shopt -s nullglob dotglob; echo $localDir/*)
if (( ! ${#files} ))
then
mail -s "gaia: FTP IPE Sync error" admin@aquilenet.fr <<< "Pas de fichiers IPE synchronisés"
fi
echo "Ingest files into temp DB $db_temp"
# Make sure db is empty
rm -f $db_temp
# Ingest into temporary DB
$ingest $localDir $db_temp
echo "Backup ori DB $db_target_path"
# Make copy of original DB before swap
cp $db_target_path $db_target_path.backup
echo "Swap DBs new and original"
# Swap DBs
mv -f $db_temp $db_target_path
#chown ftth-ipe-map:ftth-ipe-map $db_target_path
echo "Restart service $webapp_svc"
# Restart service
if sudo systemctl restart $webapp_svc; then
# Ok, delete copy of old DB
echo "OK, remove backup DB"
rm -f $db_target_path.backup
else
# Error, put back old DB
echo "Error, Put back old DB & restart"
mv -f $db_target_path.backup $db_target_path
#chown ftth-ipe-map:ftth-ipe-map $db_target_path
sudo systemctl restart $webapp_svc || mail -s "gaia: FTP IPE Sync error" admin@aquilenet.fr <<< "Le service $webapp_svc ne redémarre pas !"
fi
echo "Clean any downloaded csv files"
# Delete CSV files
rm -rf $localDir/*csv*