Skip to content

Instantly share code, notes, and snippets.

@Mikulas
Created January 30, 2018 15:56
Show Gist options
  • Save Mikulas/a609f4fa9ae3fecde52acd831c138233 to your computer and use it in GitHub Desktop.
Save Mikulas/a609f4fa9ae3fecde52acd831c138233 to your computer and use it in GitHub Desktop.
Restore kops from versioned S3
#!/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
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