docker-s3-volume-watch/watch

106 lines
2.2 KiB
Plaintext
Raw Permalink Normal View History

2014-08-30 21:16:23 +02:00
#!/bin/bash
2014-11-23 18:51:21 +01:00
[[ "$TRACE" ]] && set -x
2023-06-26 14:49:03 +02:00
DEBUG=${DEBUG:-"Unknown Error"}
2023-07-16 10:30:27 +02:00
function log {
now=`date +"%Y-%m-%d %T"`
echo "[${now}] $1"
}
2014-08-30 21:16:23 +02:00
function usage {
cat <<-EOF
2014-11-23 18:51:21 +01:00
Usage: $PROGNAME [OPTIONS] <local-path> <remote-path>
Sync s3 directory locally and backup changed files on exit
--force-restore restore even if local directory is not empty
eg: $PROGNAME /data s3://bucket/dir
EOF
2014-08-30 21:16:23 +02:00
}
function error_exit {
2023-07-16 10:30:27 +02:00
log "${1:-"Unknown Error"}" 1>&2
exit 1
2014-08-30 21:16:23 +02:00
}
2014-11-23 18:51:21 +01:00
PARSED_OPTIONS=$(getopt -n "$0" -o f --long "force-restore" -- "$@")
if [ $? -ne 0 ]; then
2014-11-23 18:51:21 +01:00
exit 1
2014-08-30 21:16:23 +02:00
fi
2014-11-23 18:51:21 +01:00
eval set -- "$PARSED_OPTIONS"
while true; do
2014-11-23 18:51:21 +01:00
case "$1" in
-f|--force-restore)
2014-11-23 18:51:21 +01:00
FORCE_RESTORE="true"
shift;;
--)
shift
break;;
esac
done
2014-08-30 21:16:23 +02:00
PROGNAME=$0
2023-06-26 14:49:03 +02:00
S3_PROG=/s5cmd
2014-08-30 21:16:23 +02:00
LOCAL=$1
REMOTE=$2
2023-06-26 14:49:03 +02:00
WATCH_EVENTS=modify
2014-08-30 21:16:23 +02:00
function restore {
if [ "$(ls -A $LOCAL)" ]; then
if [[ ${FORCE_RESTORE:false} == 'true' ]]; then
error_exit "local directory is not empty"
fi
2014-08-30 21:16:23 +02:00
fi
2023-07-16 10:30:27 +02:00
log "restoring $REMOTE => $LOCAL"
2023-06-26 14:49:03 +02:00
if ! $S3_PROG sync "$REMOTE/*" "$LOCAL"; then
2014-08-30 21:16:23 +02:00
error_exit "restore failed"
fi
}
function backup {
2023-07-16 10:30:27 +02:00
log "backup $LOCAL => $REMOTE"
2023-06-26 14:49:03 +02:00
if ! $S3_PROG sync "$LOCAL" "$REMOTE" $S3_SYNC_FLAGS; then
2023-07-16 10:30:27 +02:00
log "backup failed" 1>&2
return 1
2014-08-30 21:16:23 +02:00
fi
}
function final_backup {
2023-07-16 10:30:27 +02:00
log "backup $LOCAL => $REMOTE"
2023-06-26 14:49:03 +02:00
while ! $S3_PROG sync "$LOCAL" "$REMOTE" $S3_SYNC_FLAGS; do
2023-07-16 10:30:27 +02:00
log "backup failed, will retry" 1>&2
sleep 1
done
exit 0
}
2014-08-30 21:16:23 +02:00
function idle {
2023-07-16 10:30:27 +02:00
log "ready"
log "RESTORE_INTERVAL: ${RESTORE_INTERVAL}"
2014-08-30 21:16:23 +02:00
while true; do
2023-06-26 14:49:03 +02:00
restore
2023-07-16 10:30:27 +02:00
if [[ -v RESTORE_INTERVAL ]]; then
2023-06-26 14:49:03 +02:00
backup
2023-07-16 10:30:27 +02:00
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
2023-06-26 14:49:03 +02:00
else
2023-07-16 10:30:27 +02:00
log "Without restore interval"
2023-06-26 14:49:03 +02:00
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
2023-07-16 10:30:27 +02:00
log "The file '$file' appeared in directory '$path' via '$action'"
2023-06-26 14:49:03 +02:00
backup
done
fi
2014-08-30 21:16:23 +02:00
done
}
trap final_backup SIGHUP SIGINT SIGTERM
2014-08-30 21:16:23 +02:00
trap "backup; idle" USR1
idle