Skip to content
Snippets Groups Projects
Commit 72632c9e authored by Ing. Michal Švamberg's avatar Ing. Michal Švamberg
Browse files

Inicializacni verze skriptu

parent eb5b25e7
No related branches found
No related tags found
No related merge requests found
#!/bin/bash
: ${PROFILE:="cesnet_listvm_backup"}
: ${SRC_BUCKET:="resticlock"}
: ${DST_BUCKET:="restictmp"}
: ${DATE_THRESHOLD:="2024-02-26T10:20:30"}
AWS_OPTS="--endpoint-url https://s3.cl4.du.cesnet.cz --profile ${PROFILE}"
# Vytvoření destination bucketu, pokud neexistuje
if aws $AWS_OPTS s3 ls "s3://${DST_BUCKET}" 2>&1 | grep -q 'NoSuchBucket'
then
aws $AWS_OPTS s3 mb s3://${DST_BUCKET}
#aws $AWS_OPTS s3api put-bucket-versioning --bucket ${DST_BUCKET} --versioning-configuration Status=Enabled
fi
# Kopírování souborů včetně verzí
aws $AWS_OPTS s3api list-objects-v2 --bucket $SRC_BUCKET --query 'Contents[].Key' --output json | jq -r '.[]' | while read key
do
# Získej všechny verze tohoto objektu
versions=$(aws $AWS_OPTS s3api list-object-versions --bucket $SRC_BUCKET --prefix "$key" --output json)
# Filtruj verze podle data a vyber jen ty, které jsou mladší než datumový práh
version_id=$(echo "$versions" | jq -r --arg threshold "$DATE_THRESHOLD" '.Versions[] | select(.LastModified < $threshold) | .VersionId' | tail -1)
# Kopíruj vybranou verzi do cílového bucketu
if [ -n "$version_id" ]; then
aws $AWS_OPTS s3api copy-object --bucket $DST_BUCKET --key "$key" --copy-source "$SRC_BUCKET/$key?versionId=$version_id"
fi
done
#!/bin/bash
: ${BUCKET_NAME:=restictmp}
: ${PROFILE:="cesnet_listvm_backup"}
CESNET="--endpoint-url https://s3.cl4.du.cesnet.cz --profile ${PROFILE}"
# Get list of all object versions
aws ${CESNET} s3api list-object-versions --bucket "${BUCKET_NAME}" --output json | jq -r '.Versions[] | .Key + " " + .VersionId ' > /tmp/versions.txt
# Delete all object versions
while read -r LINE; do
KEY=$(awk '{print $1}' <<< "$LINE")
VERSION_ID=$(awk '{print $2}' <<< "$LINE")
aws ${CESNET} s3api delete-object --bucket "$BUCKET_NAME" --key "$KEY" --version-id="$VERSION_ID"
done < /tmp/versions.txt
# Get list of all delete markers
aws ${CESNET} s3api list-object-versions --bucket "$BUCKET_NAME" --output json | jq -r '.DeleteMarkers[] | .Key + " " + .VersionId ' > /tmp/delete_markers.txt
# Delete all delete markers
while read -r LINE; do
KEY=$(awk '{print $1}' <<< "$LINE")
VERSION_ID=$(awk '{print $2}' <<< "$LINE")
aws ${CESNET} s3api delete-object --bucket "$BUCKET_NAME" --key "$KEY" --version-id="$VERSION_ID"
done < /tmp/delete_markers.txt
rm /tmp/versions.txt
rm /tmp/delete_markers.txt
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment