Page 1 sur 1

[Bash] Export et versionning des scripts DzVents

Publié : 26 sept. 2022, 00:22
par hestia
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
export_dzVent.png
export_dzVent.png (39.07 Kio) Consulté 825 fois
Cela permet de faire des recherches dans les scripts
export_dzVent_R.png
export_dzVent_R.png (24.43 Kio) Consulté 825 fois
Et de voir rapidement une version précédente
export_dzVent_V.png
export_dzVent_V.png (61.29 Kio) Consulté 825 fois
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 ***"
Je le lance toute les heures

Code : Tout sélectionner

56  *  * * * /home/pi/mydomoticz/myscripts/export_dzVent.sh 2>&1 | /usr/bin/logger -t DZBACKUP

Re: [Bash] Export et versionning des scripts DzVents

Publié : 26 sept. 2022, 12:46
par Keros
Merci pour ce script.

Je viens de déplacer le sujet dans la section des tutos.

Petite question : pourquoi ce besoin de la lancer toutes les heures ?

Re: [Bash] Export et versionning des scripts DzVents

Publié : 26 sept. 2022, 23:33
par hestia
"toute les heures"
comme on veut!
mais si on fait des modifs dans la journée et que l'on veut revenir en arrière, on ne perd qu'une heure.
De toute façon, ça crée une version que si ça change, on pourrait donc le faire passer toutes les 5 minutes ou tous les jours.
C'est surtout intéressant quand ça change

Re: [Bash] Export et versionning des scripts DzVents

Publié : 28 sept. 2022, 22:42
par hestia
attention, les scripts sont sauvegardés dans le DB régulièrement...
si l'on passe le script juste après une modif ou création de DzVents, le script ne le voit pas
Je ne sais plus la fréquence ; 5 min ?
c'est pour cela que le script est lancé à 56 dans le cron proposé