Forum Programmation.shell Equivalent boucle for ?

Posté par .
Tags : aucun
1
26
nov.
2008
Bonjour,
J'ai ecris le script ci dessous pour la sauvegarde des users.
Y a t'il moyen de simplifier la liste des find par une boucle for
Je voudrais garder les fichiers de sauvegarde sous la forme
full_A_$TODAY.afio.Z -> tous les users dont le homedir commence par a
...
full_Z_$TODAY.afio.Z -> tous les users dont le homedir commence par z

Merci d'avance


#!/bin/bash
#
export LC_ALL=C # Date en anglais
EXCLUDE_FILE=/root/scripts/exclude.txt
LIST="/tmp/backup.txt"
DISK_SDA="/dev/sda1"
DISK_SDB="/dev/sdb1"
TODAY=`date +%Y%m%d`

MakeFullBackup () {

/bin/mkdir "/backup/pc406_63/full_$TODAY"
/usr/bin/find /home/a* | /usr/bin/afio -o -W "$EXCLUDE_FILE" -L "/backup/log/full_$TODAY.log" -Z "/backup/pc406_63/full_$TODAY/full_A_$TODAY.afio.Z"
/usr/bin/find /home/b* | /usr/bin/afio -o -W "$EXCLUDE_FILE" -L "/backup/log/full_$TODAY.log" -Z "/backup/pc406_63/full_$TODAY/full_B_$TODAY.afio.Z"
/usr/bin/find /home/c* | /usr/bin/afio -o -W "$EXCLUDE_FILE" -L "/backup/log/full_$TODAY.log" -Z "/backup/pc406_63/full_$TODAY/full_C_$TODAY.afio.Z"
/usr/bin/find /home/d* | /usr/bin/afio -o -W "$EXCLUDE_FILE" -L "/backup/log/full_$TODAY.log" -Z "/backup/pc406_63/full_$TODAY/full_D_$TODAY.afio.Z"
/usr/bin/find /home/e* | /usr/bin/afio -o -W "$EXCLUDE_FILE" -L "/backup/log/full_$TODAY.log" -Z "/backup/pc406_63/full_$TODAY/full_E_$TODAY.afio.Z"
/usr/bin/find /home/f* | /usr/bin/afio -o -W "$EXCLUDE_FILE" -L "/backup/log/full_$TODAY.log" -Z "/backup/pc406_63/full_$TODAY/full_F_$TODAY.afio.Z"
/usr/bin/find /home/g* | /usr/bin/afio -o -W "$EXCLUDE_FILE" -L "/backup/log/full_$TODAY.log" -Z "/backup/pc406_63/full_$TODAY/full_G_$TODAY.afio.Z"
/usr/bin/find /home/h* | /usr/bin/afio -o -W "$EXCLUDE_FILE" -L "/backup/log/full_$TODAY.log" -Z "/backup/pc406_63/full_$TODAY/full_H_$TODAY.afio.Z"
/usr/bin/find /home/i* | /usr/bin/afio -o -W "$EXCLUDE_FILE" -L "/backup/log/full_$TODAY.log" -Z "/backup/pc406_63/full_$TODAY/full_I_$TODAY.afio.Z"
/usr/bin/find /home/j* | /usr/bin/afio -o -W "$EXCLUDE_FILE" -L "/backup/log/full_$TODAY.log" -Z "/backup/pc406_63/full_$TODAY/full_J_$TODAY.afio.Z"
/usr/bin/find /home/k* | /usr/bin/afio -o -W "$EXCLUDE_FILE" -L "/backup/log/full_$TODAY.log" -Z "/backup/pc406_63/full_$TODAY/full_K_$TODAY.afio.Z"
/usr/bin/find /home/l* | /usr/bin/afio -o -W "$EXCLUDE_FILE" -L "/backup/log/full_$TODAY.log" -Z "/backup/pc406_63/full_$TODAY/full_L_$TODAY.afio.Z"
/usr/bin/find /home/m* | /usr/bin/afio -o -W "$EXCLUDE_FILE" -L "/backup/log/full_$TODAY.log" -Z "/backup/pc406_63/full_$TODAY/full_M_$TODAY.afio.Z"
/usr/bin/find /home/n* | /usr/bin/afio -o -W "$EXCLUDE_FILE" -L "/backup/log/full_$TODAY.log" -Z "/backup/pc406_63/full_$TODAY/full_N_$TODAY.afio.Z"
/usr/bin/find /home/o* | /usr/bin/afio -o -W "$EXCLUDE_FILE" -L "/backup/log/full_$TODAY.log" -Z "/backup/pc406_63/full_$TODAY/full_O_$TODAY.afio.Z"
/usr/bin/find /home/p* | /usr/bin/afio -o -W "$EXCLUDE_FILE" -L "/backup/log/full_$TODAY.log" -Z "/backup/pc406_63/full_$TODAY/full_P_$TODAY.afio.Z"
/usr/bin/find /home/q* | /usr/bin/afio -o -W "$EXCLUDE_FILE" -L "/backup/log/full_$TODAY.log" -Z "/backup/pc406_63/full_$TODAY/full_Q_$TODAY.afio.Z"
/usr/bin/find /home/r* | /usr/bin/afio -o -W "$EXCLUDE_FILE" -L "/backup/log/full_$TODAY.log" -Z "/backup/pc406_63/full_$TODAY/full_R_$TODAY.afio.Z"
/usr/bin/find /home/s* | /usr/bin/afio -o -W "$EXCLUDE_FILE" -L "/backup/log/full_$TODAY.log" -Z "/backup/pc406_63/full_$TODAY/full_S_$TODAY.afio.Z"
/usr/bin/find /home/t* | /usr/bin/afio -o -W "$EXCLUDE_FILE" -L "/backup/log/full_$TODAY.log" -Z "/backup/pc406_63/full_$TODAY/full_T_$TODAY.afio.Z"
/usr/bin/find /home/u* | /usr/bin/afio -o -W "$EXCLUDE_FILE" -L "/backup/log/full_$TODAY.log" -Z "/backup/pc406_63/full_$TODAY/full_U_$TODAY.afio.Z"
/usr/bin/find /home/v* | /usr/bin/afio -o -W "$EXCLUDE_FILE" -L "/backup/log/full_$TODAY.log" -Z "/backup/pc406_63/full_$TODAY/full_V_$TODAY.afio.Z"
/usr/bin/find /home/w* | /usr/bin/afio -o -W "$EXCLUDE_FILE" -L "/backup/log/full_$TODAY.log" -Z "/backup/pc406_63/full_$TODAY/full_W_$TODAY.afio.Z"
/usr/bin/find /home/x* | /usr/bin/afio -o -W "$EXCLUDE_FILE" -L "/backup/log/full_$TODAY.log" -Z "/backup/pc406_63/full_$TODAY/full_X_$TODAY.afio.Z"
/usr/bin/find /home/y* | /usr/bin/afio -o -W "$EXCLUDE_FILE" -L "/backup/log/full_$TODAY.log" -Z "/backup/pc406_63/full_$TODAY/full_Y_$TODAY.afio.Z"
/usr/bin/find /home/z* | /usr/bin/afio -o -W "$EXCLUDE_FILE" -L "/backup/log/full_$TODAY.log" -Z "/backup/pc406_63/full_$TODAY/full_Z_$TODAY.afio.Z"

}

MakeIncrementalBackup () {

/bin/mkdir "/backup/pc406_63/incre_$TODAY"
/usr/bin/find /home/a* -depth -type f \( -ctime -1 -o -mtime -1 \) -print > $LIST
/bin/cat "$LIST" | /usr/bin/afio -o -W "$EXCLUDE_FILE" -L "/backup/log/incre_$TODAY.log" -Z "/backup/pc406_63/incre_$TODAY/incre_A_$TODAY.afio.Z"

/usr/bin/find /home/b* -depth -type f \( -ctime -1 -o -mtime -1 \) -print > $LIST
/bin/cat "$LIST" | /usr/bin/afio -o -W "$EXCLUDE_FILE" -L "/backup/log/incre_$TODAY.log" -Z "/backup/pc406_63/incre_$TODAY/incre_B_$TODAY.afio.Z"

/usr/bin/find /home/c* -depth -type f \( -ctime -1 -o -mtime -1 \) -print > $LIST
/bin/cat "$LIST" | /usr/bin/afio -o -W "$EXCLUDE_FILE" -L "/backup/log/incre_$TODAY.log" -Z "/backup/pc406_63/incre_$TODAY/incre_C_$TODAY.afio.Z"

/usr/bin/find /home/d* -depth -type f \( -ctime -1 -o -mtime -1 \) -print > $LIST
/bin/cat "$LIST" | /usr/bin/afio -o -W "$EXCLUDE_FILE" -L "/backup/log/incre_$TODAY.log" -Z "/backup/pc406_63/incre_$TODAY/incre_D_$TODAY.afio.Z"

/usr/bin/find /home/e* -depth -type f \( -ctime -1 -o -mtime -1 \) -print > $LIST
/bin/cat "$LIST" | /usr/bin/afio -o -W "$EXCLUDE_FILE" -L "/backup/log/incre_$TODAY.log" -Z "/backup/pc406_63/incre_$TODAY/incre_E_$TODAY.afio.Z"

/usr/bin/find /home/f* -depth -type f \( -ctime -1 -o -mtime -1 \) -print > $LIST
/bin/cat "$LIST" | /usr/bin/afio -o -W "$EXCLUDE_FILE" -L "/backup/log/incre_$TODAY.log" -Z "/backup/pc406_63/incre_$TODAY/incre_F_$TODAY.afio.Z"

/usr/bin/find /home/g* -depth -type f \( -ctime -1 -o -mtime -1 \) -print > $LIST
/bin/cat "$LIST" | /usr/bin/afio -o -W "$EXCLUDE_FILE" -L "/backup/log/incre_$TODAY.log" -Z "/backup/pc406_63/incre_$TODAY/incre_G_$TODAY.afio.Z"

/usr/bin/find /home/h* -depth -type f \( -ctime -1 -o -mtime -1 \) -print > $LIST
/bin/cat "$LIST" | /usr/bin/afio -o -W "$EXCLUDE_FILE" -L "/backup/log/incre_$TODAY.log" -Z "/backup/pc406_63/incre_$TODAY/incre_H_$TODAY.afio.Z"

/usr/bin/find /home/i* -depth -type f \( -ctime -1 -o -mtime -1 \) -print > $LIST
/bin/cat "$LIST" | /usr/bin/afio -o -W "$EXCLUDE_FILE" -L "/backup/log/incre_$TODAY.log" -Z "/backup/pc406_63/incre_$TODAY/incre_I_$TODAY.afio.Z"

/usr/bin/find /home/j* -depth -type f \( -ctime -1 -o -mtime -1 \) -print > $LIST
/bin/cat "$LIST" | /usr/bin/afio -o -W "$EXCLUDE_FILE" -L "/backup/log/incre_$TODAY.log" -Z "/backup/pc406_63/incre_$TODAY/incre_J_$TODAY.afio.Z"

/usr/bin/find /home/k* -depth -type f \( -ctime -1 -o -mtime -1 \) -print > $LIST
/bin/cat "$LIST" | /usr/bin/afio -o -W "$EXCLUDE_FILE" -L "/backup/log/incre_$TODAY.log" -Z "/backup/pc406_63/incre_$TODAY/incre_K_$TODAY.afio.Z"

/usr/bin/find /home/l* -depth -type f \( -ctime -1 -o -mtime -1 \) -print > $LIST
/bin/cat "$LIST" | /usr/bin/afio -o -W "$EXCLUDE_FILE" -L "/backup/log/incre_$TODAY.log" -Z "/backup/pc406_63/incre_$TODAY/incre_L_$TODAY.afio.Z"

/usr/bin/find /home/m* -depth -type f \( -ctime -1 -o -mtime -1 \) -print > $LIST
/bin/cat "$LIST" | /usr/bin/afio -o -W "$EXCLUDE_FILE" -L "/backup/log/incre_$TODAY.log" -Z "/backup/pc406_63/incre_$TODAY/incre_M_$TODAY.afio.Z"

/usr/bin/find /home/n* -depth -type f \( -ctime -1 -o -mtime -1 \) -print > $LIST
/bin/cat "$LIST" | /usr/bin/afio -o -W "$EXCLUDE_FILE" -L "/backup/log/incre_$TODAY.log" -Z "/backup/pc406_63/incre_$TODAY/incre_N_$TODAY.afio.Z"

/usr/bin/find /home/o* -depth -type f \( -ctime -1 -o -mtime -1 \) -print > $LIST
/bin/cat "$LIST" | /usr/bin/afio -o -W "$EXCLUDE_FILE" -L "/backup/log/incre_$TODAY.log" -Z "/backup/pc406_63/incre_$TODAY/incre_O_$TODAY.afio.Z"

/usr/bin/find /home/p* -depth -type f \( -ctime -1 -o -mtime -1 \) -print > $LIST
/bin/cat "$LIST" | /usr/bin/afio -o -W "$EXCLUDE_FILE" -L "/backup/log/incre_$TODAY.log" -Z "/backup/pc406_63/incre_$TODAY/incre_P_$TODAY.afio.Z"

/usr/bin/find /home/q* -depth -type f \( -ctime -1 -o -mtime -1 \) -print > $LIST
/bin/cat "$LIST" | /usr/bin/afio -o -W "$EXCLUDE_FILE" -L "/backup/log/incre_$TODAY.log" -Z "/backup/pc406_63/incre_$TODAY/incre_Q_$TODAY.afio.Z"

/usr/bin/find /home/r* -depth -type f \( -ctime -1 -o -mtime -1 \) -print > $LIST
/bin/cat "$LIST" | /usr/bin/afio -o -W "$EXCLUDE_FILE" -L "/backup/log/incre_$TODAY.log" -Z "/backup/pc406_63/incre_$TODAY/incre_R_$TODAY.afio.Z"

/usr/bin/find /home/s* -depth -type f \( -ctime -1 -o -mtime -1 \) -print > $LIST
/bin/cat "$LIST" | /usr/bin/afio -o -W "$EXCLUDE_FILE" -L "/backup/log/incre_$TODAY.log" -Z "/backup/pc406_63/incre_$TODAY/incre_S_$TODAY.afio.Z"

/usr/bin/find /home/t* -depth -type f \( -ctime -1 -o -mtime -1 \) -print > $LIST
/bin/cat "$LIST" | /usr/bin/afio -o -W "$EXCLUDE_FILE" -L "/backup/log/incre_$TODAY.log" -Z "/backup/pc406_63/incre_$TODAY/incre_T_$TODAY.afio.Z"

/usr/bin/find /home/u* -depth -type f \( -ctime -1 -o -mtime -1 \) -print > $LIST
/bin/cat "$LIST" | /usr/bin/afio -o -W "$EXCLUDE_FILE" -L "/backup/log/incre_$TODAY.log" -Z "/backup/pc406_63/incre_$TODAY/incre_U_$TODAY.afio.Z"

/usr/bin/find /home/v* -depth -type f \( -ctime -1 -o -mtime -1 \) -print > $LIST
/bin/cat "$LIST" | /usr/bin/afio -o -W "$EXCLUDE_FILE" -L "/backup/log/incre_$TODAY.log" -Z "/backup/pc406_63/incre_$TODAY/incre_V_$TODAY.afio.Z"

/usr/bin/find /home/w* -depth -type f \( -ctime -1 -o -mtime -1 \) -print > $LIST
/bin/cat "$LIST" | /usr/bin/afio -o -W "$EXCLUDE_FILE" -L "/backup/log/incre_$TODAY.log" -Z "/backup/pc406_63/incre_$TODAY/incre_W_$TODAY.afio.Z"

/usr/bin/find /home/x* -depth -type f \( -ctime -1 -o -mtime -1 \) -print > $LIST
/bin/cat "$LIST" | /usr/bin/afio -o -W "$EXCLUDE_FILE" -L "/backup/log/incre_$TODAY.log" -Z "/backup/pc406_63/incre_$TODAY/incre_X_$TODAY.afio.Z"

/usr/bin/find /home/y* -depth -type f \( -ctime -1 -o -mtime -1 \) -print > $LIST
/bin/cat "$LIST" | /usr/bin/afio -o -W "$EXCLUDE_FILE" -L "/backup/log/incre_$TODAY.log" -Z "/backup/pc406_63/incre_$TODAY/incre_Y_$TODAY.afio.Z"

/usr/bin/find /home/z* -depth -type f \( -ctime -1 -o -mtime -1 \) -print > $LIST
/bin/cat "$LIST" | /usr/bin/afio -o -W "$EXCLUDE_FILE" -L "/backup/log/incre_$TODAY.log" -Z "/backup/pc406_63/incre_$TODAY/incre_Z_$TODAY.afio.Z"

}

ChangePartition () {

QuelDisk=`/bin/mount | /bin/grep backup | cut -d " " -f1` # Recupere la partition

# Quelle partition monté pour la nouvelle semaine de sauvegarde
if [ "$QuelDisk" == "$DISK_SDA" ] ; then
/bin/rm /backup/DISK_OK
/bin/umount /backup
/bin/mount -t ext3 "$DISK_SDB" /backup
/bin/touch /backup/DISK_OK
/bin/umount /backup
echo "$DISK_SDB"
else
echo "$DISK_SDA"
/bin/rm /backup/DISK_OK
/bin/umount /backup
/bin/mount -t ext3 "$DISK_SDA" /backup
/bin/touch /backup/DISK_OK
/bin/umount /backup
fi

}

#------------- Debut programme principal -------------

set $(date)

/bin/mount -t ext3 "$DISK_SDA" /backup

case "$1" in
"Mon")
if [ -a "/backup/DISK_OK" ] ; then
MakeFullBackup
else
/bin/umount /backup
/bin/mount -t ext3 "$DISK_SDB" /backup
MakeFullBackup
fi
echo "Fin Full Backup"
;;

"Tue" | "Wed" | "Fri" | "Sat")
if [ -a "/backup/DISK_OK" ] ; then
MakeIncrementaleBackup
else
/bin/umount /backup
/bin/mount -t ext3 "$DISK_SDB" /backup
MakeIncrementalBackup
fi
echo "Fin Incremental Backup"
;;

"Sun")
if [ -a "/backup/DISK_OK" ] ; then
MakeIncrementalBackup
else
/bin/umount /backup
/bin/mount -t ext3 "$DISK_SDB" /backup
MakeIncrementalBackup
ChangePartition
fi
;;
esac

umount /backup
<
  • # for letter in a b c d e f

    Posté par (page perso) . Évalué à 5.

    Ca devrait ressembler à un truc du genre :

    for letter in a b c d e f
    do
    LETTER=$(echo $letter | tr "[a-z]" "[A-Z]")
    /usr/bin/find /home/${letter}* | /usr/bin/afio -o -W "$EXCLUDE_FILE" -L "/backup/log/full_$TODAY.log" -Z "/backup/pc406_63/full_$TODAY/full_${LETTER}_$TODAY.afio.Z"

    for letter in a b c d e f
    do
    /usr/bin/find /home/${letter}* -depth -type f \( -ctime -1 -o -mtime -1 \) -print > $LIST

    /bin/cat "$LIST" | /usr/bin/afio -o -W "$EXCLUDE_FILE" -L "/backup/log/incre_$TODAY.log" -Z "/backup/pc406_63/incre_$TODAY/incre_${LETTER}_$TODAY.afio.Z"
    done

    Bonne journée !
    • [^] # [resolu]Re: for letter in a b c d e f

      Posté par . Évalué à 1.

      Ca fonctionne nickel !!!

      Merci.
    • [^] # Re: for letter in a b c d e f

      Posté par (page perso) . Évalué à 4.

      Bonjour,
      les fainéants préfèreront taper for letter in {a..z} ; do ....

      Les > "$LIST" ne sont pas très utiles, seul celui des z survivra...

      Le cat "$LIST" | afio ... est un « UUOC », on peut le remplacer par un < "$LIST" afio ... : j'utiliserais plutôt un seul pipe, avec si besoin l'étape supplémentaire find ... | tee -a "$LIST" | afio ... pour garder une copie de la liste (ou des "${LIST}_$letter").

      Par ailleurs certaines lettres pourraient être absentes, on peut le tester :
      [ "$(printf '%s' /home/${letter}*)" != "/home/${letter}*" ] && find ....

Suivre le flux des commentaires

Note : les commentaires appartiennent à ceux qui les ont postés. Nous n'en sommes pas responsables.