Created
January 30, 2018 15:56
-
-
Save Mikulas/a609f4fa9ae3fecde52acd831c138233 to your computer and use it in GitHub Desktop.
Restore kops from versioned S3
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
#!/usr/bin/env bash | |
set -euo pipefail | |
IFS=$'\n\t' | |
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | |
# 2018-01-30T11:53:46.000Z | |
RESTORE_BEFORE="$1" | |
BUCKET="k8s.example.com" | |
PREFIX="eu-central-1.k8s.example.com" | |
function restore { | |
FILE="$1" | |
VERSION="$2" | |
TMP="/tmp/file" | |
set -x | |
aws s3api get-object --version-id="$VERSION" --bucket="$BUCKET" --key "$FILE" "$TMP" >/tmp/log.txt | |
aws s3 cp "$TMP" "s3://$BUCKET/$FILE" | |
{ set +x; } 2>/dev/null | |
} | |
IFS=$'\n' | |
for LINE in $(aws s3 ls --recursive "$BUCKET/$PREFIX/"); do | |
FILE="$(echo "$LINE" | awk '{print $4}')" | |
echo "$FILE" | |
for PAIR in $(aws s3api list-object-versions --bucket "$BUCKET" --prefix "$FILE" | jq -r '.Versions[] | "\(.LastModified) \(.VersionId)"'); do | |
CREATED_AT="$(echo "$PAIR" | awk '{print $1}')" | |
if [[ "$CREATED_AT" < "$RESTORE_BEFORE" ]]; then | |
echo " $CREATED_AT is good" | |
VERSION="$(echo "$PAIR" | awk '{print $2}')" | |
echo " restore $VERSION" | |
restore "$FILE" "$VERSION" | |
break | |
else | |
echo " $CREATED_AT skipped" | |
fi | |
done | |
read | |
done |
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
restore all files to versions older than "2018-01-30T11:53" | |
``` | |
restore-kops.sh "2018-01-30T11:53" | |
``` | |
restore all files to versions older than "2018-01-30" (eg <="2018-01-29") | |
``` | |
restore-kops.sh "2018-01-30" | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment