docker-s3-volume-watch/watch

94 lines
1.6 KiB
Plaintext
Raw Normal View History

2014-08-30 21:16:23 +02:00
#!/bin/bash
2014-11-23 18:51:21 +01:00
[[ "$TRACE" ]] && set -x
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 {
echo "${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
LOCAL=$1
REMOTE=$2
if [ "$ENDPOINT_URL" ]; then
AWS="aws --endpoint-url $ENDPOINT_URL"
else
AWS=aws
fi
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
echo "restoring $REMOTE => $LOCAL"
if ! $AWS s3 sync "$REMOTE" "$LOCAL"; then
2014-08-30 21:16:23 +02:00
error_exit "restore failed"
fi
}
function backup {
echo "backup $LOCAL => $REMOTE"
2020-02-14 02:04:19 +01:00
if ! $AWS s3 sync "$LOCAL" "$REMOTE" $DELETE_FLAG; then
echo "backup failed" 1>&2
return 1
2014-08-30 21:16:23 +02:00
fi
}
function final_backup {
echo "backup $LOCAL => $REMOTE"
2020-02-14 02:04:19 +01:00
while ! $AWS s3 sync "$LOCAL" "$REMOTE" $DELETE_FLAG; do
echo "backup failed, will retry" 1>&2
sleep 1
done
exit 0
}
2014-08-30 21:16:23 +02:00
function idle {
echo "ready"
while true; do
sleep ${BACKUP_INTERVAL:-42} &
2014-08-30 21:16:23 +02:00
wait $!
[ -n "$BACKUP_INTERVAL" ] && backup
2014-08-30 21:16:23 +02:00
done
}
restore
trap final_backup SIGHUP SIGINT SIGTERM
2014-08-30 21:16:23 +02:00
trap "backup; idle" USR1
idle