Automatically create snapshots of Lightsail (AWS) instances. Less frequent (to save costs) and longer than the automation that lightsail offers.
Last active
April 4, 2024 13:03
-
-
Save gellweiler/d78bc316fe423701e993fcda8bc723f1 to your computer and use it in GitHub Desktop.
snapshot_manager.sh
This file contains hidden or 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
| stages: | |
| - snapshot | |
| create_snapshot: | |
| stage: snapshot | |
| image: | |
| name: amazon/aws-cli | |
| entrypoint: ["/bin/sh", "-c"] | |
| script: | |
| - bash ./snapshot_manager.sh new_snapshot | |
| only: | |
| - schedules | |
| delete_old_snapshots: | |
| stage: snapshot | |
| image: | |
| name: amazon/aws-cli | |
| entrypoint: ["/bin/sh", "-c"] | |
| variables: | |
| MAX_AGE_SNAPSHOTS_IN_DAYS: "30" | |
| script: | |
| - bash ./snapshot_manager.sh delete_old_snapshots | |
| only: | |
| - schedules |
This file contains hidden or 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 -euo pipefail | |
| INSTANCE_NAME="${INSTANCE_NAME:-autosave2}" | |
| MAX_AGE_SNAPSHOTS_IN_DAYS="${MAX_AGE_SNAPSHOTS_IN_DAYS:-30}" | |
| function new_snapshot { | |
| aws lightsail create-instance-snapshot \ | |
| --instance-snapshot-name "autosave_$(date +%Y%m%d)" \ | |
| --instance-name "$INSTANCE_NAME" | |
| } | |
| function delete_old_snapshots { | |
| # Get the current date in seconds since the epoch | |
| current_date=$(date +%s) | |
| # List all snapshots for the instance and parse their names | |
| aws lightsail get-instance-snapshots \ | |
| --query 'instanceSnapshots[?fromInstanceName==`'"$INSTANCE_NAME"'`].[name]' \ | |
| --output text | while read -r snapshot_name; do | |
| # Check if the snapshot name matches the autosave_%Y%m%d format | |
| if [[ $snapshot_name =~ ^autosave_([0-9]{8})$ ]]; then | |
| snapshot_date="${BASH_REMATCH[1]}" | |
| # Convert snapshot_date from YYYYMMDD to Unix timestamp | |
| snapshot_date_ts=$(date -d "$snapshot_date" +%s) | |
| # Calculate the difference in days from the current date | |
| diff=$(($current_date - $snapshot_date_ts)) | |
| days=$(($diff / 86400)) | |
| if [ "$days" -gt "$MAX_AGE_SNAPSHOTS_IN_DAYS" ]; then | |
| echo >&2 "Deleting snapshot: $snapshot_name, which is $days days old." | |
| # Uncomment the following line to enable actual deletion | |
| # aws lightsail delete-instance-snapshot --instance-snapshot-name "$snapshot_name" | |
| fi | |
| fi | |
| done | |
| } | |
| case "${1:-x}" in | |
| new_snapshot) new_snapshot ;; | |
| delete_old_snapshots) delete_old_snapshots ;; | |
| *) | |
| echo >&2 "usage:" | |
| echo >&2 "$0 new_snapshot|delete_old_snapshots" | |
| esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment