11#! /usr/bin/env bash
2-
32set -o errexit
3+ set -o pipefail
44
55JOB_NAME=${JOB_NAME:- mirrIO}
66RETENTION=${RETENTION:- 30d0h0m}
77ADDITIONAL_PARAMETERS=${ADDITIONAL_PARAMETERS:- --quiet}
8+ MC_BINARY=${MC_BINARY:-/ work/ mc}
89
9- function pushgateway {
10+ pushgateway () {
1011 if [[ -n " ${PUSHGATEWAY_URL} " ]]; then
11- cat << -EOF | curl --retry 3 --max-time 5 --silent --show-error --data-binary @- "${PUSHGATEWAY_URL} /metrics/job/mirrio/instance/${JOB_NAME} " >/dev/null
12- # TYPE mirrio_sync_failed gauge
13- # HELP mirrio_sync_failed Whether the last sync failed
14- mirrio_sync_failed $1
15- EOF
12+ cat << -EOF | curl --retry 3 --max-time 5 --silent --show-error \
13+ --data-binary @- "${PUSHGATEWAY_URL} /metrics/job/mirrio/instance/${JOB_NAME} " >/dev/null
14+ # TYPE mirrio_sync_failed gauge
15+ # HELP mirrio_sync_failed Whether the last sync failed
16+ mirrio_sync_failed $1
17+ EOF
1618 fi
1719}
1820
19- function check_env {
21+ check_env () {
2022 for var in " $@ " ; do
2123 if [[ -z " ${! var} " ]]; then
2224 echo " ${var} is not set!"
@@ -25,7 +27,7 @@ function check_env {
2527 done
2628}
2729
28- function notify {
30+ notify () {
2931 pushgateway 1
3032 echo " ERROR: Unable to sync!"
3133}
@@ -42,14 +44,22 @@ check_env \
4244
4345trap notify ERR
4446
45- mc alias set source " ${SOURCE_URL} " " ${SOURCE_ACCESSKEY} " " ${SOURCE_SECRETKEY} " --api S3v4
47+ # Split extra parameters into an array so multiple flags work
48+ # shellcheck disable=SC2206
49+ read -r -a MC_ARGS <<< " ${ADDITIONAL_PARAMETERS}"
4650
47- mc alias set destination " ${DESTINATION_URL} " " ${DESTINATION_ACCESSKEY} " " ${DESTINATION_SECRETKEY} " --api S3v4
51+ " $MC_BINARY " alias set source " ${SOURCE_URL} " " ${SOURCE_ACCESSKEY} " " ${SOURCE_SECRETKEY} " --api S3v4
52+ " $MC_BINARY " alias set destination " ${DESTINATION_URL} " " ${DESTINATION_ACCESSKEY} " " ${DESTINATION_SECRETKEY} " --api S3v4
4853
49- mc ls destination | grep -q " ${DESTINATION_BUCKET} " || mc mb " destination/${DESTINATION_BUCKET} "
54+ # Create destination bucket if missing (ignore if it already exists)
55+ " $MC_BINARY " mb --ignore-existing " destination/${DESTINATION_BUCKET} "
5056
51- [[ -n $( mc ls " source/${SOURCE_BUCKET} " ) ]] && mc rm --force --recursive --dangerous --older-than " ${RETENTION} " " source/${SOURCE_BUCKET} "
57+ # Optional retention cleanup on SOURCE (only if bucket exists)
58+ if " $MC_BINARY " ls " source/${SOURCE_BUCKET} " > /dev/null 2>&1 ; then
59+ " $MC_BINARY " rm --force --recursive --dangerous --older-than " ${RETENTION} " " source/${SOURCE_BUCKET} "
60+ fi
5261
53- mc mirror --remove " ${ADDITIONAL_PARAMETERS} " " source/${SOURCE_BUCKET} " " destination/${DESTINATION_BUCKET} "
62+ # Mirror with remove + any additional flags
63+ " $MC_BINARY " mirror --remove " ${MC_ARGS[@]} " " source/${SOURCE_BUCKET} " " destination/${DESTINATION_BUCKET} "
5464
5565pushgateway 0
0 commit comments