Script pour rapatrier des snapshots

Tags: #<Tag:0x00007f50a35622f0> #<Tag:0x00007f50a3562098>

Je galère avec ce script au niveau de la boucle for :confused:

J’ai un fichier nommé test-list.txt dans mon exemple avec une liste sous forme de csv lambda

XXX,XXXX,XXXXX,XXXX
YYY,YYYY,YYYYY,YYYYY

je vous joint le script en entier pour plus de lisibilité :

#!/bin/bash

# Variables
LIST="/root/XXXXXXXX/test-list.txt"                                                   	# Server list path
UUIDMNT=$(mount | grep "secstorage01-pool01" | cut -d '/' -f6 | cut -d ' ' -f1)         # Secstorage UUID volume
NFSMNT="/var/cloud/$UUIDMNT/"                                                           # Mount point NFS of secstorage
NFSMNT2="/mnt/XXXXXXXX/"                                                       			# Mount point NFS for transfer
VAR1=$(cat "$LINE" | cut -d ',' -f2)                                                    # Instance volume ID
VAR2=$(cat "$LINE" | cut -d ',' -f3)                                                    # Destination Path, Name of serveur
VAR3=$(cat "$LINE" | cut -d ',' -f4)                                                    # Destination Path, root or datadisk
VAR4=$(cat "$LINE" | cut -d ',' -f1)                                                    # Customer account ID
TRY_COUNT=0                                                                             # Actual tries count
TRY_COUNT_MAX=3                                                                         # Max tries count
RETOUR=""                                                                               # Valeur de la sortie du rsync
LOG="/root/XXXXXXXX/backup.log"                                                       	# Log path
NFSSTATE=$(timeout -s 9 10 stat -f -c '%T' "$NFSMNT2" | grep -c nfs)                    # NFS state value
NFSCHECK=$(stat -f -L -c %T "$NFSMNT2")                                                 # Check mount point type

# Checking server list
echo $LIST
if [[ -f "$LIST" ]]
then
        echo "$LIST not found" >> $LOG
else
        echo "Server list is found" >> $LOG
fi
# Checking secstorage volume mount point state
mount -t nfs -o rw 192.168.1.10:/mnt/USB-XXXXXXXX/ /mnt/XXXXXXXX/
sleep 5
if [[ -r $NFSMNT ]]
then
        echo "NFS mount point XXXXXX-secstorage01-pool01 is not readable, transfer process is canceled !" >> $LOG
        echo "NFS mount point XXXXXX-secstorage01-pool01 is not readable, please look at this" | mail -s "TEST Report ERROR NFS mount point for XXXXXXXX" XXXXXXXX
        exit 1
else
        echo "NFS mount point XXXXXX-secstorage01-pool01 is check and ready to use !" >> $LOG
fi
echo $NFSCHECK
if [[ $NFSCHECK != "nfs" ]]
then
        echo "NFS mount point XXXXXXXX is not remote dir, transfer process is canceled !" >> $LOG
        echo "NFS mount point XXXXXXXX is not remote dir, please look at this" | mail -s "TEST Report ERROR NFS mount point for XXXXXXXX" XXXXXXXX
        exit 1
else
        echo "NFS mount point XXXXXXXX is check and ready to use !" >> $LOG
fi
echo $NFSSTATE
if [[ $NFSSTATE != 1 ]]
then
        echo "NFS mount point XXXXXXXX is in time out, transfer process is canceled !" >> $LOG
        echo "NFS mount point XXXXXXXX is in time out, please look at this" | mail -s "TEST Report ERROR NFS mount point for XXXXXXXX" XXXXXXXX
        exit 1
else
        echo "NFS mount point is ready for transfer, here we go !" >> $LOG
        # Flush previous log file
        echo "" > $LOG

        # Backup loop
        for LINE in $(cat $LIST)
        do
                echo $LIST
                echo $LINE
                echo $VAR1
                echo $VAR2
                echo $VAR3
                sleep 5
                # transfer of the vhd on the final destination
                rsync -avz /var/cloud/$UUIDMNT/snapshots/188/$VAR1/ /mnt/XXXXXXXX/backup_vhd/$VAR2/$VAR3/ | tee $LOG

                # Checking return command of rsync
                RETOUR=$(echo $?)

                if [[ $RETOUR -ne 0 ]]
                then
                        # while return code of rsync is 23 (partial transfer) we launch another rsync (3 try max)
                        while [ $RETOUR -eq 23 ] && $TRY_COUNT -lt $TRY_COUNT_MAX
                        do
                                $TRY_COUNT=expr $TRY_COUNT +1
                                rsync -avz /var/cloud/$UUIDMNT/snapshots/188/$VAR1/ /mnt/XXXXXXXX/backup_vhd/$VAR2/$VAR3/ | tee $LOG
                        done
                else
                        # Email sending for backup error and reset try count
                        TRY_COUNT=0
                        ERROR="Error during instance ".$VAR2." backup"
                        echo $ERROR | mail -s "TEST Report ERROR backup XXXXXXXX" XXXXXXXX
                        echo "Error during backup reporting email is send to the GNU/Linux Team" >> $LOG
                        exit 1
                fi
                # Report success backup on LOG
                SUCCESS="Success backup transfer for".$VAR2."instance"
                echo "$SUCCESS" >> $LOG
        done
fi

echo "Backup XXXXXXXX successfully transfer on USB disk, please refer to procedure at <URL>" | mail -s "TEST Successfull backup for XXXXXXXX" XXXXXXXX

exit 0

Le souci c’est que mon echo après la boucle ne me renvoie qu’une ligne pour la variable $LINE et qu’en plus les variables $VAR1, $VAR2 et $VAR3 sont vides ce qui fait que mon rsync se passe drôlement bien et rapidement si vous voyez ce que je veux dire :wink:

J’ai tenté d’utiliser une boucle avec du while read mais sans plus de succès.

Ce que je cherche à faire c’est exploiter les variables $VAR à chaque ligne afin de pouvoir réutiliser ce script quelque soit les comptes clients et ou machines disques virtuel à exporter.

Avez-vous une idée pour corriger cela ?

Bonjour Clochette

michel@debg53sw:~$ cat "$LIST"
111,2222,33333,4444
aaa,bbbb,ccccc,dddd
mmm,nnnn,ooooo,pppp
michel@debg53sw:~$ 
michel@debg53sw:~$ maRegex='(.*),(.*),(.*),(.*)'
michel@debg53sw:~$ 
michel@debg53sw:~$ while read; do [[ $REPLY =~ $maRegex ]]; echo "${BASH_REMATCH[1]}"; echo "${BASH_REMATCH[2]}"; echo "${BASH_REMATCH[3]}"; echo "${BASH_REMATCH[4]}"; echo; done < "$LIST"
111
2222
33333
4444

aaa
bbbb
ccccc
dddd

mmm
nnnn
ooooo
pppp

michel@debg53sw:~$ 

Donc, en modifiant légèrement la boucle contenue dans ton script :

#!/bin/bash

# Variables
LIST="/root/XXXXXXXX/test-list.txt"                                                   	# Server list path
UUIDMNT=$(mount | grep "secstorage01-pool01" | cut -d '/' -f6 | cut -d ' ' -f1)         # Secstorage UUID volume
NFSMNT="/var/cloud/$UUIDMNT/"                                                           # Mount point NFS of secstorage
NFSMNT2="/mnt/XXXXXXXX/"                                                       			# Mount point NFS for transfer
TRY_COUNT=0                                                                             # Actual tries count
TRY_COUNT_MAX=3                                                                         # Max tries count
RETOUR=""                                                                               # Valeur de la sortie du rsync
LOG="/root/XXXXXXXX/backup.log"                                                       	# Log path
NFSSTATE=$(timeout -s 9 10 stat -f -c '%T' "$NFSMNT2" | grep -c nfs)                    # NFS state value
NFSCHECK=$(stat -f -L -c %T "$NFSMNT2")                                                 # Check mount point type

# Checking server list
echo $LIST
if [[ -f "$LIST" ]]
then
        echo "$LIST not found" >> $LOG
else
        echo "Server list is found" >> $LOG
fi
# Checking secstorage volume mount point state
mount -t nfs -o rw 192.168.1.10:/mnt/USB-XXXXXXXX/ /mnt/XXXXXXXX/
sleep 5
if [[ -r $NFSMNT ]]
then
        echo "NFS mount point XXXXXX-secstorage01-pool01 is not readable, transfer process is canceled !" >> $LOG
        echo "NFS mount point XXXXXX-secstorage01-pool01 is not readable, please look at this" | mail -s "TEST Report ERROR NFS mount point for XXXXXXXX" XXXXXXXX
        exit 1
else
        echo "NFS mount point XXXXXX-secstorage01-pool01 is check and ready to use !" >> $LOG
fi
echo $NFSCHECK
if [[ $NFSCHECK != "nfs" ]]
then
        echo "NFS mount point XXXXXXXX is not remote dir, transfer process is canceled !" >> $LOG
        echo "NFS mount point XXXXXXXX is not remote dir, please look at this" | mail -s "TEST Report ERROR NFS mount point for XXXXXXXX" XXXXXXXX
        exit 1
else
        echo "NFS mount point XXXXXXXX is check and ready to use !" >> $LOG
fi
echo $NFSSTATE
if [[ $NFSSTATE != 1 ]]
then
        echo "NFS mount point XXXXXXXX is in time out, transfer process is canceled !" >> $LOG
        echo "NFS mount point XXXXXXXX is in time out, please look at this" | mail -s "TEST Report ERROR NFS mount point for XXXXXXXX" XXXXXXXX
        exit 1
else
        echo "NFS mount point is ready for transfer, here we go !" >> $LOG
        # Flush previous log file
        echo "" > $LOG

        # Backup loop
        myRegex='(.*),(.*),(.*),(.*)'       # To extract fields in line
        while read
        do
                [[ $REPLY =~ $myRegexp ]]   # Apply regex on line
                echo $LIST
                echo $REPLY
                echo ${BASH_REMATCH[2]}     # Instance volume ID
                echo ${BASH_REMATCH[3]}     # Destination Path, Name of serveur
                echo ${BASH_REMATCH[4]}     # Destination Path, root or datadisk
                echo ${BASH_REMATCH[1]}     # Customer account ID
                sleep 5
                # transfer of the vhd on the final destination
                rsync -avz /var/cloud/$UUIDMNT/snapshots/188/${BASH_REMATCH[2]}/ /mnt/XXXXXXXX/backup_vhd/${BASH_REMATCH[2]}/${BASH_REMATCH[3]}/ | tee $LOG

                # Checking return command of rsync
                RETOUR=$(echo $?)

                if [[ $RETOUR -ne 0 ]]
                then
                        # while return code of rsync is 23 (partial transfer) we launch another rsync (3 try max)
                        while [ $RETOUR -eq 23 ] && $TRY_COUNT -lt $TRY_COUNT_MAX
                        do
                                $TRY_COUNT=expr $TRY_COUNT +1
                                rsync -avz /var/cloud/$UUIDMNT/snapshots/188/${BASH_REMATCH[2]}/ /mnt/XXXXXXXX/backup_vhd/${BASH_REMATCH[2]}/${BASH_REMATCH[3]}/ | tee $LOG
                        done
                else
                        # Email sending for backup error and reset try count
                        TRY_COUNT=0
                        ERROR="Error during instance ${BASH_REMATCH[3]} backup"
                        echo $ERROR | mail -s "TEST Report ERROR backup XXXXXXXX" XXXXXXXX
                        echo "Error during backup reporting email is send to the GNU/Linux Team" >> $LOG
                        exit 1
                fi
                # Report success backup on LOG
                SUCCESS="Success backup transfer for ${BASH_REMATCH[3]} instance"
                echo "$SUCCESS" >> $LOG
        done < "$LIST"
fi

echo "Backup XXXXXXXX successfully transfer on USB disk, please refer to procedure at <URL>" | mail -s "TEST Successfull backup for XXXXXXXX" XXXXXXXX

exit 0
1 J'aime

Intéressant je vais regarder à ça et merci :wink:

Je te tient au jus.