-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(): Add support for s3 storage in Kubernetes etcd backup job #25
Open
norbertgruszka
wants to merge
5
commits into
main
Choose a base branch
from
22-add-support-for-s3-storage
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c4bc917
feat(): add S3 Environment variables
norbertgruszka 06a2b6d
feat(): create backup and push to s3
norbertgruszka 05c6b64
fix(): user permssion
norbertgruszka cf1515b
fix(): use double quotes
norbertgruszka 8a752bc
fix(): use double quotes
norbertgruszka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -3,7 +3,7 @@ | |
# backup.sh General etcd backup script | ||
################################################################################ | ||
# | ||
# Copyright (C) 2023 Adfinis AG | ||
# Copyright (C) 2024 Adfinis AG | ||
# https://adfinis.com | ||
# [email protected] | ||
# | ||
|
@@ -32,50 +32,77 @@ | |
|
||
set -xeuo pipefail | ||
|
||
# set proper umask | ||
umask "${ETCD_BACKUP_UMASK}" | ||
# check storage type | ||
if [ "${ETCD_BACKUP_S3}" = "true" ]; then | ||
# prepare & push backup to S3 | ||
|
||
# validate expire type | ||
case "${ETCD_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 [ "${ETCD_BACKUP_EXPIRE_TYPE}" = "days" ]; then | ||
case "${ETCD_BACKUP_KEEP_DAYS}" in | ||
''|*[!0-9]*) echo "backup.expiredays needs to be a valid number"; exit 1 ;; | ||
*) ;; | ||
esac | ||
elif [ "${ETCD_BACKUP_EXPIRE_TYPE}" = "count" ]; then | ||
case "${ETCD_BACKUP_KEEP_COUNT}" in | ||
''|*[!0-9]*) echo "backup.expirecount needs to be a valid number"; exit 1 ;; | ||
*) ;; | ||
# configure mcli assuming the bucket already exists | ||
bash +o history | ||
mcli alias set "${ETCD_BACKUP_S3_NAME}" "${ETCD_BACKUP_S3_HOST}" "${ETCD_BACKUP_S3_ACCESS_KEY}" "${ETCD_BACKUP_S3_SECRET_KEY}" | ||
bash -o history | ||
|
||
# make dirname | ||
BACKUP_FOLDER="$( date "${ETCD_BACKUP_DIRNAME}")" || { echo "Invalid backup.dirname" && exit 1; } | ||
|
||
# make necessary directory | ||
mkdir -p "/tmp/etcd-backup/${BACKUP_FOLDER}" | ||
|
||
# create backup to temporary location | ||
ETCDCTL_API=3 etcdctl --endpoints "${ENDPOINT}:2379" --cacert="/etc/kubernetes/pki/etcd-ca/ca.crt" --cert="/etc/kubernetes/pki/etcd-peer/tls.crt" --key="/etc/kubernetes/pki/etcd-peer/tls.key" snapshot save "/tmp/etcd-backup/${BACKUP_FOLDER}/snapshot.db" | ||
ETCDCTL_API=3 etcdutl --write-out=table snapshot status "/tmp/etcd-backup/${BACKUP_FOLDER}/snapshot.db" | ||
|
||
# move files to S3 and delete temporary files | ||
mcli mv $-r /tmp/etcd-backup/* "${ETCD_BACKUP_S3_NAME }"/"${ETCD_BACKUP_S3_BUCKET}" | ||
rm -rv /tmp/etcd-backup | ||
else | ||
# set proper umask | ||
umask "${ETCD_BACKUP_UMASK}" | ||
|
||
# validate expire type | ||
case "${ETCD_BACKUP_EXPIRE_TYPE}" in | ||
days|count|never) ;; | ||
*) echo "backup.expiretype needs to be one of: days,count,never"; exit 1 ;; | ||
esac | ||
fi | ||
|
||
# make dirname and cleanup paths | ||
BACKUP_FOLDER="$( date "${ETCD_BACKUP_DIRNAME}")" || { echo "Invalid backup.dirname" && exit 1; } | ||
BACKUP_PATH="$( realpath -m "${ETCD_BACKUP_SUBDIR}/${BACKUP_FOLDER}" )" | ||
BACKUP_PATH_POD="$( realpath -m "/backup/${BACKUP_PATH}" )" | ||
BACKUP_ROOTPATH="$( realpath -m "/backup/${ETCD_BACKUP_SUBDIR}" )" | ||
|
||
# make nescesary directorys | ||
mkdir -p "/tmp/etcd-backup" | ||
mkdir -p "${BACKUP_PATH_POD}" | ||
|
||
# create backup to temporary location | ||
ETCDCTL_API=3 etcdctl --endpoints "${ENDPOINT}:2379" --cacert='/etc/kubernetes/pki/etcd-ca/ca.crt' --cert='/etc/kubernetes/pki/etcd-peer/tls.crt' --key='/etc/kubernetes/pki/etcd-peer/tls.key' snapshot save /tmp/etcd-backup/snapshot.db | ||
ETCDCTL_API=3 etcdutl --write-out=table snapshot status /tmp/etcd-backup/snapshot.db | ||
|
||
# move files to pvc and delete temporary files | ||
mv /tmp/etcd-backup/* "${BACKUP_PATH_POD}" | ||
rm -rv /tmp/etcd-backup | ||
|
||
# expire backup | ||
if [ "${ETCD_BACKUP_EXPIRE_TYPE}" = "days" ]; then | ||
find "${BACKUP_ROOTPATH}" -mindepth 1 -maxdepth 1 -type d -mtime "+${ETCD_BACKUP_KEEP_DAYS}" -exec rm -rv {} + | ||
elif [ "${ETCD_BACKUP_EXPIRE_TYPE}" = "count" ]; then | ||
# shellcheck disable=SC3040,SC2012 | ||
ls -1tp "${BACKUP_ROOTPATH}" | awk "NR>${ETCD_BACKUP_KEEP_COUNT}" | xargs -I{} rm -rv "${BACKUP_ROOTPATH}/{}" | ||
# validate expire numbers | ||
if [ "${ETCD_BACKUP_EXPIRE_TYPE}" = "days" ]; then | ||
case "${ETCD_BACKUP_KEEP_DAYS}" in | ||
''|*[!0-9]*) echo "backup.expiredays needs to be a valid number"; exit 1 ;; | ||
*) ;; | ||
esac | ||
elif [ "${ETCD_BACKUP_EXPIRE_TYPE}" = "count" ]; then | ||
case "${ETCD_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 "${ETCD_BACKUP_DIRNAME}")" || { echo "Invalid backup.dirname" && exit 1; } | ||
BACKUP_PATH="$( realpath -m "${ETCD_BACKUP_SUBDIR}/${BACKUP_FOLDER}" )" | ||
BACKUP_PATH_POD="$( realpath -m "/backup/${BACKUP_PATH}" )" | ||
BACKUP_ROOTPATH="$( realpath -m "/backup/${ETCD_BACKUP_SUBDIR}" )" | ||
|
||
# make nescesary directorys | ||
mkdir -p "/tmp/etcd-backup" | ||
mkdir -p "${BACKUP_PATH_POD}" | ||
|
||
# create backup to temporary location | ||
ETCDCTL_API=3 etcdctl --endpoints "${ENDPOINT}:2379" --cacert='/etc/kubernetes/pki/etcd-ca/ca.crt' --cert='/etc/kubernetes/pki/etcd-peer/tls.crt' --key='/etc/kubernetes/pki/etcd-peer/tls.key' snapshot save /tmp/etcd-backup/snapshot.db | ||
ETCDCTL_API=3 etcdutl --write-out=table snapshot status /tmp/etcd-backup/snapshot.db | ||
|
||
# move files to pvc and delete temporary files | ||
mv /tmp/etcd-backup/* "${BACKUP_PATH_POD}" | ||
rm -rv /tmp/etcd-backup | ||
|
||
# expire backup | ||
if [ "${ETCD_BACKUP_EXPIRE_TYPE}" = "days" ]; then | ||
find "${BACKUP_ROOTPATH}" -mindepth 1 -maxdepth 1 -type d -mtime "+${ETCD_BACKUP_KEEP_DAYS}" -exec rm -rv {} + | ||
elif [ "${ETCD_BACKUP_EXPIRE_TYPE}" = "count" ]; then | ||
# shellcheck disable=SC3040,SC2012 | ||
ls -1tp "${BACKUP_ROOTPATH}" | awk "NR>${ETCD_BACKUP_KEEP_COUNT}" | xargs -I{} rm -rv "${BACKUP_ROOTPATH}/{}" | ||
fi | ||
fi |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want to manage the removal of old backups in there too with
mc
maybe? Then we would not need of a retention policy, what do you think?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know, I mostly focused on the first if statement, which publishes to s3. This part is responsible for nfs backups.
This part is edited, because I had to format it and that's all
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, well we could add that later if needed, so for now I suggest we are fine! Let's merge and test this then :)