feat: Add datetime on log
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Simon 2023-07-16 10:30:27 +02:00
parent 1f29978152
commit 67440a7f1f
1 changed files with 17 additions and 12 deletions

29
watch
View File

@ -4,6 +4,11 @@
DEBUG=${DEBUG:-"Unknown Error"}
function log {
now=`date +"%Y-%m-%d %T"`
echo "[${now}] $1"
}
function usage {
cat <<-EOF
Usage: $PROGNAME [OPTIONS] <local-path> <remote-path>
@ -16,7 +21,7 @@ EOF
}
function error_exit {
echo "${1:-"Unknown Error"}" 1>&2
log "${1:-"Unknown Error"}" 1>&2
exit 1
}
@ -51,49 +56,49 @@ function restore {
fi
fi
echo "restoring $REMOTE => $LOCAL"
log "restoring $REMOTE => $LOCAL"
if ! $S3_PROG sync "$REMOTE/*" "$LOCAL"; then
error_exit "restore failed"
fi
}
function backup {
echo "backup $LOCAL => $REMOTE"
log "backup $LOCAL => $REMOTE"
if ! $S3_PROG sync "$LOCAL" "$REMOTE" $S3_SYNC_FLAGS; then
echo "backup failed" 1>&2
log "backup failed" 1>&2
return 1
fi
}
function final_backup {
echo "backup $LOCAL => $REMOTE"
log "backup $LOCAL => $REMOTE"
while ! $S3_PROG sync "$LOCAL" "$REMOTE" $S3_SYNC_FLAGS; do
echo "backup failed, will retry" 1>&2
log "backup failed, will retry" 1>&2
sleep 1
done
exit 0
}
function idle {
echo "ready"
log "ready"
log "RESTORE_INTERVAL: ${RESTORE_INTERVAL}"
while true; do
restore
if [[ -v $RESTORE_INTERVAL ]]; then
if [[ -v RESTORE_INTERVAL ]]; then
backup
timeout ${RESTORE_INTERVAL} inotifywait -m -e modify -e move -e create -e delete --timefmt '%Y-%m-%d %H:%M:%S' --format '%T %f' $LOCAL | while read date time file; do echo echo "The file '$file' appeared in directory '$path' via '$action'"; backup; done
timeout ${RESTORE_INTERVAL} inotifywait -m -e modify -e move -e create -e delete --timefmt '%Y-%m-%d %H:%M:%S' --format '%T %f' $LOCAL | while read date time file; do log "The file '$file' appeared in directory '$path' via '$action'"; backup; done
else
log "Without restore interval"
inotifywait -m -e modify -e move -e create -e delete --timefmt '%Y-%m-%d %H:%M:%S' --format '%T %f' $LOCAL |
while read date time file
do
echo echo "The file '$file' appeared in directory '$path' via '$action'"
log "The file '$file' appeared in directory '$path' via '$action'"
backup
done
fi
done
}
restore
trap final_backup SIGHUP SIGINT SIGTERM
trap "backup; idle" USR1