Last active
March 6, 2025 04:57
-
-
Save mizzy/6c76520331ba5364f5a7ab147e42e186 to your computer and use it in GitHub Desktop.
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
#!/bin/bash | |
set -o pipefail | |
ORIGINAL_TABLE="User" | |
TMP_TABLE="${ORIGINAL_TABLE}-$(date +%Y%m%d%H%M%S)" | |
function wait_for_table_creation() { | |
local table_name=$1 | |
echo "Check the status of \"$table_name\"." | |
while true; do | |
ddb_table_status=$( | |
aws dynamodb describe-table --table-name "$table_name" | | |
jq -r .Table.TableStatus | |
) | |
if [ "$ddb_table_status" = "ACTIVE" ]; then | |
echo "Restoring \"$table_name\" has been completed." | |
break | |
else | |
echo "Current status: $ddb_table_status" | |
sleep 10 | |
fi | |
done | |
} | |
function wait_for_table_deletion { | |
local table_name=$1 | |
echo "Check the status of \"$table_name\"." | |
while true; do | |
ddb_table_status=$( | |
aws dynamodb describe-table --table-name "$table_name" 2>/dev/null | | |
jq -r .Table.TableStatus | |
) | |
if [ $? != 0 ]; then | |
echo "Deleting \"$table_name\" has been completed." | |
break | |
else | |
echo "Current status: $ddb_table_status" | |
sleep 10 | |
fi | |
done | |
} | |
# 別名でリストア | |
start_time=$(date +%s) | |
aws dynamodb restore-table-to-point-in-time \ | |
--source-table-name "$ORIGINAL_TABLE" \ | |
--target-table-name "$TMP_TABLE" \ | |
--use-latest-restorable-time >/dev/null | |
wait_for_table_creation "$TMP_TABLE" | |
end_time=$(date +%s) | |
elapsed_time=$((end_time - start_time)) | |
echo "Time taken to restore ${TMP_TABLE}: $(date -u -d "@$elapsed_time" +%H:%M:%S)" | |
echo | |
aws dynamodb update-continuous-backups \ | |
--table-name "$TMP_TABLE" \ | |
--point-in-time-recovery-specification PointInTimeRecoveryEnabled=true > /dev/null | |
# 元のテーブルを削除 | |
start_time=$(date +%s) | |
aws dynamodb delete-table --table-name ${ORIGINAL_TABLE} > /dev/null | |
wait_for_table_deletion ${ORIGINAL_TABLE} | |
end_time=$(date +%s) | |
elapsed_time=$((end_time - start_time)) | |
echo "Time taken to delete ${ORIGINAL_TABLE}: $(date -u -d "@$elapsed_time" +%H:%M:%S)" | |
echo | |
# 元テーブル名でリストア | |
start_time=$(date +%s) | |
aws dynamodb restore-table-to-point-in-time \ | |
--source-table-name "$TMP_TABLE" \ | |
--target-table-name "$ORIGINAL_TABLE" \ | |
--use-latest-restorable-time >/dev/null | |
wait_for_table_creation "$ORIGINAL_TABLE" | |
end_time=$(date +%s) | |
elapsed_time=$((end_time - start_time)) | |
echo "Time taken to restore ${ORIGINAL_TABLE}: $(date -u -d "@$elapsed_time" +%H:%M:%S)" | |
aws dynamodb update-continuous-backups \ | |
--table-name "$ORIGINAL_TABLE" \ | |
--point-in-time-recovery-specification PointInTimeRecoveryEnabled=true > /dev/null | |
# 一時テーブル不要なので削除 | |
aws dynamodb delete-table --table-name "${TMP_TABLE}" >/dev/null |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment