• [^] # Re: s/hotplug/udev

    Posté par . En réponse au message Lancer un script a la connexion d'un disque dur usb. Évalué à 2.

    Ah ça (me) rassure ! :-)

    Dans ma règle udev, j'appelle un 1er script qui lance le deuxième en arrière plan. Le tout avec un verrou basé sur l'existence d'un fichier qui évite de le relancer si il tourne déja.

    Désolé, mes scripts ne sont pas dispos car ils contiennent des dépendances vers d'autres scripts (et ce n'est pas libre ;-( car pas encore entièrement fini et dépoussiérés de choses "en dur")

    Mais bon, voici quelques extraits:

    -----------------------------------
    la règle udev:

    # detect exactly a given external usb disk (brand/model/serial)
    # then launch backup to it

    # to find sysfs{*} data:
    # 1) udevinfo -q path -n /dev/sdX (or -q all to see all values)
    # 2) udevinfo -a -p value_from_previous_command

    # NOTE: run command must be given with full path
    # variable ACTION can be tested (will be "add" or "remove")
    # WARNING: script is called 2 times on disk insertion !!!
    # In fact, one time for the disk and one time for each partition !!!
    # WARNING: remove does not work, script will NOT be called on disk removal !!!

    BUS=="usb", SYSFS{idProduct}=="xxxx", SYSFS{idVendor}=="xxxx", SYSFS{serial}=="xxxxxxxxxxxxxxxx", NAME="%k", SYMLINK+="diskusb4backup%n", OWNER="root", GROUP="root", MODE="0770", RUN+="/usr/local/sbin/backup_usbdisk.sh"

    -----------------------------------
    le 1er script:

    #!/bin/bash

    # this script will be called by the udev rule which detects a certain
    # external usb disk based on manufacturer, model and serial number
    #
    # BUG: it is called many times (for disk and for each partitions)

    logger -t xxx 0ドル started

    # random delay 0..9
    DELAY=$(($RANDOM % 10))
    logger -t xxx 0ドル sleeping for ${DELAY} seconds...
    sleep ${DELAY}

    bash backup_usbdisk2.sh tag &

    logger -t xxx 0ドル finished

    -----------------------------------
    le 2ème script:

    #!/bin/bash

    # backup_usbdisk2.sh
    # called by backup_usbdisk.sh

    set -x

    CURR_DATETIME=$(date "+%F_%H:%M:%S_%N")

    logger -t xxx "0ドル started : udev action=${ACTION} at ${CURR_DATETIME} , user=$(whoami)"

    # check tag argument
    if [ "1ドル" != "tag" ]; then
    logger -t xxx "0ドル : Script should not be called directly !"
    exit 1
    fi

    # source common functions
    COMMFONC_FILE="common_funcs.sh"
    which ${COMMFONC_FILE} >/dev/null 2>&1
    if [ $? -eq 0 ]; then
    . ${COMMFONC_FILE}
    else
    logger -t xxx "0ドル : Script file with common functions (${COMMFONC_FILE}) not found !"
    exit 1
    fi

    # source config file
    CONFIG_FILE="/etc/xxx/backup.config"
    which ${CONFIG_FILE} >/dev/null 2>&1
    if [ $? -eq 0 ]; then
    . ${CONFIG_FILE}
    else
    logger -t xxx "0ドル : Config file (${CONFIG_FILE}) not found !"
    exit 1
    fi

    # script execution is blocked ?
    if [ "${USBDISK_RUNBLOCK}" == "1" ]; then
    logger -t xxx "0ドル : Script execution is actually blocked by config file option !"
    exit 1
    fi

    # exit function
    trap_exit () {
    if [ "$MOUNTED" == "1" ]; then
    umount ${DISK_PART} 2>/dev/null
    fi
    script_unlock >/dev/null
    }
    export -f trap_exit

    trap trap_exit EXIT SIGINT SIGTERM

    # lock execution (bug ? udev will call this script many times !)
    if [ "$(test_script_lock)" == "1" ]; then
    logger -t xxx "0ドル is already running..."
    sleep 1s
    exit 1
    fi
    script_lock >/dev/null

    # do one backup type
    one_backup () {
    # ici y'a le backup à proprement parler...
    MOUNTED=0
    sync
    }

    MOUNTED=0

    if [ "${ACTION}" == "add" ]; then
    one_backup

    if [ "${ACTION}" == "remove" ]; then
    # nothing to do ?
    logger -t xxx "0ドル : Remove done"
    fi

    script_unlock >/dev/null
    exit 0

    Quand j'aurais tout fini, je le mettrais à disposition car il permet de sauvegarder sur CD ou DVD en faisant automatiquement des "rondelles" de la bonne taille + des fichiers par2 pour l'ecc. Je n'ai rien trouvé de tout prêt pour faire ça donc je me suis lancé la dedans...

    /