workflow/initialization/functions.sh
2025-07-19 17:15:10 +02:00

548 lines
22 KiB
Bash

#!/bin/bash
# ----------------- documentation -----------------
#
# @author functions_sync by @tykayn - contact at cipherbliss.com
#!/bin/bash
# ----------------- documentation -----------------
#
# @author functions_sync by @tykayn - contact at cipherbliss.com
source $HOME/Nextcloud/ressources/workflow_nextcloud/public_workflow/workflow_variables.sh
logInOrgmodeInbox() {
local message=$1
echo "TODO: ERREUR functions sync: /!\ $message" | tee >>$inbox_orgmode
echo "CREATED: [$(date +%Y-%m-%d_%H-%M-%S)]" | tee >>$inbox_orgmode
}
export logInOrgmodeInbox
if [ ! -f ~/.tk-borg-passphrase-light ]; then
logInOrgmodeInbox "$HOST : il manque le fichier de borg passphrase dans ~/.tk-borg-passphrase-light"
fi
# --------- log de la date courante -------- #
logDate() {
echo -e "${txtcyn}${txtbold}---${txtreset}" | tee -a $LOG_FILE_BACKUP_DATES 2>&1
date "+%Y-%m-%d %H:%M:%S" | tee -a $LOG_FILE_BACKUP_DATES 2>&1
echo -e "${txtcyn}${txtbold} $1 ${txtreset}" | tee -a $LOG_FILE_BACKUP_DATES 2>&1
echo "${txtcyn}---${txtreset} " | tee -a $LOG_FILE_BACKUP_DATES 2>&1
}
export logDate
#logDate "exclusions de rsync: \n ${exclude_opts[@]}"
# --------- syncro uniquement de borg backup -------- #
# du -sch /home/poule/borg_archives/backup_land4to
clearDiskSyncBorg() {
local diskName=$1
echo " " >>$LOG_FILE_BACKUP_DATES
echo " ---------- sync borg folder to disk $diskName " >>$LOG_FILE_BACKUP_DATES
# chech that the disk exists
FILE=/media/$USER/$diskName
if test -d "$FILE"; then
echo "### $FILE , $diskName exists." >>$LOG_FILE_BACKUP_DATES
echo "### $FILE , dernière syncro:" >>$LOG_FILE_BACKUP_DATES
date -r /media/$USER/$diskName/borg_archives/borg2 "+%Y-%m-%d %H:%M:%S" >>$LOG_FILE_BACKUP_DATES
echo "### ${today} replicate to disk $diskName" >>$LOG_FILE_BACKUP_DATES
logDate "disk $diskName : partie $SPACESHIP_NEW_BORG_REPO"
# log the date of the last big syncro
touch $SPACESHIP_NEW_BORG_REPO/last_synced_from_$HOST.txt
date -r /media/$USER/$diskName/borg_archives/borg2 "+%Y-%m-%d %H:%M:%S" >$SPACESHIP_NEW_BORG_REPO/last_synced_from_$HOST.txt
rsync -avhWP /home/poule/borg_archives /media/$USER/$diskName --perms --delete-before --inplace
date "+%Y-%m-%d %H:%M:%S" | tee -a $LOG_FILE_BACKUP_DATES 2>&1
echo "---- clearDiskSyncBorg $diskName faite -----------------------" | tee -a $LOG_FILE_BACKUP_DATES 2>&1
else
echo "### $FILE introuvable." >>$LOG_FILE_BACKUP_DATES
fi
}
export clearDiskSyncBorg
# syncroniser un laptop avec un disque usb contenant stockage syncable
# exemple:
# getStockageSyncableFromDisk louisbraille
getStockageSyncableFromDisk() {
local diskName=$1
echo " " >>$LOG_FILE_BACKUP_DATES
echo " ---------- get stockage syncable from disk $diskName " >>$LOG_FILE_BACKUP_DATES
FILE=/media/$USER/$diskName
if test -d "$FILE"; then
rsync -avhWP /media/$USER/$diskName/encrypted/stockage-syncable/photos $ARCHIVE_SYNCABLE --perms --delete-before --inplace
rsync -avhWP /media/$USER/$diskName/encrypted/stockage-syncable/ressources $ARCHIVE_SYNCABLE --perms --delete-before --inplace
rsync -avhWP /media/$USER/$diskName/encrypted/stockage-syncable/dessins $ARCHIVE_SYNCABLE --perms --delete-before --inplace
else
echo "### $FILE introuvable." >>$LOG_FILE_BACKUP_DATES
fi
date "+%Y-%m-%d %H:%M:%S" | tee -a $LOG_FILE_BACKUP_DATES 2>&1
echo "---- get stockage syncable from disk $diskName faite -----------------------" | tee -a $LOG_FILE_BACKUP_DATES 2>&1
}
export getStockageSyncableFromDisk
clearDiskSyncBorgServer() {
local diskName=$1
echo " " >>$LOG_FILE_BACKUP_DATES
echo " ---------- sync borg server folder to disk $diskName " >>$LOG_FILE_BACKUP_DATES
# chech that the disk exists
FILE=/media/$USER/$diskName
# enlever le borg backup d'avant
# if test -d "/media/$USER/$diskName/backup_land4to"; then
# rm -rf /media/$USER/$diskName/backup_land4to
# fi
if test -d "$FILE"; then
echo "### $FILE , $diskName exists." >>$LOG_FILE_BACKUP_DATES
echo "### $FILE , dernière syncro:" >>$LOG_FILE_BACKUP_DATES
date -r /media/$USER/$diskName/borg_archives/borg2 "+%Y-%m-%d %H:%M:%S" >>$LOG_FILE_BACKUP_DATES
echo "### ${today} replicate to disk $diskName" >>$LOG_FILE_BACKUP_DATES
logDate "disk $diskName : partie borg2"
# log the date of the last big syncro
touch /home/poule/borg_archives/borg2/last_synced.txt
mkdir -p /media/$USER/$diskName/borg_archives/production-servers-backup
# rsync -avhWP /home/poule/borg_archives/production-servers-backup/rise /media/$USER/$diskName/borg_archives/production-servers-backup --perms --delete-before --inplace
rsync -avhWP /home/poule/borg_archives/borg2/* /media/$USER/$diskName/borg_archives/borg2/ --perms --delete-before --inplace
else
echo "### $FILE introuvable." >>$LOG_FILE_BACKUP_DATES
fi
date "+%Y-%m-%d %H:%M:%S" | tee -a $LOG_FILE_BACKUP_DATES 2>&1
echo "---- clearDiskSyncBorgServer $diskName faite -----------------------" | tee -a $LOG_FILE_BACKUP_DATES 2>&1
}
export clearDiskSyncBorgServer
# --------- recopie des éléments de poule zfs -------- #
# les disques de desintation doivent avoir environ 2.5To de place disponible
# chacun doit refléter la partie interne de /home/poule ainsi que le dossier music
syncToBigDiskName() {
local diskName=$1
echo " " >>$LOG_FILE_BACKUP_DATES
#check that the disk exists
FILE=/media/$USER/$diskName
if test -d "$FILE"; then
echo "### $FILE , $diskName exists." >>$LOG_FILE_BACKUP_DATES
# tester si y'a de la place disponible
if test -d "$stockage_syncable_folder"; then
echo "### ${today} replicate to disk $diskName" >>$LOG_FILE_BACKUP_DATES
# logDate "disk $diskName : part home";
# rsync -avhWP /home/poule/encrypted/home /media/$USER/$diskName/encrypted --perms --delete-before --inplace "${exclude_opts[@]}"
logDate "${txtgrn} disk $diskName : part stockage-syncable : ressources ${txtreset}"
rsync -avhWP $stockage_syncable_folder/ressources/* /media/$USER/$diskName/encrypted/stockage-syncable/ressources/ --delete-before --inplace "${exclude_opts[@]}"
logDate "${txtgrn} disk $diskName : part wulfi borg ${txtreset}"
rsync -avhWP /home/poule/borg_archives/wulfi_backup_borg/ /media/$USER/$diskName/borg_archives/wulfi_backup_borg/ --delete-before --inplace "${exclude_opts[@]}"
logDate "disk $diskName : syncro borg2"
clearDiskSyncBorgServer $1
# log the date of the last big syncro
touch /home/poule/encrypted/last_synced.text
touch $stockage_syncable_folder/source-is-zfs-spaceship.txt
else
echo "### le dossier d'archives $stockage_syncable_folder est introuvable. Zfs n'a pas été dévérouillé par l'administrateur " >>$LOG_FILE_BACKUP_DATES
fi
# else
# echo "disque $diskName trop rempli"
# notify_desktop "ERREUR Syncronisation de sauvegarde: disque $diskName trop rempli"
# fi
else
echo "### $FILE introuvable." >>$LOG_FILE_BACKUP_DATES
fi
date "+%Y-%m-%d %H:%M:%S" | tee -a $LOG_FILE_BACKUP_DATES 2>&1
echo "---- syncToBigDiskName $diskName faite -----------------------" | tee -a $LOG_FILE_BACKUP_DATES 2>&1
disque_libre $diskName
# marquer visuellement la dernière syncronisation à la racine du disque
rm "/media/$USER/$diskName/last synced from $HOST.txt"
rm "/media/$USER/$diskName/*synced from $HOST.txt"
touch "/media/$USER/$diskName/last synced from $HOST.txt"
}
export syncToBigDiskName
# ----------------------------------------------------------------------
# --------- le laptop fatland n"a que 2 To de disponible -------- #
getwulfinas() {
rsync -avhWP "tykayn@192.168.1.15:/volume1/bidules_partagés/wulfila_home/*" /home/poule/encrypted/backup_du_nas/wulfi_home_backup --delete-before --inplace --perms "${exclude_opts[@]}" --exclude "TK-LAND" --exclude=npm --delete-excluded
borg create -r /home/poule/borg_archives/wulfi_backup_borg wulfi_backup_borg::wulfi_home_{now} /home/poule/encrypted/backup_du_nas/wulfi_home_backup --progress --stats
borg prune -v --list --stats --keep-daily=8 --keep-weekly=6 --keep-monthly=3 --keep-yearly=2 -r /home/poule/borg_archives/wulfi_backup_borg
}
export getwulfinas
pushnas() {
rsync -avPz -e ssh /home/poule/borg_archives/borg2/* "tykayn@192.168.1.15:/volume1/bidules_partagés/backup a ne pas modifier/poule/borg_archives/borg2" --delete-before
rsync -avPz -e ssh /home/poule/borg_archives/wulfi_backup_borg/* "tykayn@192.168.1.15:/volume1/bidules_partagés/backup a ne pas modifier/poule/borg_archives/wulfi_backup_borg" --delete-before
}
export pushnas
syncdisksusb() {
syncToBigDiskName brossadent
syncToBigDiskName louisbraille
syncToBigDiskName rugged
syncToBigDiskName hulk
syncToBigDiskName ironman
}
export syncdisksusb
syncfatland() {
echo " " >>$LOG_FILE_BACKUP_DATES
echo " - envoi vers FATland" >>$LOG_FILE_BACKUP_DATES
#### vers le laptop FATland
rsync $stockage_syncable_folder/photos/$CURRENT_YEAR tykayn@192.168.1.12:$stockage_syncable_folder/photos -avhWP --delete-before "${exclude_opts[@]}"
rsync -avhWP $stockage_syncable_folder/photos/* tykayn@192.168.1.12:$stockage_syncable_folder/photos --delete-before "${exclude_opts[@]}"
rsync $stockage_syncable_folder tykayn@192.168.1.12:/home/poule/encrypted -avhWP --delete-before "${exclude_opts[@]}"
rsync /home/poule/borg_archives/* tykayn@192.168.1.12:/home/poule/borg_archives -avhWP --delete-before
date | tee -a $LOG_FILE_BACKUP_DATES 2>&1
echo "sync fatland fait" | tee -a $LOG_FILE_BACKUP_DATES 2>&1
}
export syncfatland
# ----------------- BORG -----------------
# partie contenant tout stockage-syncable
upBorg() {
#killall borg
logDate "### --------- SPACESHIP | creating borg archive at $SPACESHIP_BORG_REPO"
rm -rf ~/.cache/borg/150867528afd85114c8aba98af201a7ad8cf01869c507a87c025d2f8701040a9/lock.exclusive
rm -rf $SPACESHIP_BORG_REPO/lock.exclusive
# borg 2 way to create archive, dans /home/poule/borg_archives/borg2
borg create encrypted_spaceship_{now} $ARCHIVE_SYNCABLE "${exclude_opts[@]}" --progress --verbose --stats --compression zstd,9 | tee -a $LOG_FILE_BACKUP 2>&1
borg prune -v --list --stats --keep-daily=8 --keep-weekly=6 --keep-monthly=3 --keep-yearly=2 -r /home/poule/borg_archives/borg2
# small archive
borg create -r /home/poule/borg_archives/small_stockage_syncable small::{now}_stockage_syncable_small /home/poule/encrypted/stockage-syncable/dessins "${exclude_opts[@]}" --progress --verbose --stats --compression zstd,9 --exclude photos --exclude BAZAR --exclude www --exclude archives
borg prune -v --list --stats --keep-daily=8 --keep-weekly=6 --keep-monthly=3 --keep-yearly=2 -r /home/poule/borg_archives/small_stockage_syncable
borg create -r /home/poule/borg_archives/borg_tk_stockage_photos stockage_syncable::{now}_photos_only /home/poule/encrypted/stockage-syncable/photos/ --exclude imageries -v --stats --progress --verbose --stats --compression zstd,9
borg prune -v --list --stats --keep-daily=8 --keep-weekly=6 --keep-monthly=3 --keep-yearly=2 -r /home/poule/borg_archives/borg_tk_stockage_photos
borg create -r /home/poule/borg_archives/borg_tk_stockage_tout_sauf_photos tout_sauf_photos::stockage_syncable_{now} /home/poule/encrypted/stockage-syncable --exclude photos --progress --stats --compression zstd,9
borg prune -v --list --stats --keep-daily=8 --keep-weekly=6 --keep-monthly=3 --keep-yearly=2 -r /home/poule/borg_archives/borg_tk_stockage_tout_sauf_photos
rsync -avhWP "tykayn@192.168.1.15:/volume1/bidules_partagés/wulfila_home/*" /home/poule/encrypted/backup_du_nas/wulfi_home_backup --delete-before --inplace --perms "${exclude_opts[@]}" --exclude "TK-LAND" --exclude=npm --delete-excluded
# wulfi backup
borg create -r /home/poule/borg_archives/wulfi_backup_borg wulfi_backup_borg::wulfi_home_{now} /home/poule/encrypted/backup_du_nas/wulfi_home_backup --progress --stats
borg prune -v --list --stats --keep-daily=8 --keep-weekly=6 --keep-monthly=3 --keep-yearly=2 -r /home/poule/borg_archives/wulfi_backup_borg
echo " " | tee -a $LOG_FILE_BACKUP 2>&1
logDate "### --------- ${today} | SPACESHIP | pruning old archives" | tee -a $LOG_FILE_BACKUP 2>&1
# nettoyage tk_backup
borg prune -v --list --stats --keep-daily=8 --keep-weekly=6 --keep-monthly=3 --keep-yearly=2 --repo $SPACESHIP_BORG_REPO | tee -a $LOG_FILE_BACKUP 2>&1
logDate "### --------- pruning done"
}
export upBorg
getRiseupBorgArchivesToPoule() {
rsync -avzPW "tykayn@proxmox.coussinet.org:/poule/encrypted/*" /home/poule/borg_archives/production-servers-backup/rise/encrypted --inplace --delete-before --exclude do_not_sync_back --exclude borgbackup_tkland --exclude backup_land4to --exclude imagerie_carto --exclude borg2 --exclude mastodon
}
export getRiseupBorgArchivesToPoule
# envoi vers le serveur riseup de l'archive borg2
sendBorg2ToRiseupServer() {
rsync -avzhWP /home/poule/borg_archives/borg2/* tykayn@proxmox.coussinet.org:/poule/encrypted/borg2 --delete-before
rsync -avzhWP /home/poule/borg_archives/wulfi_backup_borg/* tykayn@proxmox.coussinet.org:/poule/encrypted/borg_archives/wulfi_backup_borg --delete-before
}
export sendBorg2ToRiseupServer
upPhotosADispatcher() {
cd $stockage_syncable_folder/photos/a_dispatcher
/home/tykayn/.local/bin/guessfilename IMG*
/home/tykayn/.local/bin/guessfilename *.jpg
/home/tykayn/.local/bin/guessfilename *.png
/home/tykayn/.local/bin/guessfilename *.mp4
/home/tykayn/.local/bin/guessfilename *.avi
/home/tykayn/.local/bin/date2name --files -w *.jpg
/home/tykayn/.local/bin/date2name --files -w *.png
/home/tykayn/.local/bin/date2name --files -w *.mp4
/home/tykayn/.local/bin/date2name --files -w *.avi
/home/tykayn/.local/bin/filetags "*Capture d'écran*" --tags="screenshots -screenshot"
mv $stockage_syncable_folder/photos/a_dispatcher/*screenshot* /home/poule/encrypted/stockage-syncable/photos/screenshots/
/home/tykayn/.local/bin/filetags /home/poule/encrypted/stockage-syncable/photos/screenshots/*.jpg --tags="screenshots -screenshot"
/home/tykayn/.local/bin/filetags /home/poule/encrypted/stockage-syncable/photos/screenshots/*.png --tags="screenshots -screenshot"
/home/tykayn/.local/bin/guessfilename /home/poule/encrypted/stockage-syncable/photos/screenshots/*.jpg
/home/tykayn/.local/bin/guessfilename /home/poule/encrypted/stockage-syncable/photos/screenshots/*.png
mkdir -p $stockage_syncable_folder/photos/$CURRENT_YEAR
mv $stockage_syncable_folder/photos/a_dispatcher/$CURRENT_YEAR* $stockage_syncable_folder/photos/$CURRENT_YEAR
move2archive --archivepath /home/poule/encrypted/stockage-syncable/photos --batchmode 20*
}
export upPhotosADispatcher
function syncFromSpaceship() {
echo "functions_sync.sh: récupération du dossier stockage syncable depuis spaceship 192.168.1.17"
rsync -avzP "tykayn@192.168.1.17:/home/poule/encrypted/stockage-syncable/photos" /home/poule/encrypted/stockage-syncable/ --delete-before --exclude Nextcloud --inplace "${exclude_opts[@]}"
}
export syncFromSpaceship
disque_libre() {
local disque=$1
# vérifier que le disque existe
if [ ! -d "/media/$USER/$disque" ]; then
# echo "Le disque $disque n'existe pas dans /media/$USER/"
if [ -e "/dev/disk/by-label/$disque" ] && [ ! -d "/media/$USER/$disque" ]; then
echo "Le disque $disque existe mais n'est pas monté dans /media/$USER/"
else
echo "Le disque $disque n'existe pas, il n'est probablement pas branché."
fi
return 1
fi
# seuil en Mo
local seuil=1024
local espace_disponible=$(df -H -k --output=avail "/media/$USER/$disque" | tail -n 1 | awk '{print $1}')
local espace_disponible_gb=$(echo "scale=2; $espace_disponible/1024/1024" | bc)
echo "espace disponible: $espace_disponible_gb GB"
if [ "$espace_disponible" -lt "$seuil" ]; then
echo "Il reste moins de 1Go d'espace disponible sur le disque $disque"
return 0
else
echo "Il reste de la place sur le disque $disque"
return 1
fi
}
export disque_libre
# ----------------------------------------------------------------------
# ------- # renommage des fichiers go pro en gardant le nom original et en ajoutant les tags
gopro_rename() {
echo "renommage des fichiers gorpo dans le dossier courant"
for current_file in ./*; do
if [[ $current_file == *"GPFR"* ]]; then
echo " "
echo "C'est une capture gopro frontale'"
exiftool '-filename<CreateDate' -d "%Y-%m-%dT%H.%I.%S%%c %%f -- gopro gopro-front.%%le" $current_file
elif [[ $current_file == *"GF"* ]]; then
echo " "
echo "C'est une capture gopro frontale'"
exiftool '-filename<CreateDate' -d "%Y-%m-%dT%H.%I.%S%%c %%f -- gopro gopro-front.%%le" $current_file
elif [[ $current_file == *"GPBK"* ]]; then
echo " "
echo "C'est une capture gopro arrière'"
exiftool '-filename<CreateDate' -d "%Y-%m-%dT%H.%I.%S%%c %%f -- gopro gopro-back.%%le" $current_file
elif [[ $current_file == *"GB"* ]]; then
echo " "
echo "C'est une capture gopro arrière'"
exiftool '-filename<CreateDate' -d "%Y-%m-%dT%H.%I.%S%%c %%f -- gopro gopro-back.%%le" $current_file
else
echo " "
echo " guess file name"
guessfilename $current_file
fi
done
echo "fichiers contenant le tag gopro: "
ls -larth . | grep " gopro " | wc -l
echo " "
echo " gopro_rename fini!"
}
export rename
move_geolocated_files() {
# Source directory
src_dir="$1"
# Destination directory
dest_dir="/home/poule/encrypted/stockage-syncable/photos/imageries/gopro/assemblages_géolocalisés/"
mkdir -p "$dest_dir"
for file in "$src_dir"/*; do
# Check if the file is a regular file and not already moved
if [[ -f $file && ! -e ${dest_dir}/$file ]]; then
# Extract GPS data from EXIF metadata using exiftool command line utility
gps=$(exiftool -s3 -GPSLatitude -GPSLongitude "$file")
# Check if GPS data was found
if [[ $gps != "" ]]; then
mv "$file" "${dest_dir}/${file}"
fi
fi
done
}
# ---------- manage log git
logGit_csv() {
git log --pretty=format:"%cd - %an : %s" --graph --since=8.weeks | tee -a log_boulot.org 2>&1
}
export logGit_csv
# écrire un log des commits réalisés groupés par jour pour le dossier courant
logGit_per_day() {
while read -r -u 9 since name; do
until=$(date "+%Y-%m-%d %H:%M:%S")
echo "$since $name"
echo
GIT_PAGER=cat git log \
--no-merges \
--committer="$name" \
--since="$since 00:00:00 +0000" \
--until="$until 00:00:00 +0000" \
--format=" * [%h] %s"
echo
done 9< <(git log --no-merges --format=$"%cd %cn" --date=short --since=8.weeks | sort --unique --reverse)
}
export logGit_per_day
logGit_to_org() {
folder_name=${PWD##*/}
touch log_git_list.org
echo "* Log git $folder_name\n" >log_git_list.org
pwd >>log_git_list.org
cat log_git_list.org
logGit_per_day | tee -a log_git_list.org 2>&1
}
export logGit_to_org
updateTags() {
cp $WORKFLOW_PATH/files_management/.filetags ~/
cp $WORKFLOW_PATH/files_management/.filetags $stockage_syncable_folder/photos
cat $WORKFLOW_PATH/files_management/.filetags
echo "tags mis à jour depuis $WORKFLOW_PATH/files_management/.filetags"
}
export updateTags
# trouver un fichier dans mon corpus de textes org qui contient un certain terme recherché
trouve()
{
# chercher dans mes textes du dossier orgmode
DOSSIER=$orgmode_path
# Rechercher le mot dans les fichiers du dossier
RESULTATS=$(rg -n -i --glob "*.{org,md,txt}" "$1" "$DOSSIER")
# Proposer de sélectionner le fichier avec fzf
SELECTION=$(echo "$RESULTATS" | fzf --prompt "Sélectionner un fichier : ")
# Ouvrir le fichier sélectionné dans gedit et aller à la ligne du résultat
FICHIER=$(echo "$SELECTION" | cut -d: -f1)
LIGNE=$(echo "$SELECTION" | cut -d: -f2)
if [ -n "$FICHIER" ]; then
gedit +$LIGNE "$FICHIER"
emacsclient --daemon --eval "(find-file \"$FICHIER\")(goto-line $LIGNE)" &
fi
}
export trouve
# recherche de fichier selon un terme contenu dans son nom
trouve_file()
{
# Définir le dossier à rechercher
DOSSIER="."
# Demander le terme à rechercher
TERME=$1
# Rechercher le terme dans les noms de fichiers avec rg
RESULTATS=$(rg -i --files-with-matches --glob "*$TERME*" "$DOSSIER")
# Proposer de sélectionner le fichier avec fzf
SELECTION=$(echo "$RESULTATS" | fzf --prompt "Sélectionner un fichier : ")
# Ouvrir le fichier sélectionné
if [ -n "$SELECTION" ]; then
gedit "$SELECTION"
fi
}
export trouve_file
# faire une notification sur le bureau quand un disque est plein
notify_desktop() {
local ICON="dialog-information"
local DURATION=10000
local TITLE=$1
local MESSAGE=$2
local TIMESTAMP=$(date +%s)
local LAST_NOTIFICATION=$(cat /tmp/last_notification 2>/dev/null)
if [ -z "$LAST_NOTIFICATION" ]; then
LAST_NOTIFICATION=0
fi
local ELAPSED_TIME=$((TIMESTAMP - LAST_NOTIFICATION))
if [ $ELAPSED_TIME -gt 60 ]; then
LAST_NOTIFICATION=$TIMESTAMP
echo $LAST_NOTIFICATION > /tmp/last_notification
notify-send -i "$ICON" -t "$DURATION" "$TITLE" "$MESSAGE"
else
local NOTIFICATION_COUNT=$(cat /tmp/notification_count 2>/dev/null)
if [ -z "$NOTIFICATION_COUNT" ]; then
NOTIFICATION_COUNT=0
fi
if [ $NOTIFICATION_COUNT -lt 3 ]; then
((NOTIFICATION_COUNT++))
echo $NOTIFICATION_COUNT > /tmp/notification_count
notify-send -i "$ICON" -t "$DURATION" "$TITLE" "$MESSAGE"
fi
fi
}
export notify_desktop
## rsync qui vérifie les erreurs et fait une notification de bureau
rsync_secure() {
local SOURCE=$1
local DESTINATION=$2
shift 2
local RSYNC_OPTIONS=("$@")
if [ ! -d "$SOURCE" ]; then
notify_desktop "Erreur" "Le dossier source n'existe pas" "Veuillez vérifier le chemin du dossier source"
return 1
fi
if [ ! -d "$DESTINATION" ]; then
notify_desktop "Erreur" "Le dossier de destination n'existe pas" "Veuillez vérifier le chemin du dossier de destination"
return 1
fi
local DISK_SPACE=$(df -h "$DESTINATION" | awk '{print $5}' | sed's/%//g')
if [ $DISK_SPACE -gt 90 ]; then
notify_desktop "Erreur" "La destination est trop remplie" "Veuillez libérer de l'espace disque avant de poursuivre. Dossier: $DESTINATION"
return 1
fi
rsync "${RSYNC_OPTIONS[@]}" "$SOURCE" "$DESTINATION"
if [ $? -ne 0 ]; then
notify_desktop "Erreur" "Erreur lors de la synchronisation" "Veuillez vérifier les journaux pour plus d'informations"
return 1
fi
notify_desktop "Succès" "La synchronisation a été effectuée avec succès" ""
}
export rsync_secure