[Bash] Export et versionning des scripts DzVents
Publié : 26 sept. 2022, 00:22
hello
j'ai fais un script pour exporter et archiver avec versions les scripts de dz (seulement pour DzVents car je n'utilise que ça! Mais modifiable simplement)
Comme je les exporte vers mon NAS, je peux ensuite y accéder en via un PC bureatique Cela permet de faire des recherches dans les scripts Et de voir rapidement une version précédente Tout en restant dans domoticz pour faire les scripts
Le shell
Je le lance toute les heures
j'ai fais un script pour exporter et archiver avec versions les scripts de dz (seulement pour DzVents car je n'utilise que ça! Mais modifiable simplement)
Comme je les exporte vers mon NAS, je peux ensuite y accéder en via un PC bureatique Cela permet de faire des recherches dans les scripts Et de voir rapidement une version précédente Tout en restant dans domoticz pour faire les scripts
Le shell
Code : Tout sélectionner
#!/bin/bash
#
# export all dzVents to separate files with version and status (On = active / Off = inactive)
# archive previous versions
#
# scriptname.nnn.status.lua
# nnnn = version 001, 002...
# status = 1 (active) / 0 (Inactive)
#
# from: https://www.domoticz.com/forum/viewtopic.php?f=31&t=28627&p=218924&hilit=save+file+lua#p218924
# 29/10/2021 - initial
# 08/05/2022 - versionning of the scripts
# Path to the NAS backup folder
DESTDIRNAS='/mnt/criosNFS/backups'
# mount the folder for the BK file (if not yet done = issue)
sudo mount -o vers=3 /mnt/criosNFS # script to execute by root due to this command
domoticzDir='/home/pi/PROD_domoticz/'
scriptDirVersion=$DESTDIRNAS'/BK_dzVents' # scripts last version
scriptDirArchives=$DESTDIRNAS'/BK_dzVents_Archives' # script archives
scriptDirTmp='/tmp/dzVentsScripts'
mkdir -p $scriptDirVersion
mkdir -p $scriptDirTmp
mkdir -p $scriptDirArchives
sudo cp $domoticzDir/domoticz.db /tmp/tmp.db
sudo sqlite3 /tmp/tmp.db "select ID, name,status from eventMaster WHERE interpreter = 'dzVents'" | while read line;
do
#echo "line:" $line
ID=$(echo $line | awk -F'|' '{ print $1 }')
#echo "ID " $ID
NAME=$(echo $line | awk -F'|' '{ print $2 }')
#echo "NAME " $NAME
STATUS=$(echo $line | awk -F'|' '{ print $3 }')
#echo "STATUS" $STATUS
scriptDirTmpName=$scriptDirTmp/$NAME
#echo $scriptDirTmpName
sudo sqlite3 /tmp/tmp.db "select XMLSTatement from eventMaster WHERE interpreter = 'dzVents' and ID = $ID" > $scriptDirTmpName
#echo "export " $NAME " to " $scriptDirTmpName
# search in versions
LASTFILE=`find $scriptDirVersion -maxdepth 1 -name "$NAME.*" -type f -print | sort | tail -1`
N=${#LASTFILE}
echo '====> version < ' $N '>'
#echo $LASTFILE
LASTVERSION=0
if [ $N = 0 ] ; then
#echo $NAME 'not present in the version folder'
# search in archives
LASTFILE=`find $scriptDirArchives -name "$NAME.*" -type f -print | sort | tail -1`
N=${#LASTFILE}
#echo '====> archive < ' $N '>'
#echo $LASTFILE
if [ $N = 0 ] ; then
echo $NAME 'not present in the archives folder' > /dev/null
else
#echo $NAME 'exists in the archives folder'
LASTVERSION=$(echo $LASTFILE | awk -F'.' '{ print $2 }')
echo 'LASTVERSION='$LASTVERSION
fi
VERSION=$((10#$LASTVERSION + 1))
VERSION=$(printf "%03d" $VERSION)
#echo 'VERSION='$VERSION
NAMEVERSION=$NAME"."$VERSION"."$STATUS".lua"
#echo $NAMEVERSION
scriptDirVersionName=$scriptDirVersion/$NAMEVERSION
echo $NAME $VERSION $STATUS "new"
# copy the export script to the version folder
cp $scriptDirTmpName $scriptDirVersionName
else
echo $NAME 'exists in the version folder'
LASTVERSION=$(echo $LASTFILE | awk -F'.' '{ print $2 }')
echo 'LASTVERSION='$LASTVERSION
if cmp -s $LASTFILE $scriptDirTmpName ; then
#echo $NAME "idem"
VERSION=$LASTVERSION
NAMEVERSION=$NAME"."$VERSION"."$STATUS".lua"
#echo $NAMEVERSION
scriptDirVersionName=$scriptDirVersion/$NAMEVERSION
#echo $scriptDirVersionName
if [ $LASTFILE != $scriptDirVersionName ]; then
mv $LASTFILE $scriptDirVersionName
echo $NAME $VERSION $STATUS "status changed"
fi
else
echo $NAME "different"
VERSION=$((10#$LASTVERSION + 1))
VERSION=$(printf "%03d" $VERSION)
#echo 'VERSION='$VERSION
NAMEVERSION=$NAME"."$VERSION"."$STATUS".lua"
#echo $NAMEVERSION
scriptDirVersionName=$scriptDirVersion/$NAMEVERSION
echo $NAME $VERSION $STATUS "new version"
# move previous version(s) to archive, one folder for each script
mkdir -p $scriptDirArchives/"$NAME"
find $scriptDirVersion -maxdepth 1 ! -name $NAMEVERSION -name "$NAME.*" -type f -execdir mv -t $scriptDirArchives/"$NAME" {} +
# copy the export script to the version folder
cp $scriptDirTmpName $scriptDirVersionName
fi
fi
done
# archive old version with no longer script in dz
for SCRIPTPATHNAME in $scriptDirVersion/*.lua
do
NAMEVERSION=`basename $SCRIPTPATHNAME`
#echo $NAMEVERSION "in version folder"
NAME=$(echo $NAMEVERSION | awk -F'.' '{ print $1 }')
#echo $NAME
EXPORTNAME=`find $scriptDirTmp -maxdepth 1 -name "$NAME" -type f -print`
#echo "<"$EXPORTNAME">"
if [ -z $EXPORTNAME ] ; then
# move old version to archive, one folder for each script
echo $NAME $VERSION $STATUS "archived"
mkdir -p $scriptDirArchives"/"$NAME
mv $SCRIPTPATHNAME $scriptDirArchives"/"$NAME
fi
done
rm -r $scriptDirTmp
sudo rm /tmp/tmp.db
#echo "*** END ***"
Code : Tout sélectionner
56 * * * * /home/pi/mydomoticz/myscripts/export_dzVent.sh 2>&1 | /usr/bin/logger -t DZBACKUP