1
0
Fork 0
scripts-admin-quickndirty-p.../oldies/script_inotify-rsync.sh

129 lines
3 KiB
Bash
Executable file

#!/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 <<EOF
$0 [-i inotifywait_options] [-r rsync_options] [-t max_wait] -s source_directory -d rsync_destination
$0 -h
-s : specify the rsync source (needs to be a local directory)
-d : specify the rsync destination (can be anythin rsync accepts)
-i : inotifywait options (minus the -t option)
(default : "$INOTIFYWAIT_OPTIONS")
-r : rsync options
(default : "$RSYNC_OPTIONS")
-t : Even without event (or in case of missed event), rsync will be launched every "max_wait" seconds
(0 = wait forever) (default: $MAX_WAIT)
-h : this help screen
EOF
}
# Début du code
# gestion des options de lancement
while getopts s:d:r:w:h o; do
case $o in
's')
DIR_SOURCE="$OPTARG"
;;
'd')
DIR_DESTINATION="$OPTARG"
;;
'i')
INOTIFYWAIT_OPTIONS="$OPTARG"
;;
'r')
RSYNC_OPTIONS="$OPTARG"
;;
'w')
MAX_WAIT="$OPTARG"
;;
'h')
usage
exit 0
;;
\?)
usage >&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