Autostart script from USB device with Udev

Create Udev rule

SUBSYSTEM=="block", KERNEL=="sd*1", ACTION=="add", RUN="/usr/sbin/usb_autorun.sh %E{ID_FS_LABEL} %E{ID_FS_UUID}"
>

Create autorun wrapper

#!/bin/bash

SCRIPT=autorun.sh

autorun() {
    if [ -z ${1} ]; then
        FILE=/media/${2}/${SCRIPT}
    else
        FILE=/media/${1}/${SCRIPT}
    fi

    sleep 10

    if [ -f ${FILE} ]; then
        /bin/bash ${FILE}
    fi
}

autorun $@ &
>

Create autorun.sh script on your USB device (example)

#!/bin/bash

USER=$(who | cut -d" " -f1 | head -1)
MAILTO="YOU@YOUR_DOMAIN.COM"

LOG="${0%/*}/backup.$(date -I).log"
CMD="/bin/bash ${0%/*}/backup.sh | tee -a ${LOG}"

if [ -f /home/"${USER}"/.Xauthority ]; then
    # try to open xterm
    export XAUTHORITY=/home/${USER}/.Xauthority
    export DISPLAY=:0.0

    notify-send -i drive-harddisk-usb "Backup" "USB backup started"

    xterm -e ${CMD}

    notify-send -i dialog-ok "Backup" "USB backup completed"
else
    # no X11, run on console
    ${CMD}
fi

# send log by email if sendmail available
if [ -x /usr/sbin/sendmail -a -n "${MAILTO}" ]; then
    # change user for nullmailer
    NULLMAILER_USER=info

    echo "Subject: Backup Log from ${HOSTNAME}" | cat - ${LOG} | sendmail ${MAILTO} 
fi
>