Skip to content

Commit 2dbef6b

Browse files
committed
improve script
1 parent 6f7e59f commit 2dbef6b

2 files changed

Lines changed: 27 additions & 17 deletions

File tree

Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
FROM ghcr.io/containeroo/alpine-toolbox:2.7.11
1+
FROM ghcr.io/containeroo/alpine-toolbox:2.8.5
22

33
# renovate: datasource=github-tags depName=minio/mc extractVersion=^RELEASE\\.(?<major>\\d+)-(?<minor>\\d+)-(?<patch>\\d+)T.*Z(-(?<compatibility>.*))?$
44
ARG MINIO_VERSION=RELEASE.2023-10-14T01-57-03Z
55

6-
RUN wget -O /usr/local/bin/mc https://dl.min.io/client/mc/release/linux-amd64/mc && \
7-
chmod +x /usr/local/bin/mc
6+
RUN wget -O /work/mc https://dl.min.io/client/mc/release/linux-amd64/mc && \
7+
chmod +x /work/mc
88

99
ADD mirrio.sh /mirrio.sh
1010

mirrio.sh

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
#!/usr/bin/env bash
2-
32
set -o errexit
3+
set -o pipefail
44

55
JOB_NAME=${JOB_NAME:-mirrIO}
66
RETENTION=${RETENTION:-30d0h0m}
77
ADDITIONAL_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

4345
trap 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

5565
pushgateway 0

0 commit comments

Comments
 (0)