-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(s3): add s3 support for backup storage (#70)
* feat(s3): add s3 support for backup storage * feat(Dockerfile): use mc RPM instead of binary directly * feat(backup.sh): use mcli instead of mc * chore(README): add link to MinIO object expiration configuration doc Signed-off-by: Valentin Maillot <[email protected]> --------- Signed-off-by: Valentin Maillot <[email protected]> Co-authored-by: Lucas Bickel <[email protected]>
- Loading branch information
Showing
4 changed files
with
80 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
#!/bin/bash | ||
|
||
################################################################################ | ||
# backup.sh OpenShift etcd backup script | ||
################################################################################ | ||
# | ||
# Copyright (C) 2021 Adfinis AG | ||
# Copyright (C) 2024 Adfinis AG | ||
# https://adfinis.com | ||
# [email protected] | ||
# | ||
|
@@ -26,53 +27,75 @@ | |
# | ||
# Authors: | ||
# Cyrill von Wattenwyl <[email protected]> | ||
|
||
# Valentin Maillot <[email protected]> | ||
|
||
set -xeuo pipefail | ||
|
||
# set proper umask | ||
umask "${OCP_BACKUP_UMASK}" | ||
# check storage type | ||
if [ "${OCP_BACKUP_S3}" = "true" ]; then | ||
# prepare & push backup to S3 | ||
|
||
# validate expire type | ||
case "${OCP_BACKUP_EXPIRE_TYPE}" in | ||
days|count|never) ;; | ||
*) echo "backup.expiretype needs to be one of: days,count,never"; exit 1 ;; | ||
esac | ||
# update CA trust | ||
update-ca-trust | ||
|
||
# validate expire numbers | ||
if [ "${OCP_BACKUP_EXPIRE_TYPE}" = "days" ]; then | ||
case "${OCP_BACKUP_KEEP_DAYS}" in | ||
''|*[!0-9]*) echo "backup.expiredays needs to be a valid number"; exit 1 ;; | ||
*) ;; | ||
esac | ||
elif [ "${OCP_BACKUP_EXPIRE_TYPE}" = "count" ]; then | ||
case "${OCP_BACKUP_KEEP_COUNT}" in | ||
''|*[!0-9]*) echo "backup.expirecount needs to be a valid number"; exit 1 ;; | ||
*) ;; | ||
esac | ||
fi | ||
# configure mcli assuming the bucket already exists | ||
bash +o history | ||
mcli alias set "${OCP_BACKUP_S3_NAME}" "${OCP_BACKUP_S3_HOST}" "${OCP_BACKUP_S3_ACCESS_KEY}" "${OCP_BACKUP_S3_SECRET_KEY}" | ||
bash -o history | ||
|
||
# create backup to temporary location | ||
chroot /host /usr/local/bin/cluster-backup.sh /var/tmp/etcd-backup | ||
|
||
# move files to S3 and delete temporary files | ||
mcli mv /host/var/tmp/etcd-backup/* "${OCP_BACKUP_S3_NAME}"/"${OCP_BACKUP_S3_BUCKET}" | ||
rm -rv /host/var/tmp/etcd-backup | ||
else | ||
# prepare, run and copy backup | ||
|
||
# set proper umask | ||
umask "${OCP_BACKUP_UMASK}" | ||
|
||
# validate expire type | ||
case "${OCP_BACKUP_EXPIRE_TYPE}" in | ||
days|count|never) ;; | ||
*) echo "backup.expiretype needs to be one of: days,count,never"; exit 1 ;; | ||
esac | ||
|
||
# validate expire numbers | ||
if [ "${OCP_BACKUP_EXPIRE_TYPE}" = "days" ]; then | ||
case "${OCP_BACKUP_KEEP_DAYS}" in | ||
''|*[!0-9]*) echo "backup.expiredays needs to be a valid number"; exit 1 ;; | ||
*) ;; | ||
esac | ||
elif [ "${OCP_BACKUP_EXPIRE_TYPE}" = "count" ]; then | ||
case "${OCP_BACKUP_KEEP_COUNT}" in | ||
''|*[!0-9]*) echo "backup.expirecount needs to be a valid number"; exit 1 ;; | ||
*) ;; | ||
esac | ||
fi | ||
|
||
# make dirname and cleanup paths | ||
BACKUP_FOLDER="$( date "${OCP_BACKUP_DIRNAME}")" || { echo "Invalid backup.dirname" && exit 1; } | ||
BACKUP_PATH="$( realpath -m "${OCP_BACKUP_SUBDIR}/${BACKUP_FOLDER}" )" | ||
BACKUP_PATH_POD="$( realpath -m "/backup/${BACKUP_PATH}" )" | ||
BACKUP_ROOTPATH="$( realpath -m "/backup/${OCP_BACKUP_SUBDIR}" )" | ||
# make dirname and cleanup paths | ||
BACKUP_FOLDER="$( date "${OCP_BACKUP_DIRNAME}")" || { echo "Invalid backup.dirname" && exit 1; } | ||
BACKUP_PATH="$( realpath -m "${OCP_BACKUP_SUBDIR}/${BACKUP_FOLDER}" )" | ||
BACKUP_PATH_POD="$( realpath -m "/backup/${BACKUP_PATH}" )" | ||
BACKUP_ROOTPATH="$( realpath -m "/backup/${OCP_BACKUP_SUBDIR}" )" | ||
|
||
# make nescesary directorys | ||
mkdir -p "/host/var/tmp/etcd-backup" | ||
mkdir -p "${BACKUP_PATH_POD}" | ||
# make necessary directories | ||
mkdir -p "/host/var/tmp/etcd-backup" | ||
mkdir -p "${BACKUP_PATH_POD}" | ||
|
||
# create backup to temporary location | ||
chroot /host /usr/local/bin/cluster-backup.sh /var/tmp/etcd-backup | ||
# create backup to temporary location | ||
chroot /host /usr/local/bin/cluster-backup.sh /var/tmp/etcd-backup | ||
|
||
# move files to pvc and delete temporary files | ||
mv /host/var/tmp/etcd-backup/* "${BACKUP_PATH_POD}" | ||
rm -rv /host/var/tmp/etcd-backup | ||
# move files to PVC and delete temporary files | ||
mv /host/var/tmp/etcd-backup/* "${BACKUP_PATH_POD}" | ||
rm -rv /host/var/tmp/etcd-backup | ||
|
||
# expire backup | ||
if [ "${OCP_BACKUP_EXPIRE_TYPE}" = "days" ]; then | ||
find "${BACKUP_ROOTPATH}" -mindepth 1 -maxdepth 1 -type d -mtime "+${OCP_BACKUP_KEEP_DAYS}" -exec rm -rv {} + | ||
elif [ "${OCP_BACKUP_EXPIRE_TYPE}" = "count" ]; then | ||
# shellcheck disable=SC3040,SC2012 | ||
ls -1tp "${BACKUP_ROOTPATH}" | awk "NR>${OCP_BACKUP_KEEP_COUNT}" | xargs -I{} rm -rv "${BACKUP_ROOTPATH}/{}" | ||
# expire backup | ||
if [ "${OCP_BACKUP_EXPIRE_TYPE}" = "days" ]; then | ||
find "${BACKUP_ROOTPATH}" -mindepth 1 -maxdepth 1 -type d -mtime "+${OCP_BACKUP_KEEP_DAYS}" -exec rm -rv {} + | ||
elif [ "${OCP_BACKUP_EXPIRE_TYPE}" = "count" ]; then | ||
# shellcheck disable=SC3040,SC2012 | ||
ls -1tp "${BACKUP_ROOTPATH}" | awk "NR>${OCP_BACKUP_KEEP_COUNT}" | xargs -I{} rm -rv "${BACKUP_ROOTPATH}/{}" | ||
fi | ||
fi |