Oui j’avais oublié ![]()
J’ai écris un code BaSH pour dé-zipper les fichiers DMARC dans le répertoire du mois ![]()
Cà permet de ne pas oubliez de fichiers non dézipper - Au cas où l’Humain que je suis en aurais oublié ![]()
Le script est le suivant : dmarc-file-dzip.sh
#!/bin/bash
#-------------------------------------------------------------
# Script pour dezipper les fichiers DMARC
# Author : O.Romain.Jaillet-ramey (orj (at) lab3w (dot) fr)
# Date Create : 20220527
#-------------------------------------------------------------
DIR="/var/pro/web_sites/zw3b_site/www/dmarc/"
YEAR="2022"
MONTH="06"
FOLDER="${DIR}/${YEAR}/${MONTH}"
FILE_OK=`/bin/ls ${FOLDER} | awk "/(gz|zip)/"`
NB_FILES=`/bin/ls ${FOLDER} | wc -l`
echo '+-----------------------------------------------'
echo NB FILES : ${NB_FILES} IN FOLDER
echo '+-----------------------------------------------'
echo ""
c=0;
for i in `ls ${FOLDER} | awk "/(gz|zip)/"`;
do
EXT=`echo ${i} | awk '{n=split($0,tab,"."); print tab[n];}'`
FILE_DZIPED="`echo ${i} | sed -E 's/\.(zip|gz)//g'`"
# echo "Extension : $EXT"
# file ${FOLDER}/${i};
if [ ${EXT} = 'zip' ];
then
# echo 'File Type : ZIP';
if [ `echo ${FILE_DZIPED} | grep "xml"` ];
then
FILE_DZIPED="${FILE_DZIPED}"
else
FILE_DZIPED="${FILE_DZIPED}.xml"
fi
fi
if [ ${EXT} = 'gz' ];
then
# echo 'File type : GZ';
FILE_DZIPED="${FILE_DZIPED}"
fi
# echo '+-----------------------------------------------'
# echo ""
# echo "FILE : "${i};
# echo "TYPE DZIPED : " ${FILE_DZIPED};
# echo ""
# echo '+--------------'
if [ ! -f "${FOLDER}/${FILE_DZIPED}" ] && [ -f "${FOLDER}/$i" ];
then
c=$(($c+1))
echo '+-----------------------------------------------'
echo -n "\ $c";
echo " - "${i};
echo " \ "
echo -n ' +-- Unzip file '
if [ ${EXT} = 'zip' ];
then
echo ' type : ZIP';
echo ""
unzip "${FOLDER}/${i}" -d ${FOLDER}
fi
if [ ${EXT} = 'gz' ];
then
echo ' type : GZ';
echo ""
gzip -dk "${FOLDER}/${i}"
fi
# echo '+-----------------------------------------------'
echo ""
fi
done;
echo '+-----------------------------------------------'
echo NB FILES : ${c} PROCESSED
echo '+-----------------------------------------------'
echo ""
Lancez la commande comme cela : sh dmarc-file-dzip.sh
On peut mieux faire hein ![]()
NdMoi-même : Ajout du 20220624 - Optimisation du script par @PengouinPdt.
time dmarc-file-dzip.sh
| real | 0m0,026s |
|---|---|
| user | 0m0,029s |
| sys | 0m0,003s |
Donc, gentiment, @PengouinPdt a optimisé le script sous le nom extract-dmarc-file.bash ![]()
#!/bin/bash
#-------------------------------------------------------------
# Author : O.Romain.Jaillet-ramey (orj (at) lab3w (dot) fr)
# Contributor : Stephane Huc (PengouinPdt)
# Desc : Script pour dezipper les fichiers DMARC
# ATTENTION à la variable $DIR, elle est juste si ton script shell
# est bien à la racine de ton sous-répertoire "/dmarc"
# Date Create : 20220527
# Date modify : 20220622
#-------------------------------------------------------------
DIR="$(dirname "$(readlink -f -- "$0")")"
#DIR="/var/pro/web_sites/zw3b_site/www/dmarc/"
YEAR="2022"
MONTH="06"
FOLDER="${DIR}/${YEAR}/${MONTH}"
if [ ! -d "${FOLDER}" ]; then mkdir -p "${FOLDER}"; fi
## find . -maxdepth 1 -type f \( -iname "*.gz" -o -iname "*.zip" \) -printf '%f\n'
# build an array by listing the folder only with files *.(gz|zip)
readarray -d '' lists < <(find "${FOLDER}" -maxdepth 1 -type f \( -iname "*.gz" -o -iname "*.zip" \) -print0)
echo "+-----------------------------------------------"
echo "NB FILES : ${#lists[@]} IN FOLDER"
echo -e "+-----------------------------------------------\n"
c=0;
for name in "${lists[@]}"; do
#echo "name: ${name}"
EXT="${name##*.}"
# echo "EXT: ${EXT}"
FILE_DZIPED="$(basename "${name%.*}")"
# echo "Filename: ${FILE_DZIPED}"
case "${EXT}" in
'zip')
[ "$(echo "${FILE_DZIPED}" | grep -q "xml")" ] || FILE_DZIPED="${FILE_DZIPED}.xml"
;;
esac
if [ ! -f "${FOLDER}/${FILE_DZIPED}" ] && [ -f "${name}" ]; then
c=$((c+1))
echo "+-----------------------------------------------"
echo -n "\ $c";
echo " - ${name}";
echo " \ "
echo -n " +-- Unzip file "
echo -e " type : ${EXT} \n";
case "${EXT}" in
'gz')
gzip -dk "${name}"
;;
'zip')
unzip "${name}" -d ${FOLDER}
;;
esac
echo -e "\n"
fi
done;
echo "+-----------------------------------------------"
echo "NB FILES : ${c} PROCESSED"
echo -e "+-----------------------------------------------\n"
time extract-dmarc-file.bash
| real | 0m0,014s |
|---|---|
| user | 0m0,014s |
| sys | 0m0,001s |
Et une autre version :
Super génial ![]()
#!/bin/bash
#-------------------------------------------------------------
# Author : O.Romain.Jaillet-ramey (orj (at) lab3w (dot) fr)
# Contributor : Stephane Huc (PengouinPdt)
# Desc : Script pour dezipper les fichiers DMARC
# Pour ne pas avoir à créer manuellement les répertoires de destination « AAAA/MM ».
# Ils seront créés à partir du timestamp récupéré depuis le nom de fichier compressé reçu
# Fichier GZ/IP dans le répertoire ./dmarc/
# Executer le script depuis le sous-répertoire ./dmarc/
# Date Create : 20220527
# Date modify : 20220622
#-------------------------------------------------------------
DIR="$(dirname "$(readlink -f -- "$0")")"
#DIR="/var/pro/web_sites/zw3b_site/www/dmarc/"
## find . -maxdepth 1 -type f \( -iname "*.gz" -o -iname "*.zip" \) -printf '%f\n'
# build an array by listing the folder only with files *.(gz|zip)
readarray -d '' lists < <(find "${DIR}" -maxdepth 1 -type f \( -iname "*.gz" -o -iname "*.zip" \) -print0)
echo "+-----------------------------------------------"
echo "NB FILES : ${#lists[@]} IN FOLDER"
echo -e "+-----------------------------------------------\n"
c=0;
for name in "${lists[@]}"; do
#echo "name: ${name}"
EXT="${name##*.}"
#echo "EXT: ${EXT}"
FILE_DZIPED="$(basename "${name%.*}")"
# echo "Filename: ${FILE_DZIPED}"
case "${EXT}" in
'zip')
[ "$(echo "${FILE_DZIPED}" | grep -q "xml")" ] || FILE_DZIPED="${FILE_DZIPED}.xml"
;;
esac
#echo "Filename: ${FILE_DZIPED}"
# get timestamp into filename to build folder
timestamp="$(echo "${FILE_DZIPED}" | awk -F '!' '{ print $3 }')"
#echo "timestamp: $timestamp"
datename="$(date +'%Y/%m' -d "@${timestamp}")"
#echo "date: $datename"
unset timestamp
FOLDER="${DIR}/${datename}"
#echo "Folder: $FOLDER"
unset datename
# mkdir folder if not exists
if [ ! -d "${FOLDER}" ]; then mkdir -p "${FOLDER}"; fi
if [ ! -f "${FOLDER}/${FILE_DZIPED}" ]; then
c=$((c+1))
echo "+-----------------------------------------------"
echo -n "\ $c";
echo " - ${name}";
echo " \ "
echo -n " +-- Unzip file "
echo -e " type : ${EXT} \n";
case "${EXT}" in
'gz')
gunzip -ck "${name}" > "${FOLDER}/${FILE_DZIPED}"
;;
'zip')
unzip "${name}" -d "${FOLDER}"
;;
esac
echo -e "\n"
fi
done;
echo "+-----------------------------------------------"
echo "NB FILES : ${c} PROCESSED"
echo -e "+-----------------------------------------------\n"
Je soulignais malgré tout
que plus de 1000 fichiers dans un et même répertoire çà craind - çà rame - que je reçois par exemple environ 200 fichiers / mois - Il faudrait simplement déplacer les ZIP/GZ dans le répertoire à côté des XML dézipper - Pour celles et ceux qui auraient mis tous les fichiers DMARC de rapports GZ/ZIP dans un dossier sans savoir en faire quoi
- Y’a un super tableau HTML maintenant, le PHPSource ici ![]()
Bonne journée à vous.
NdMoi-même 20221003 : J’ai créé un « tar.gz » des « dmarc-script-bash-to-unzip-range-files » avec les 3 fichiers pour unziper ![]()
# tar xzvfp dmarc-script-bash-to-unzip-range-files.tar.gz
dmarc-file-dzip.sh
extract-dmarc-file.bash
extract-dmarc-file-vs2.bash
Romain ![]()