#!/bin/sh # Configuration DIR_SOURCE="" DIR_DESTINATION="" MAX_WAIT=60 RSYNC_OPTIONS="-q -aH --delete" INOTIFYWAIT_OPTIONS="-qq -r -e close_write -e attrib -e move -e create -e delete -e unmount" #RSYNC_OPTIONS="-v -aH --delete" #INOTIFYWAIT_OPTIONS="-r -e close_write -e attrib -e move -e create -e delete -e unmount" # On essaie de diminuer la priorité de la chose # Aucune importance si les utilitaires ne sont pas dispos renice 15 -p $$ >/dev/null 2>&1 ionice -c 3 -p $$ >/dev/null 2>&1 # Arrêt à la moindre erreur non-catchée set -e # Fonctions # Little helper to centralize syslog calls loglog() { LEVEL="$1" shift logger -s -t inotify-rsync -p user.$LEVEL $@ } # affiche le message d'aide usage() { cat <&2 exit 1 ;; esac done #shift $( expr $OPTIND - 1 ) #REPDRUPAL="$1" # Some checks if [ -z "$DIR_SOURCE" ] || [ ! -d "$DIR_SOURCE" ]; then loglog err "ERROR: no source specified or directory inexistent." >&2 exit 1 fi if [ -z "$DIR_DESTINATION" ]; then loglog err "ERROR: no destination specified." >&2 exit 1 fi while true; do # rsync can send error code != 0, we'll have to check # what this code means before failing set +e rsync $RSYNC_OPTIONS "$DIR_SOURCE" "$DIR_DESTINATION" RSYNC_RETURN_CODE="$?" set -e while [ "$RSYNC_RETURN_CODE" -ne "0" ]; do for i in 24; do if [ "$RSYNC_RETURN_CODE" -eq "$i" ]; then # If the return code is "acceptable", we go on loglog debug "DEBUG: rsync returned code '$RSYNC_RETURN_CODE'" break 2 fi done # The return code wasn't in the "acceptable" list => log + exit loglog err "ERROR: rsync returned unexpected error code : $RSYNC_RETURN_CODE. Script stopped." exit $RSYNC_RETURN_CODE done # Wait for another change # inotifywait can send error code != 0, we'll have to check # what this code means before failing set +e inotifywait $INOTIFYWAIT_OPTIONS -t "$MAX_WAIT" "$DIR_SOURCE" INOTIFYWAIT_RETURN_CODE="$?" set -e if [ "$INOTIFYWAIT_RETURN_CODE" -ne "0" ]; then if [ "$INOTIFYWAIT_RETURN_CODE" -ne "2" ]; then loglog err "ERROR: inotifywait returned unexpected error code : $INOTIFYWAIT_RETURN_CODE. Script stopped." exit $INOTIFYWAIT_RETURN_CODE fi fi done